§ Описание

Скачать шаблон шаблон, файлы разнесены в lib.

§ main.cc

1#include <glut.cc>
2
3SCREEN(13,0);
4
5// Обработчик окна при вызове таймера
6void display()  {
7
8    for (int i = 0; i < 200; i++)
9    for (int j = 0; j < 320; j++)
10        pset(j, i, dac(i*j/200));
11
12    update();
13}

§ makefile

1all:
2	g++ main.cc -DLINUX_HOST -Ilib -O3 -lGL -lGLU -lglut -o main
3	strip main
4	./main

§ make.bat

1@echo off
2
3REM Only windows, only hardcore
4REM g++ main.cc -Ilib -mwindows -O3 glut32.lib -lopengl32 -lglu32 -m32 -o main.exe
5
6REM Console Edition Lite
7g++ main.cc -Ilib -O3 lib/glut32.lib -lopengl32 -lglu32 -m32 -o main.exe
8
9if %errorlevel% neq 0 goto :stop
10strip main.exe
11main.exe
12goto :end
13:stop
14pause
15:end

§ lib/glut.cc

1#include "glut.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5
6// Запустить экран
7void screen(int mode) {
8
9    int   argc = 0;
10    char* argv[1];
11
12    switch (mode) {
13
14        case 12: app_width = 640; app_height = 480; app_factor = 1; break;
15        case 13: app_width = 640; app_height = 400; app_factor = 2; break;
16        default: exit(1);
17    }
18
19    // Инициализация переменных
20    app_interval = 25;
21
22    // Запустить окно
23    glutInit(&argc, argv);
24    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
25    glutInitWindowSize(app_width, app_height);
26    glutInitWindowPosition(0, 0);
27    glutCreateWindow("GLUT BASIC");
28    glClearColor(0.0, 0.0, 0.0, 0.0);
29    glMatrixMode(GL_PROJECTION);
30    glLoadIdentity();
31    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
32
33    // Выделить необходимую область памяти
34    app_membuf = (unsigned char*) malloc(app_width * app_height * 4);
35}
36
37void start(int interval) {
38
39    glutDisplayFunc(display);
40
41    if (interval) {
42
43        app_interval = interval;
44        timer_function(0);
45    }
46
47    glutMainLoop();
48}
49
50// Обработчик таймера
51void timer_function(int value) {
52
53    glutPostRedisplay();
54    glutTimerFunc(app_interval, timer_function, 0);
55}
56
57// Преобразовать цвет в DOS
58int dac(int c) {
59    return doscolortable[c & 255];
60}
61
62// Нарисовать точку
63void pset(int x, int y, unsigned int cl) {
64
65    x *= app_factor;
66    y *= app_factor;
67
68    if (x < 0 || y < 0 || x >= app_width || y >= app_height)
69        return;
70
71    for (int i = 0; i < app_factor; i++)
72    for (int j = 0; j < app_factor; j++) {
73
74        int cursor = 4*(j+x + (app_height-1-i-y)*app_width);
75        app_membuf[cursor++] = cl>>16;
76        app_membuf[cursor++] = cl>>8;
77        app_membuf[cursor++] = cl;
78        app_membuf[cursor++] = 255;
79    }
80}
81
82// Получение точки с учетом factor
83int point(int x, int y) {
84
85    x *= app_factor;
86    y *= app_factor;
87
88    int cursor = 4*(x + (app_height-1-y)*app_width);
89
90    unsigned char r = app_membuf[cursor  ];
91    unsigned char g = app_membuf[cursor+1];
92    unsigned char b = app_membuf[cursor+2];
93
94    return (r*65536 + g*256 + b);
95}
96
97// Обновить экран
98void update() {
99
100    glRasterPos2i(-1, -1);
101    glDrawPixels(app_width, app_height, GL_RGBA, GL_UNSIGNED_BYTE, app_membuf);
102    glutSwapBuffers();
103}
104
105// Сохранение изображения в PPM
106void saveppm(const char* filename) {
107
108    char temp[256];
109
110    int w = app_width  / app_factor;
111    int h = app_height / app_factor;
112
113    FILE* fp = fopen(filename, "wb");
114    if (fp == NULL) exit(2);
115
116    sprintf(temp, "P6\n%d %d\n255\n", w, h);
117    fputs(temp, fp);
118
119    for (int y = 0; y < h; y++)
120    for (int x = 0; x < w; x++) {
121
122        int p = point(x, y);
123
124        temp[0] = p>>16;
125        temp[1] = p>>8;
126        temp[2] = p;
127
128        fwrite(temp, 1, 3, fp);
129    }
130
131    fclose(fp);
132}

§ lib/glut.h

