Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (126 loc) · 2.45 KB
/
Copy pathmain.cpp
File metadata and controls
170 lines (126 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <SDL.h>
#include <GL/glew.h>
#include "panic.h"
#include "world.h"
namespace {
constexpr int WINDOW_WIDTH = 240;
constexpr int WINDOW_HEIGHT = 320;
constexpr int BORDER = 8;
}
static bool running = false;
static void
init_sdl()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
panic("SDL_Init: %s", SDL_GetError());
if (SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_OPENGL) == 0)
panic("SDL_SetVideoMode: %s", SDL_GetError());
}
static void
init_glew()
{
GLenum rv;
if ((rv = glewInit()) != GLEW_OK)
panic("glewInit: %s", glewGetErrorString(rv));
}
static void
init_gl_state()
{
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPolygonMode(GL_FRONT, GL_FILL);
}
static void
tear_down_sdl()
{
SDL_Quit();
}
static void
init()
{
init_sdl();
init_glew();
init_gl_state();
}
static void
tear_down()
{
tear_down_sdl();
}
static void
handle_events()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
running = false;
break;
}
}
}
#ifdef DUMP_FRAMES
static void
dump_frame(int frame_num)
{
static char buffer[3*WINDOW_WIDTH*WINDOW_HEIGHT];
glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, buffer);
char path[80];
sprintf(path, "%05d.ppm", frame_num);
FILE *out = fopen(path, "wb");
if (!out)
return;
fprintf(out, "P6\n%d %d\n255\n", WINDOW_WIDTH, WINDOW_HEIGHT);
fwrite(buffer, sizeof buffer, 1, out);
fclose(out);
++frame_num;
}
#endif
void
game_loop()
{
world w(WINDOW_WIDTH - 2*BORDER, WINDOW_HEIGHT - 2*BORDER);
#ifdef DUMP_FRAMES
int cur_frame = 0;
#endif
running = true;
static const int FRAME_INTERVAL = 33;
Uint32 next_frame = SDL_GetTicks() + FRAME_INTERVAL;
while (running) {
handle_events();
w.update();
w.update();
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(BORDER, BORDER, 0);
w.draw();
glPopMatrix();
#ifdef DUMP_FRAMES
if (cur_frame%2 == 0)
dump_frame(cur_frame/2);
++cur_frame;
#endif
SDL_GL_SwapBuffers();
Uint32 now = SDL_GetTicks();
if (now < next_frame)
SDL_Delay(next_frame - now);
next_frame += FRAME_INTERVAL;
}
}
int
main(int argc, char *argv[])
{
init();
game_loop();
tear_down();
return 0;
}