-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
348 lines (288 loc) · 8.4 KB
/
main.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#include "context.h"
#include "arguments/arguments.h"
/**
* The width of the stereogram image.
*/
#define IMAGE_WIDTH 512
/**
* The height of the stereogram image.
*/
#define IMAGE_HEIGHT 512
/**
* The number of milliseconds between each redraw.
*/
#define TIMER_INTERVAL 40
/**
* The number of milliseconds late a render event may happen before rendering is
* ignored for the current frame.
*/
#define TIMER_MARGIN 10
/**
* The acceleration caused by the keys and the joystick.
*/
#define ACCELERATION 0.2
/**
* The user event code that signals that the display should be refreshed.
*/
#define USER_EVENT_DISPLAY (SDL_USEREVENT + 1)
/**
* Whether to prevent flooding the CPU with stereogram generation requests.
*
* If the CPU does not have time to render stereograms, the process may
* eventually slow down to a crawl. If too many frames are skipped though, the
* scene will jerk.
*/
static int prevent_flooding = 0;
/**
* The timer callback function.
*
* This function pushes an event to SDL to make it execute in the main thread.
*
* @param interval
* The timer interval.
* @param dummy
* Not used.
* @return the next timer interval, which is the same as the current
*/
static Uint32
do_timer(Uint32 interval, void *dummy)
{
SDL_Event event;
event.user.type = SDL_USEREVENT;
event.user.code = USER_EVENT_DISPLAY;
event.user.data1 = NULL;
event.user.data2 = NULL;
SDL_PushEvent(&event);
return interval;
}
/**
* Updates the display.
*
* @param context
* The context.
*/
static void
do_display(Context *context)
{
static Uint32 last_ticks = 0;
Uint32 current_ticks = SDL_GetTicks();
glLoadIdentity();
/* Render the context if we have not missed the render window */
if (!prevent_flooding || !last_ticks
|| current_ticks - last_ticks < TIMER_INTERVAL + TIMER_MARGIN) {
context_render(context);
}
last_ticks = current_ticks;
/* Update the target and camera */
context_target_move(context);
context_camera_move(context);
/* Render to screen */
SDL_GL_SwapBuffers();
}
/**
* Handles any pending SDL events.
*
* @return non-zero if the application should continue running and 0 otherwise
*/
static int
handle_events(Context *context)
{
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
/* Exit if the window is closed */
case SDL_QUIT:
return 0;
/* Check for keypresses */
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return 0;
case SDLK_SPACE:
context->gl.render_stereo = !context->gl.render_stereo;
break;
case SDLK_f:
prevent_flooding = !prevent_flooding;
break;
case SDLK_p:
context->stereo.update_pattern =
!context->stereo.update_pattern;
break;
case SDLK_t:
context->gl.apply_texture = !context->gl.apply_texture;
break;
case SDLK_UP:
context_target_accelerate_y(context, -ACCELERATION);
break;
case SDLK_DOWN:
context_target_accelerate_y(context, ACCELERATION);
break;
case SDLK_LEFT:
context_target_accelerate_x(context, -ACCELERATION);
break;
case SDLK_RIGHT:
context_target_accelerate_x(context, ACCELERATION);
break;
/* Prevent compiler warning */
default: break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_UP:
if (context->target.ay < 0.0) {
context_target_accelerate_y(context, 0.0);
}
break;
case SDLK_DOWN:
if (context->target.ay > 0.0) {
context_target_accelerate_y(context, 0.0);
}
break;
case SDLK_LEFT:
if (context->target.ax < 0.0) {
context_target_accelerate_x(context, 0.0);
}
break;
case SDLK_RIGHT:
if (context->target.ax > 0.0) {
context_target_accelerate_x(context, 0.0);
}
break;
/* Prevent compiler warning */
default: break;
}
break;
case SDL_JOYAXISMOTION:
switch (event.jaxis.axis) {
case 0:
context_target_accelerate_x(context,
ACCELERATION * (double)event.jaxis.value / 32768);
break;
case 1:
context_target_accelerate_y(context,
ACCELERATION * (double)event.jaxis.value / 32768);
break;
}
break;
case SDL_USEREVENT:
switch (event.user.code) {
case USER_EVENT_DISPLAY:
do_display(context);
break;
default: break;
}
break;
/* Prevent compiler warning */
default: break;
}
}
return 1;
}
/**
* Initialises OpenGL for the specified resolution.
*
* @param width, height
* The width and height of the viewport.
*/
static void
opengl_initialize(int width, int height)
{
/* Culling. */
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
/* Enable depth test */
glEnable(GL_DEPTH_TEST);
/* Set the clear color. */
glClearColor(0, 0, 0, 0);
/* Setup our viewport. */
glViewport(0, 0, width, height);
}
static int
main(int argc, char *argv[],
window_size_t window_size,
maze_size_t maze_size,
double wall_width,
double slope_width,
double shortcut_ratio,
double stereogram_strength,
StereoPattern *pattern_image)
{
/* Initialize SDL */
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
/* Hide the mouse cursor */
SDL_ShowCursor(0);
/* Get video information */
const SDL_VideoInfo *vinfo = SDL_GetVideoInfo();
if (!vinfo) {
printf("Unable to get video info: %s\n", SDL_GetError());
return 1;
}
/* Initialise the screen */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Surface* screen;
if (window_size.width > 0 && window_size.height > 0) {
screen = SDL_SetVideoMode(window_size.width, window_size.height,
32, SDL_OPENGL);
}
else {
screen = SDL_SetVideoMode(vinfo->current_w, vinfo->current_h,
32, SDL_OPENGL | SDL_FULLSCREEN);
}
if (!screen) {
printf("Unable to set %dx%d video: %s\n",
vinfo->current_w, vinfo->current_h, SDL_GetError());
return 1;
}
/* Setup OpenGL */
opengl_initialize(vinfo->current_w, vinfo->current_h);
/* Initialise the context */
Context context;
memset(&context, 0, sizeof(context));
if (!context_initialize(&context, IMAGE_WIDTH, IMAGE_HEIGHT,
vinfo->current_w, vinfo->current_h, pattern_image)) {
printf("Unable to initialise context.\n");
return 1;
}
/* Zero the cached value, since the pattern now is owned by the context */
ARGUMENT_VALUE(pattern_image) = NULL;
/* Create the timer */
SDL_TimerID timer = SDL_AddTimer(TIMER_INTERVAL, do_timer, NULL);
if (!timer) {
context_free(&context);
printf("Unable to add timer.\n");
return 1;
}
/* Open the joystick */
SDL_Joystick *joystick = NULL;
int jindex;
for (jindex = 0; jindex < SDL_NumJoysticks(); jindex++) {
joystick = SDL_JoystickOpen(jindex);
if (joystick) {
/* If we have at least two axes, use this joystick */
if (SDL_JoystickNumAxes(joystick) >= 2) {
printf("Found joystick %s\n", SDL_JoystickName(jindex));
break;
}
SDL_JoystickClose(joystick);
}
}
/* Enter the main loop */
while (handle_events(&context));
SDL_JoystickClose(joystick);
SDL_RemoveTimer(timer);
context_free(&context);
return 0;
}