1#ifndef LINUX_HOST
2#include <windows.h>
3#endif
4#include <GL/glut.h>
5
6// Макросы
7#define SCREEN(x,y) int main(int argc, char* argv[]) { screen(x); start(y); return 0; }
8
9// Свойства
10int app_width, app_height, app_factor;
11int app_interval;
12
13unsigned char* app_membuf;
14
15// Процедуры
16void    screen(int);        // Режим экрана
17void    start(int);         // Запуск в работу
18int     dac(int c);
19void    pset(int, int, unsigned int cl);
20void    update();
21
22// Должны быть реализованы как часть движка
23void    display();
24void    timer_function(int);
25
26// Таблица цветов DOS
27static const int doscolortable[256] = {
28    0x000000, 0x0000aa, 0x00aa00, 0x00aaaa, 0xaa0000, 0xaa00aa, 0xaa5500, 0xaaaaaa, // 0
29    0x555555, 0x5555ff, 0x55ff55, 0x55ffff, 0xff5555, 0xff55ff, 0xffff55, 0xffffff, // 8
30    0x000000, 0x141414, 0x202020, 0x2c2c2c, 0x383838, 0x454545, 0x515151, 0x616161, // 10
31    0x717171, 0x828282, 0x929292, 0xa2a2a2, 0xb6b6b6, 0xcbcbcb, 0xe3e3e3, 0xffffff, // 18
32    0x0000ff, 0x4100ff, 0x7d00ff, 0xbe00ff, 0xff00ff, 0xff00be, 0xff007d, 0xff0041, // 20
33    0xff0000, 0xff4100, 0xff7d00, 0xffbe00, 0xffff00, 0xbeff00, 0x7dff00, 0x41ff00, // 28
34    0x00ff00, 0x00ff41, 0x00ff7d, 0x00ffbe, 0x00ffff, 0x00beff, 0x007dff, 0x0041ff, // 30
35    0x7d7dff, 0x9e7dff, 0xbe7dff, 0xdf7dff, 0xff7dff, 0xff7ddf, 0xff7dbe, 0xff7d9e, // 38
36    0xff7d7d, 0xff9e7d, 0xffbe7d, 0xffdf7d, 0xffff7d, 0xdfff7d, 0xbeff7d, 0x9eff7d, // 40
37    0x7dff7d, 0x7dff9e, 0x7dffbe, 0x7dffdf, 0x7dffff, 0x7ddfff, 0x7dbeff, 0x7d9eff, // 48
38    0xb6b6ff, 0xc7b6ff, 0xdbb6ff, 0xebb6ff, 0xffb6ff, 0xffb6eb, 0xffb6db, 0xffb6c7, // 50
39    0xffb6b6, 0xffc7b6, 0xffdbb6, 0xffebb6, 0xffffb6, 0xebffb6, 0xdbffb6, 0xc7ffb6, // 58
40    0xb6ffb6, 0xb6ffc7, 0xb6ffdb, 0xb6ffeb, 0xb6ffff, 0xb6ebff, 0xb6dbff, 0xb6c7ff, // 60
41    0x000071, 0x1c0071, 0x380071, 0x550071, 0x710071, 0x710055, 0x710038, 0x71001c, // 68
42    0x710000, 0x711c00, 0x713800, 0x715500, 0x717100, 0x557100, 0x387100, 0x1c7100, // 70
43    0x007100, 0x00711c, 0x007138, 0x007155, 0x007171, 0x005571, 0x003871, 0x001c71, // 78
44    0x383871, 0x453871, 0x553871, 0x613871, 0x713871, 0x713861, 0x713855, 0x713845, // 80
45    0x713838, 0x714538, 0x715538, 0x716138, 0x717138, 0x617138, 0x557138, 0x457138, // 88
46    0x387138, 0x387145, 0x387155, 0x387161, 0x387171, 0x386171, 0x385571, 0x384571, // 90
47    0x515171, 0x595171, 0x615171, 0x695171, 0x715171, 0x715169, 0x715161, 0x715159, // 98
48    0x715151, 0x715951, 0x716151, 0x716951, 0x717151, 0x697151, 0x617151, 0x597151, // A0
49    0x517151, 0x517159, 0x517161, 0x517169, 0x517171, 0x516971, 0x516171, 0x515971, // A8
50    0x000041, 0x100041, 0x200041, 0x300041, 0x410041, 0x410030, 0x410020, 0x410010, // B0
51    0x410000, 0x411000, 0x412000, 0x413000, 0x414100, 0x304100, 0x204100, 0x104100, // B8
52    0x004100, 0x004110, 0x004120, 0x004130, 0x004141, 0x003041, 0x002041, 0x001041, // C0
53    0x202041, 0x282041, 0x302041, 0x382041, 0x412041, 0x412038, 0x412030, 0x412028, // C8
54    0x412020, 0x412820, 0x413020, 0x413820, 0x414120, 0x384120, 0x304120, 0x284120, // D0
55    0x204120, 0x204128, 0x204130, 0x204138, 0x204141, 0x203841, 0x203041, 0x202841, // D8
56    0x2c2c41, 0x302c41, 0x342c41, 0x3c2c41, 0x412c41, 0x412c3c, 0x412c34, 0x412c30, // E0
57    0x412c2c, 0x41302c, 0x41342c, 0x413c2c, 0x41412c, 0x3c412c, 0x34412c, 0x30412c, // E8
58    0x2c412c, 0x2c4130, 0x2c4134, 0x2c413c, 0x2c4141, 0x2c3c41, 0x2c3441, 0x2c3041, // F0
59    0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000  // F8
60};