Ignore mouse motion events generated by SDL_WarpMouse.
[neverball] / share / config.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <math.h>
21
22 #include "config.h"
23 #include "glext.h"
24 #include "vec3.h"
25 #include "sync.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 /* Define the mkdir symbol. */
30
31 #ifdef _WIN32
32 #include <direct.h>
33 #else
34 #include <sys/stat.h>
35 #endif
36
37 /*---------------------------------------------------------------------------*/
38
39 static int   option_d[CONFIG_OPTION_D_COUNT];
40 static char *option_s[CONFIG_OPTION_S_COUNT];
41
42 static int dirty = 0;
43
44 /*---------------------------------------------------------------------------*/
45
46 static void config_key(const char *s, int i, int d)
47 {
48     int c;
49
50     config_set_d(i, d);
51
52     for (c = 0; c < SDLK_LAST; c++)
53         if (strcmp(s, SDL_GetKeyName((SDLKey) c)) == 0)
54         {
55             config_set_d(i, c);
56             break;
57         }
58 }
59
60 /*---------------------------------------------------------------------------*/
61
62 void config_init(void)
63 {
64     memset(option_d, 0, sizeof (option_d));
65     memset(option_s, 0, sizeof (option_s));
66
67     config_set_d(CONFIG_FULLSCREEN,           DEFAULT_FULLSCREEN);
68     config_set_d(CONFIG_WIDTH,                DEFAULT_WIDTH);
69     config_set_d(CONFIG_HEIGHT,               DEFAULT_HEIGHT);
70     config_set_d(CONFIG_STEREO,               DEFAULT_STEREO);
71     config_set_d(CONFIG_CAMERA,               DEFAULT_CAMERA);
72     config_set_d(CONFIG_TEXTURES,             DEFAULT_TEXTURES);
73     config_set_d(CONFIG_GEOMETRY,             DEFAULT_GEOMETRY);
74     config_set_d(CONFIG_REFLECTION,           DEFAULT_REFLECTION);
75     config_set_d(CONFIG_MULTISAMPLE,          DEFAULT_MULTISAMPLE);
76     config_set_d(CONFIG_MIPMAP,               DEFAULT_MIPMAP);
77     config_set_d(CONFIG_ANISO,                DEFAULT_ANISO);
78     config_set_d(CONFIG_BACKGROUND,           DEFAULT_BACKGROUND);
79     config_set_d(CONFIG_SHADOW,               DEFAULT_SHADOW);
80     config_set_d(CONFIG_AUDIO_BUFF,           DEFAULT_AUDIO_BUFF);
81     config_set_d(CONFIG_MOUSE_SENSE,          DEFAULT_MOUSE_SENSE);
82     config_set_d(CONFIG_MOUSE_INVERT,         DEFAULT_MOUSE_INVERT);
83     config_set_d(CONFIG_VSYNC,                DEFAULT_VSYNC);
84     config_set_d(CONFIG_NICE,                 DEFAULT_NICE);
85     config_set_d(CONFIG_FPS,                  DEFAULT_FPS);
86     config_set_d(CONFIG_SOUND_VOLUME,         DEFAULT_SOUND_VOLUME);
87     config_set_d(CONFIG_MUSIC_VOLUME,         DEFAULT_MUSIC_VOLUME);
88     config_set_d(CONFIG_JOYSTICK,             DEFAULT_JOYSTICK);
89     config_set_d(CONFIG_JOYSTICK_DEVICE,      DEFAULT_JOYSTICK_DEVICE);
90     config_set_d(CONFIG_JOYSTICK_AXIS_X,      DEFAULT_JOYSTICK_AXIS_X);
91     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      DEFAULT_JOYSTICK_AXIS_Y);
92     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    DEFAULT_JOYSTICK_BUTTON_A);
93     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    DEFAULT_JOYSTICK_BUTTON_B);
94     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    DEFAULT_JOYSTICK_BUTTON_L);
95     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    DEFAULT_JOYSTICK_BUTTON_R);
96     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, DEFAULT_JOYSTICK_BUTTON_EXIT);
97     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    DEFAULT_JOYSTICK_CAMERA_1);
98     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    DEFAULT_JOYSTICK_CAMERA_2);
99     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    DEFAULT_JOYSTICK_CAMERA_3);
100     config_set_d(CONFIG_JOYSTICK_DPAD_L,      DEFAULT_JOYSTICK_DPAD_L);
101     config_set_d(CONFIG_JOYSTICK_DPAD_R,      DEFAULT_JOYSTICK_DPAD_R);
102     config_set_d(CONFIG_JOYSTICK_DPAD_U,      DEFAULT_JOYSTICK_DPAD_U);
103     config_set_d(CONFIG_JOYSTICK_DPAD_D,      DEFAULT_JOYSTICK_DPAD_D);
104     config_set_d(CONFIG_KEY_CAMERA_1,         DEFAULT_KEY_CAMERA_1);
105     config_set_d(CONFIG_KEY_CAMERA_2,         DEFAULT_KEY_CAMERA_2);
106     config_set_d(CONFIG_KEY_CAMERA_3,         DEFAULT_KEY_CAMERA_3);
107     config_set_d(CONFIG_KEY_CAMERA_R,         DEFAULT_KEY_CAMERA_R);
108     config_set_d(CONFIG_KEY_CAMERA_L,         DEFAULT_KEY_CAMERA_L);
109     config_set_d(CONFIG_VIEW_FOV,             DEFAULT_VIEW_FOV);
110     config_set_d(CONFIG_VIEW_DP,              DEFAULT_VIEW_DP);
111     config_set_d(CONFIG_VIEW_DC,              DEFAULT_VIEW_DC);
112     config_set_d(CONFIG_VIEW_DZ,              DEFAULT_VIEW_DZ);
113     config_set_d(CONFIG_ROTATE_FAST,          DEFAULT_ROTATE_FAST);
114     config_set_d(CONFIG_ROTATE_SLOW,          DEFAULT_ROTATE_SLOW);
115     config_set_s(CONFIG_PLAYER,               DEFAULT_PLAYER);
116     config_set_s(CONFIG_BALL,                 DEFAULT_BALL);
117     config_set_s(CONFIG_WIIMOTE_ADDR,         DEFAULT_WIIMOTE_ADDR);
118     config_set_d(CONFIG_CHEAT,                DEFAULT_CHEAT);
119     config_set_d(CONFIG_STATS,                DEFAULT_STATS);
120     config_set_d(CONFIG_KEY_FORWARD,          DEFAULT_KEY_FORWARD);
121     config_set_d(CONFIG_KEY_BACKWARD,         DEFAULT_KEY_BACKWARD);
122     config_set_d(CONFIG_KEY_LEFT,             DEFAULT_KEY_LEFT);
123     config_set_d(CONFIG_KEY_RIGHT,            DEFAULT_KEY_RIGHT);
124     config_set_d(CONFIG_KEY_PAUSE,            DEFAULT_KEY_PAUSE);
125     config_set_d(CONFIG_KEY_RESTART,          DEFAULT_KEY_RESTART);
126 }
127
128 void config_load(void)
129 {
130     FILE *fp;
131
132     if ((fp = fopen(config_user(USER_CONFIG_FILE), "r")))
133     {
134         char buf[MAXSTR];
135         char key[MAXSTR];
136         char val[MAXSTR];
137
138         while (fgets(buf, MAXSTR, fp))
139             if (sscanf(buf, "%s %s", key, val) == 2)
140             {
141                 if      (strcmp(key, "fullscreen")            == 0)
142                     config_set_d(CONFIG_FULLSCREEN,           atoi(val));
143                 else if (strcmp(key, "width")                 == 0)
144                     config_set_d(CONFIG_WIDTH,                atoi(val));
145                 else if (strcmp(key, "height")                == 0)
146                     config_set_d(CONFIG_HEIGHT,               atoi(val));
147                 else if (strcmp(key, "stereo")                == 0)
148                     config_set_d(CONFIG_STEREO,               atoi(val));
149                 else if (strcmp(key, "camera")                == 0)
150                     config_set_d(CONFIG_CAMERA,               atoi(val));
151                 else if (strcmp(key, "textures")              == 0)
152                     config_set_d(CONFIG_TEXTURES,             atoi(val));
153                 else if (strcmp(key, "geometry")              == 0)
154                     config_set_d(CONFIG_GEOMETRY,             atoi(val));
155                 else if (strcmp(key, "reflection")            == 0)
156                     config_set_d(CONFIG_REFLECTION,           atoi(val));
157                 else if (strcmp(key, "multisample")           == 0)
158                     config_set_d(CONFIG_MULTISAMPLE,          atoi(val));
159                 else if (strcmp(key, "mipmap")                == 0)
160                     config_set_d(CONFIG_MIPMAP,               atoi(val));
161                 else if (strcmp(key, "aniso")                 == 0)
162                     config_set_d(CONFIG_ANISO,                atoi(val));
163                 else if (strcmp(key, "background")            == 0)
164                     config_set_d(CONFIG_BACKGROUND,           atoi(val));
165                 else if (strcmp(key, "shadow")                == 0)
166                     config_set_d(CONFIG_SHADOW,               atoi(val));
167                 else if (strcmp(key, "audio_buff")            == 0)
168                     config_set_d(CONFIG_AUDIO_BUFF,           atoi(val));
169                 else if (strcmp(key, "mouse_sense")           == 0)
170                     config_set_d(CONFIG_MOUSE_SENSE,          atoi(val));
171                 else if (strcmp(key, "mouse_invert")          == 0)
172                     config_set_d(CONFIG_MOUSE_INVERT,         atoi(val));
173                 else if (strcmp(key, "vsync")                 == 0)
174                     config_set_d(CONFIG_VSYNC,                atoi(val));
175                 else if (strcmp(key, "nice")                  == 0)
176                     config_set_d(CONFIG_NICE,                 atoi(val));
177                 else if (strcmp(key, "fps")                   == 0)
178                     config_set_d(CONFIG_FPS,                  atoi(val));
179                 else if (strcmp(key, "sound_volume")          == 0)
180                     config_set_d(CONFIG_SOUND_VOLUME,         atoi(val));
181                 else if (strcmp(key, "music_volume")          == 0)
182                     config_set_d(CONFIG_MUSIC_VOLUME,         atoi(val));
183                 else if (strcmp(key, "joystick")              == 0)
184                     config_set_d(CONFIG_JOYSTICK,             atoi(val));
185                 else if (strcmp(key, "joystick_device")       == 0)
186                     config_set_d(CONFIG_JOYSTICK_DEVICE,      atoi(val));
187                 else if (strcmp(key, "joystick_axis_x")       == 0)
188                     config_set_d(CONFIG_JOYSTICK_AXIS_X,      atoi(val));
189                 else if (strcmp(key, "joystick_axis_y")       == 0)
190                     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      atoi(val));
191                 else if (strcmp(key, "joystick_button_a")     == 0)
192                     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    atoi(val));
193                 else if (strcmp(key, "joystick_button_b")     == 0)
194                     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    atoi(val));
195                 else if (strcmp(key, "joystick_button_r")     == 0)
196                     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    atoi(val));
197                 else if (strcmp(key, "joystick_button_l")     == 0)
198                     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    atoi(val));
199                 else if (strcmp(key, "joystick_button_exit")  == 0)
200                     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, atoi(val));
201                 else if (strcmp(key, "joystick_camera_1")     == 0)
202                     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    atoi(val));
203                 else if (strcmp(key, "joystick_camera_2")     == 0)
204                     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    atoi(val));
205                 else if (strcmp(key, "joystick_camera_3")     == 0)
206                     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    atoi(val));
207                 else if (strcmp(key, "view_fov")              == 0)
208                     config_set_d(CONFIG_VIEW_FOV,             atoi(val));
209                 else if (strcmp(key, "view_dp")               == 0)
210                     config_set_d(CONFIG_VIEW_DP,              atoi(val));
211                 else if (strcmp(key, "view_dc")               == 0)
212                     config_set_d(CONFIG_VIEW_DC,              atoi(val));
213                 else if (strcmp(key, "view_dz")               == 0)
214                     config_set_d(CONFIG_VIEW_DZ,              atoi(val));
215                 else if (strcmp(key, "rotate_fast")           == 0)
216                     config_set_d(CONFIG_ROTATE_FAST,          atoi(val));
217                 else if (strcmp(key, "rotate_slow")           == 0)
218                     config_set_d(CONFIG_ROTATE_SLOW,          atoi(val));
219
220                 else if (strcmp(key, "key_forward")  == 0)
221                     config_key(val, CONFIG_KEY_FORWARD, DEFAULT_KEY_FORWARD);
222                 else if (strcmp(key, "key_backward")  == 0)
223                     config_key(val, CONFIG_KEY_BACKWARD, DEFAULT_KEY_BACKWARD);
224                 else if (strcmp(key, "key_left")  == 0)
225                     config_key(val, CONFIG_KEY_LEFT, DEFAULT_KEY_LEFT);
226                 else if (strcmp(key, "key_right")  == 0)
227                     config_key(val, CONFIG_KEY_RIGHT, DEFAULT_KEY_RIGHT);
228
229                 else if (strcmp(key, "key_camera_1")  == 0)
230                     config_key(val, CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1);
231                 else if (strcmp(key, "key_camera_2")  == 0)
232                     config_key(val, CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2);
233                 else if (strcmp(key, "key_camera_3")  == 0)
234                     config_key(val, CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3);
235                 else if (strcmp(key, "key_camera_r")  == 0)
236                     config_key(val, CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R);
237                 else if (strcmp(key, "key_camera_l")  == 0)
238                     config_key(val, CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L);
239
240                 else if (strcmp(key, "key_pause")    == 0)
241                     config_key(val, CONFIG_KEY_PAUSE,   DEFAULT_KEY_PAUSE);
242                 else if (strcmp(key, "key_restart")  == 0)
243                     config_key(val, CONFIG_KEY_RESTART, DEFAULT_KEY_RESTART);
244
245                 else if (strcmp(key, "player") == 0)
246                     config_set_s(CONFIG_PLAYER, val);
247                 else if (strcmp(key, "ball") == 0)
248                     config_set_s(CONFIG_BALL, val);
249                 else if (strcmp(key, "wiimote_addr") == 0)
250                     config_set_s(CONFIG_WIIMOTE_ADDR, val);
251
252                 else if (strcmp(key, "cheat") == 0)
253                     config_set_d(CONFIG_CHEAT, atoi(val));
254                 else if (strcmp(key, "stats") == 0)
255                     config_set_d(CONFIG_STATS, atoi(val));
256             }
257
258         fclose(fp);
259
260         dirty = 0;
261     }
262 }
263
264 void config_save(void)
265 {
266     FILE *fp;
267
268     if (dirty && (fp = fopen(config_user(USER_CONFIG_FILE), "w")))
269     {
270         fprintf(fp, "fullscreen           %d\n",
271                 option_d[CONFIG_FULLSCREEN]);
272         fprintf(fp, "width                %d\n",
273                 option_d[CONFIG_WIDTH]);
274         fprintf(fp, "height               %d\n",
275                 option_d[CONFIG_HEIGHT]);
276         fprintf(fp, "stereo               %d\n",
277                 option_d[CONFIG_STEREO]);
278         fprintf(fp, "camera               %d\n",
279                 option_d[CONFIG_CAMERA]);
280         fprintf(fp, "textures             %d\n",
281                 option_d[CONFIG_TEXTURES]);
282         fprintf(fp, "geometry             %d\n",
283                 option_d[CONFIG_GEOMETRY]);
284         fprintf(fp, "reflection           %d\n",
285                 option_d[CONFIG_REFLECTION]);
286         fprintf(fp, "multisample          %d\n",
287                 option_d[CONFIG_MULTISAMPLE]);
288         fprintf(fp, "mipmap               %d\n",
289                 option_d[CONFIG_MIPMAP]);
290         fprintf(fp, "aniso                %d\n",
291                 option_d[CONFIG_ANISO]);
292         fprintf(fp, "background           %d\n",
293                 option_d[CONFIG_BACKGROUND]);
294         fprintf(fp, "shadow               %d\n",
295                 option_d[CONFIG_SHADOW]);
296         fprintf(fp, "audio_buff           %d\n",
297                 option_d[CONFIG_AUDIO_BUFF]);
298         fprintf(fp, "mouse_sense          %d\n",
299                 option_d[CONFIG_MOUSE_SENSE]);
300         fprintf(fp, "mouse_invert         %d\n",
301                 option_d[CONFIG_MOUSE_INVERT]);
302         fprintf(fp, "vsync                %d\n",
303                 option_d[CONFIG_VSYNC]);
304         fprintf(fp, "nice                 %d\n",
305                 option_d[CONFIG_NICE]);
306         fprintf(fp, "fps                  %d\n",
307                 option_d[CONFIG_FPS]);
308         fprintf(fp, "sound_volume         %d\n",
309                 option_d[CONFIG_SOUND_VOLUME]);
310         fprintf(fp, "music_volume         %d\n",
311                 option_d[CONFIG_MUSIC_VOLUME]);
312         fprintf(fp, "joystick             %d\n",
313                 option_d[CONFIG_JOYSTICK]);
314         fprintf(fp, "joystick_device      %d\n",
315                 option_d[CONFIG_JOYSTICK_DEVICE]);
316         fprintf(fp, "joystick_axis_x      %d\n",
317                 option_d[CONFIG_JOYSTICK_AXIS_X]);
318         fprintf(fp, "joystick_axis_y      %d\n",
319                 option_d[CONFIG_JOYSTICK_AXIS_Y]);
320         fprintf(fp, "joystick_button_a    %d\n",
321                 option_d[CONFIG_JOYSTICK_BUTTON_A]);
322         fprintf(fp, "joystick_button_b    %d\n",
323                 option_d[CONFIG_JOYSTICK_BUTTON_B]);
324         fprintf(fp, "joystick_button_r    %d\n",
325                 option_d[CONFIG_JOYSTICK_BUTTON_R]);
326         fprintf(fp, "joystick_button_l    %d\n",
327                 option_d[CONFIG_JOYSTICK_BUTTON_L]);
328         fprintf(fp, "joystick_button_exit %d\n",
329                 option_d[CONFIG_JOYSTICK_BUTTON_EXIT]);
330         fprintf(fp, "joystick_camera_1    %d\n",
331                 option_d[CONFIG_JOYSTICK_CAMERA_1]);
332         fprintf(fp, "joystick_camera_2    %d\n",
333                 option_d[CONFIG_JOYSTICK_CAMERA_2]);
334         fprintf(fp, "joystick_camera_3    %d\n",
335                 option_d[CONFIG_JOYSTICK_CAMERA_3]);
336         fprintf(fp, "view_fov             %d\n",
337                 option_d[CONFIG_VIEW_FOV]);
338         fprintf(fp, "view_dp              %d\n",
339                 option_d[CONFIG_VIEW_DP]);
340         fprintf(fp, "view_dc              %d\n",
341                 option_d[CONFIG_VIEW_DC]);
342         fprintf(fp, "view_dz              %d\n",
343                 option_d[CONFIG_VIEW_DZ]);
344         fprintf(fp, "rotate_fast          %d\n",
345                 option_d[CONFIG_ROTATE_FAST]);
346         fprintf(fp, "rotate_slow          %d\n",
347                 option_d[CONFIG_ROTATE_SLOW]);
348         
349         fprintf(fp, "key_forward          %s\n",
350                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_FORWARD]));
351         fprintf(fp, "key_backward         %s\n",
352                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_BACKWARD]));
353         fprintf(fp, "key_left             %s\n",
354                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_LEFT]));
355         fprintf(fp, "key_right            %s\n",
356                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_RIGHT]));
357
358         fprintf(fp, "key_camera_1         %s\n",
359                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_CAMERA_1]));
360         fprintf(fp, "key_camera_2         %s\n",
361                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_CAMERA_2]));
362         fprintf(fp, "key_camera_3         %s\n",
363                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_CAMERA_3]));
364         fprintf(fp, "key_camera_r         %s\n",
365                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_CAMERA_R]));
366         fprintf(fp, "key_camera_l         %s\n",
367                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_CAMERA_L]));
368
369         fprintf(fp, "key_pause            %s\n",
370                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_PAUSE]));
371         fprintf(fp, "key_restart          %s\n",
372                 SDL_GetKeyName((SDLKey) option_d[CONFIG_KEY_RESTART]));
373
374         if (strlen(option_s[CONFIG_PLAYER]) > 0)
375             fprintf(fp, "player       %s\n", option_s[CONFIG_PLAYER]);
376         if (strlen(option_s[CONFIG_BALL]) > 0)
377             fprintf(fp, "ball         %s\n", option_s[CONFIG_BALL]);
378         if (strlen(option_s[CONFIG_WIIMOTE_ADDR]) > 0)
379             fprintf(fp, "wiimote_addr %s\n", option_s[CONFIG_WIIMOTE_ADDR]);
380
381         fprintf(fp, "stats                %d\n",
382                 option_d[CONFIG_STATS]);
383         if (config_cheat())
384             fprintf(fp, "cheat                %d\n", option_d[CONFIG_CHEAT]);
385
386         fclose(fp);
387     }
388
389     dirty = 0;
390 }
391
392 /*---------------------------------------------------------------------------*/
393
394 int check_extension(const char *needle)
395 {
396     const GLubyte *haystack, *c;
397
398     /* Search for the given string in the OpenGL extension strings. */
399
400     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
401     {
402         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
403             if (*c != *haystack)
404                 break;
405
406         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
407             return 1;
408     }
409
410     return 0;
411 }
412
413 int config_mode(int f, int w, int h)
414 {
415     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
416     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
417     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
418     int samples = config_get_d(CONFIG_MULTISAMPLE);
419
420     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
421     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
422     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
423     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
424
425     /* Try to set the currently specified mode. */
426
427     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
428     {
429         config_set_d(CONFIG_FULLSCREEN, f);
430         config_set_d(CONFIG_WIDTH,      w);
431         config_set_d(CONFIG_HEIGHT,     h);
432
433         glViewport(0, 0, w, h);
434         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
435
436         glEnable(GL_NORMALIZE);
437         glEnable(GL_CULL_FACE);
438         glEnable(GL_DEPTH_TEST);
439         glEnable(GL_TEXTURE_2D);
440         glEnable(GL_LIGHTING);
441         glEnable(GL_BLEND);
442
443         glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,
444                       GL_SEPARATE_SPECULAR_COLOR);
445         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
446
447         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
448         glDepthFunc(GL_LEQUAL);
449
450         if (config_get_d(CONFIG_VSYNC))
451             sync_init();
452
453         /* If GL supports multisample, and SDL got a multisample buffer... */
454
455 #ifdef GL_ARB_multisample
456         if (check_extension("ARB_multisample"))
457         {
458             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
459             if (buffers)
460                 glEnable(GL_MULTISAMPLE_ARB);
461         }
462 #endif
463
464         return 1;
465     }
466
467     /* If the mode failed, try it without stereo. */
468
469     else if (stereo)
470     {
471         config_set_d(CONFIG_STEREO, 0);
472         return config_mode(f, w, h);
473     }
474
475     /* If the mode failed, try decreasing the level of multisampling. */
476
477     else if (buffers)
478     {
479         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
480         return config_mode(f, w, h);
481     }
482
483     /* If that mode failed, try it without reflections. */
484
485     else if (stencil)
486     {
487         config_set_d(CONFIG_REFLECTION, 0);
488         return config_mode(f, w, h);
489     }
490
491     /* If THAT mode failed, punt. */
492
493     return 0;
494 }
495
496 /*---------------------------------------------------------------------------*/
497
498 static float ms     = 0;
499 static int   fps    = 0;
500 static int   last   = 0;
501 static int   ticks  = 0;
502 static int   frames = 0;
503
504 int  config_perf(void)
505 {
506     return fps;
507 }
508
509 void config_swap(void)
510 {
511     int dt;
512
513     SDL_GL_SwapBuffers();
514
515     /* Accumulate time passed and frames rendered. */
516
517     dt = (int) SDL_GetTicks() - last;
518
519     frames +=  1;
520     ticks  += dt;
521     last   += dt;
522
523     /* Average over 250ms. */
524
525     if (ticks > 1000)
526     {
527         /* Round the frames-per-second value to the nearest integer. */
528
529         double k = 1000.0 * frames / ticks;
530         double f = floor(k);
531         double c = ceil (k);
532
533         /* Compute frame time and frames-per-second stats. */
534
535         fps = (int) ((c - k < k - f) ? c : f);
536         ms  = (float) ticks / (float) frames;
537
538         /* Reset the counters for the next update. */
539
540         frames = 0;
541         ticks  = 0;
542
543         /* Output statistics if configured. */
544
545         if (option_d[CONFIG_STATS])
546             fprintf(stdout, "%4d %8.4f\n", fps, ms);
547     }
548 }
549
550 /*---------------------------------------------------------------------------*/
551
552 void config_set_d(int i, int d)
553 {
554     option_d[i] = d;
555     dirty = 1;
556 }
557
558 void config_tgl_d(int i)
559 {
560     option_d[i] = (option_d[i] ? 0 : 1);
561     dirty = 1;
562 }
563
564 int config_tst_d(int i, int d)
565 {
566     return (option_d[i] == d) ? 1 : 0;
567 }
568
569 int config_get_d(int i)
570 {
571     return option_d[i];
572 }
573
574 /*---------------------------------------------------------------------------*/
575
576 void config_set_s(int i, const char *src)
577 {
578     int len = (int) strlen(src);
579
580     if (option_s[i])
581         free(option_s[i]);
582
583     if ((option_s[i] = (char *) malloc(len + 1)))
584         strncpy(option_s[i], src, len + 1);
585
586     dirty = 1;
587 }
588
589 void config_get_s(int i, char *dst, int len)
590 {
591     strncpy(dst, option_s[i], len);
592 }
593
594 /*---------------------------------------------------------------------------*/
595
596 static int grabbed = 0;
597
598 void config_set_grab(int w)
599 {
600     if (w)
601     {
602         SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
603
604         SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
605                       config_get_d(CONFIG_HEIGHT) / 2);
606
607         SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
608     }
609
610     SDL_WM_GrabInput(SDL_GRAB_ON);
611     SDL_ShowCursor(SDL_DISABLE);
612
613     grabbed = 1;
614 }
615
616 void config_clr_grab(void)
617 {
618     SDL_WM_GrabInput(SDL_GRAB_OFF);
619     SDL_ShowCursor(SDL_ENABLE);
620     grabbed = 0;
621 }
622
623 int  config_get_grab(void)
624 {
625     return grabbed;
626 }
627
628 /*---------------------------------------------------------------------------*/
629
630 int config_cheat(void)
631 {
632     return config_get_d(CONFIG_CHEAT);
633 }
634
635 void config_set_cheat(void)
636 {
637     config_set_d(CONFIG_CHEAT, 1);
638 }
639
640 void config_clr_cheat(void)
641 {
642     config_set_d(CONFIG_CHEAT, 0);
643 }
644
645 /*---------------------------------------------------------------------------*/
646
647 void config_push_persp(float fov, float n, float f)
648 {
649     GLdouble m[4][4];
650
651     GLdouble r = fov / 2 * V_PI / 180;
652     GLdouble s = sin(r);
653     GLdouble c = cos(r) / s;
654
655     GLdouble a = ((GLdouble) option_d[CONFIG_WIDTH] /
656                   (GLdouble) option_d[CONFIG_HEIGHT]);
657
658     glMatrixMode(GL_PROJECTION);
659     {
660         glPushMatrix();
661         glLoadIdentity();
662
663         m[0][0] =  c/a;
664         m[0][1] =  0.0;
665         m[0][2] =  0.0;
666         m[0][3] =  0.0;
667         m[1][0] =  0.0;
668         m[1][1] =    c;
669         m[1][2] =  0.0;
670         m[1][3] =  0.0;
671         m[2][0] =  0.0;
672         m[2][1] =  0.0;
673         m[2][2] = -(f + n) / (f - n);
674         m[2][3] = -1.0;
675         m[3][0] =  0.0;
676         m[3][1] =  0.0;
677         m[3][2] = -2.0 * n * f / (f - n);
678         m[3][3] =  0.0;
679
680         glMultMatrixd(&m[0][0]);
681     }
682     glMatrixMode(GL_MODELVIEW);
683 }
684
685 void config_push_ortho(void)
686 {
687     GLdouble w = (GLdouble) option_d[CONFIG_WIDTH];
688     GLdouble h = (GLdouble) option_d[CONFIG_HEIGHT];
689
690     glMatrixMode(GL_PROJECTION);
691     {
692         glPushMatrix();
693         glLoadIdentity();
694         glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
695     }
696     glMatrixMode(GL_MODELVIEW);
697 }
698
699 void config_pop_matrix(void)
700 {
701     glMatrixMode(GL_PROJECTION);
702     {
703         glPopMatrix();
704     }
705     glMatrixMode(GL_MODELVIEW);
706 }
707
708 void config_clear(void)
709 {
710     if (option_d[CONFIG_REFLECTION])
711         glClear(GL_COLOR_BUFFER_BIT |
712                 GL_DEPTH_BUFFER_BIT |
713                 GL_STENCIL_BUFFER_BIT);
714     else
715         glClear(GL_COLOR_BUFFER_BIT |
716                 GL_DEPTH_BUFFER_BIT);
717 }
718
719 /*---------------------------------------------------------------------------*/