Oops! Fixed newly envmapped glass being scheduled as opaque.
[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
26 /*---------------------------------------------------------------------------*/
27
28 /* Define the mkdir symbol. */
29
30 #ifdef _WIN32
31 #include <direct.h>
32 #else
33 #include <sys/stat.h>
34 #endif
35
36 /*---------------------------------------------------------------------------*/
37
38 static int   option_d[CONFIG_OPTION_D_COUNT];
39 static char *option_s[CONFIG_OPTION_S_COUNT];
40
41 static int dirty = 0;
42
43 /*---------------------------------------------------------------------------*/
44
45 static void config_key(const char *s, int i, int d)
46 {
47     int c;
48
49     config_set_d(i, d);
50
51     for (c = 0; c < SDLK_LAST; c++)
52         if (strcmp(s, SDL_GetKeyName(c)) == 0)
53         {
54             config_set_d(i, c);
55             break;
56         }
57 }
58
59 /*---------------------------------------------------------------------------*/
60
61 void config_init(void)
62 {
63     memset(option_d, 0, sizeof (option_d));
64     memset(option_s, 0, sizeof (option_s));
65
66     config_set_d(CONFIG_FULLSCREEN,           DEFAULT_FULLSCREEN);
67     config_set_d(CONFIG_WIDTH,                DEFAULT_WIDTH);
68     config_set_d(CONFIG_HEIGHT,               DEFAULT_HEIGHT);
69     config_set_d(CONFIG_STEREO,               DEFAULT_STEREO);
70     config_set_d(CONFIG_CAMERA,               DEFAULT_CAMERA);
71     config_set_d(CONFIG_TEXTURES,             DEFAULT_TEXTURES);
72     config_set_d(CONFIG_GEOMETRY,             DEFAULT_GEOMETRY);
73     config_set_d(CONFIG_REFLECTION,           DEFAULT_REFLECTION);
74     config_set_d(CONFIG_MULTISAMPLE,          DEFAULT_MULTISAMPLE);
75     config_set_d(CONFIG_MIPMAP,               DEFAULT_MIPMAP);
76     config_set_d(CONFIG_ANISO,                DEFAULT_ANISO);
77     config_set_d(CONFIG_BACKGROUND,           DEFAULT_BACKGROUND);
78     config_set_d(CONFIG_SHADOW,               DEFAULT_SHADOW);
79     config_set_d(CONFIG_AUDIO_BUFF,           DEFAULT_AUDIO_BUFF);
80     config_set_d(CONFIG_MOUSE_SENSE,          DEFAULT_MOUSE_SENSE);
81     config_set_d(CONFIG_MOUSE_INVERT,         DEFAULT_MOUSE_INVERT);
82     config_set_d(CONFIG_NICE,                 DEFAULT_NICE);
83     config_set_d(CONFIG_FPS,                  DEFAULT_FPS);
84     config_set_d(CONFIG_SOUND_VOLUME,         DEFAULT_SOUND_VOLUME);
85     config_set_d(CONFIG_MUSIC_VOLUME,         DEFAULT_MUSIC_VOLUME);
86     config_set_d(CONFIG_JOYSTICK,             DEFAULT_JOYSTICK);
87     config_set_d(CONFIG_JOYSTICK_DEVICE,      DEFAULT_JOYSTICK_DEVICE);
88     config_set_d(CONFIG_JOYSTICK_AXIS_X,      DEFAULT_JOYSTICK_AXIS_X);
89     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      DEFAULT_JOYSTICK_AXIS_Y);
90     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    DEFAULT_JOYSTICK_BUTTON_A);
91     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    DEFAULT_JOYSTICK_BUTTON_B);
92     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    DEFAULT_JOYSTICK_BUTTON_L);
93     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    DEFAULT_JOYSTICK_BUTTON_R);
94     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, DEFAULT_JOYSTICK_BUTTON_EXIT);
95     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    DEFAULT_JOYSTICK_CAMERA_1);
96     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    DEFAULT_JOYSTICK_CAMERA_2);
97     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    DEFAULT_JOYSTICK_CAMERA_3);
98     config_set_d(CONFIG_KEY_CAMERA_1,         DEFAULT_KEY_CAMERA_1);
99     config_set_d(CONFIG_KEY_CAMERA_2,         DEFAULT_KEY_CAMERA_2);
100     config_set_d(CONFIG_KEY_CAMERA_3,         DEFAULT_KEY_CAMERA_3);
101     config_set_d(CONFIG_KEY_CAMERA_R,         DEFAULT_KEY_CAMERA_R);
102     config_set_d(CONFIG_KEY_CAMERA_L,         DEFAULT_KEY_CAMERA_L);
103     config_set_d(CONFIG_VIEW_FOV,             DEFAULT_VIEW_FOV);
104     config_set_d(CONFIG_VIEW_DP,              DEFAULT_VIEW_DP);
105     config_set_d(CONFIG_VIEW_DC,              DEFAULT_VIEW_DC);
106     config_set_d(CONFIG_VIEW_DZ,              DEFAULT_VIEW_DZ);
107     config_set_d(CONFIG_ROTATE_FAST,          DEFAULT_ROTATE_FAST);
108     config_set_d(CONFIG_ROTATE_SLOW,          DEFAULT_ROTATE_SLOW);
109     config_set_s(CONFIG_PLAYER,               DEFAULT_PLAYER);
110     config_set_s(CONFIG_BALL,                 DEFAULT_BALL);
111     config_set_d(CONFIG_CHEAT,                DEFAULT_CHEAT);
112     config_set_d(CONFIG_KEY_FORWARD,          DEFAULT_KEY_FORWARD);
113     config_set_d(CONFIG_KEY_BACKWARD,         DEFAULT_KEY_BACKWARD);
114     config_set_d(CONFIG_KEY_LEFT,             DEFAULT_KEY_LEFT);
115     config_set_d(CONFIG_KEY_RIGHT,            DEFAULT_KEY_RIGHT);
116     config_set_d(CONFIG_KEY_PAUSE,            DEFAULT_KEY_PAUSE);
117     config_set_d(CONFIG_KEY_RESTART,          DEFAULT_KEY_RESTART);
118 }
119
120 void config_load(void)
121 {
122     FILE *fp;
123
124     if ((fp = fopen(config_user(USER_CONFIG_FILE), "r")))
125     {
126         char buf[MAXSTR];
127         char key[MAXSTR];
128         char val[MAXSTR];
129
130         while (fgets(buf, MAXSTR, fp))
131             if (sscanf(buf, "%s %s", key, val) == 2)
132             {
133                 if      (strcmp(key, "fullscreen")            == 0)
134                     config_set_d(CONFIG_FULLSCREEN,           atoi(val));
135                 else if (strcmp(key, "width")                 == 0)
136                     config_set_d(CONFIG_WIDTH,                atoi(val));
137                 else if (strcmp(key, "height")                == 0)
138                     config_set_d(CONFIG_HEIGHT,               atoi(val));
139                 else if (strcmp(key, "stereo")                == 0)
140                     config_set_d(CONFIG_STEREO,               atoi(val));
141                 else if (strcmp(key, "camera")                == 0)
142                     config_set_d(CONFIG_CAMERA,               atoi(val));
143                 else if (strcmp(key, "textures")              == 0)
144                     config_set_d(CONFIG_TEXTURES,             atoi(val));
145                 else if (strcmp(key, "geometry")              == 0)
146                     config_set_d(CONFIG_GEOMETRY,             atoi(val));
147                 else if (strcmp(key, "reflection")            == 0)
148                     config_set_d(CONFIG_REFLECTION,           atoi(val));
149                 else if (strcmp(key, "multisample")           == 0)
150                     config_set_d(CONFIG_MULTISAMPLE,          atoi(val));
151                 else if (strcmp(key, "mipmap")                == 0)
152                     config_set_d(CONFIG_MIPMAP,               atoi(val));
153                 else if (strcmp(key, "aniso")                 == 0)
154                     config_set_d(CONFIG_ANISO,                atoi(val));
155                 else if (strcmp(key, "background")            == 0)
156                     config_set_d(CONFIG_BACKGROUND,           atoi(val));
157                 else if (strcmp(key, "shadow")                == 0)
158                     config_set_d(CONFIG_SHADOW,               atoi(val));
159                 else if (strcmp(key, "audio_buff")            == 0)
160                     config_set_d(CONFIG_AUDIO_BUFF,           atoi(val));
161                 else if (strcmp(key, "mouse_sense")           == 0)
162                     config_set_d(CONFIG_MOUSE_SENSE,          atoi(val));
163                 else if (strcmp(key, "mouse_invert")          == 0)
164                     config_set_d(CONFIG_MOUSE_INVERT,         atoi(val));
165                 else if (strcmp(key, "nice")                  == 0)
166                     config_set_d(CONFIG_NICE,                 atoi(val));
167                 else if (strcmp(key, "fps")                   == 0)
168                     config_set_d(CONFIG_FPS,                  atoi(val));
169                 else if (strcmp(key, "sound_volume")          == 0)
170                     config_set_d(CONFIG_SOUND_VOLUME,         atoi(val));
171                 else if (strcmp(key, "music_volume")          == 0)
172                     config_set_d(CONFIG_MUSIC_VOLUME,         atoi(val));
173                 else if (strcmp(key, "joystick")              == 0)
174                     config_set_d(CONFIG_JOYSTICK,             atoi(val));
175                 else if (strcmp(key, "joystick_device")       == 0)
176                     config_set_d(CONFIG_JOYSTICK_DEVICE,      atoi(val));
177                 else if (strcmp(key, "joystick_axis_x")       == 0)
178                     config_set_d(CONFIG_JOYSTICK_AXIS_X,      atoi(val));
179                 else if (strcmp(key, "joystick_axis_y")       == 0)
180                     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      atoi(val));
181                 else if (strcmp(key, "joystick_button_a")     == 0)
182                     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    atoi(val));
183                 else if (strcmp(key, "joystick_button_b")     == 0)
184                     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    atoi(val));
185                 else if (strcmp(key, "joystick_button_r")     == 0)
186                     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    atoi(val));
187                 else if (strcmp(key, "joystick_button_l")     == 0)
188                     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    atoi(val));
189                 else if (strcmp(key, "joystick_button_exit")  == 0)
190                     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, atoi(val));
191                 else if (strcmp(key, "joystick_camera_1")     == 0)
192                     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    atoi(val));
193                 else if (strcmp(key, "joystick_camera_2")     == 0)
194                     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    atoi(val));
195                 else if (strcmp(key, "joystick_camera_3")     == 0)
196                     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    atoi(val));
197                 else if (strcmp(key, "view_fov")              == 0)
198                     config_set_d(CONFIG_VIEW_FOV,             atoi(val));
199                 else if (strcmp(key, "view_dp")               == 0)
200                     config_set_d(CONFIG_VIEW_DP,              atoi(val));
201                 else if (strcmp(key, "view_dc")               == 0)
202                     config_set_d(CONFIG_VIEW_DC,              atoi(val));
203                 else if (strcmp(key, "view_dz")               == 0)
204                     config_set_d(CONFIG_VIEW_DZ,              atoi(val));
205                 else if (strcmp(key, "rotate_fast")           == 0)
206                     config_set_d(CONFIG_ROTATE_FAST,          atoi(val));
207                 else if (strcmp(key, "rotate_slow")           == 0)
208                     config_set_d(CONFIG_ROTATE_SLOW,          atoi(val));
209
210                 else if (strcmp(key, "key_forward")  == 0)
211                     config_key(val, CONFIG_KEY_FORWARD, DEFAULT_KEY_FORWARD);
212                 else if (strcmp(key, "key_backward")  == 0)
213                     config_key(val, CONFIG_KEY_BACKWARD, DEFAULT_KEY_BACKWARD);
214                 else if (strcmp(key, "key_left")  == 0)
215                     config_key(val, CONFIG_KEY_LEFT, DEFAULT_KEY_LEFT);
216                 else if (strcmp(key, "key_right")  == 0)
217                     config_key(val, CONFIG_KEY_RIGHT, DEFAULT_KEY_RIGHT);
218
219                 else if (strcmp(key, "key_camera_1")  == 0)
220                     config_key(val, CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1);
221                 else if (strcmp(key, "key_camera_2")  == 0)
222                     config_key(val, CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2);
223                 else if (strcmp(key, "key_camera_3")  == 0)
224                     config_key(val, CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3);
225                 else if (strcmp(key, "key_camera_r")  == 0)
226                     config_key(val, CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R);
227                 else if (strcmp(key, "key_camera_l")  == 0)
228                     config_key(val, CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L);
229
230                 else if (strcmp(key, "key_pause")    == 0)
231                     config_key(val, CONFIG_KEY_PAUSE,   DEFAULT_KEY_PAUSE);
232                 else if (strcmp(key, "key_restart")  == 0)
233                     config_key(val, CONFIG_KEY_RESTART, DEFAULT_KEY_RESTART);
234
235                 else if (strcmp(key, "player") == 0)
236                     config_set_s(CONFIG_PLAYER, val);
237                 else if (strcmp(key, "ball") == 0)
238                     config_set_s(CONFIG_BALL, val);
239
240                 else if (strcmp(key, "cheat") == 0)
241                     config_set_d(CONFIG_CHEAT, atoi(val));
242             }
243
244         fclose(fp);
245
246         dirty = 0;
247     }
248 }
249
250 void config_save(void)
251 {
252     FILE *fp;
253
254     if (dirty && (fp = fopen(config_user(USER_CONFIG_FILE), "w")))
255     {
256         fprintf(fp, "fullscreen           %d\n",
257                 option_d[CONFIG_FULLSCREEN]);
258         fprintf(fp, "width                %d\n",
259                 option_d[CONFIG_WIDTH]);
260         fprintf(fp, "height               %d\n",
261                 option_d[CONFIG_HEIGHT]);
262         fprintf(fp, "stereo               %d\n",
263                 option_d[CONFIG_STEREO]);
264         fprintf(fp, "camera               %d\n",
265                 option_d[CONFIG_CAMERA]);
266         fprintf(fp, "textures             %d\n",
267                 option_d[CONFIG_TEXTURES]);
268         fprintf(fp, "geometry             %d\n",
269                 option_d[CONFIG_GEOMETRY]);
270         fprintf(fp, "reflection           %d\n",
271                 option_d[CONFIG_REFLECTION]);
272         fprintf(fp, "multisample          %d\n",
273                 option_d[CONFIG_MULTISAMPLE]);
274         fprintf(fp, "mipmap               %d\n",
275                 option_d[CONFIG_MIPMAP]);
276         fprintf(fp, "aniso                %d\n",
277                 option_d[CONFIG_ANISO]);
278         fprintf(fp, "background           %d\n",
279                 option_d[CONFIG_BACKGROUND]);
280         fprintf(fp, "shadow               %d\n",
281                 option_d[CONFIG_SHADOW]);
282         fprintf(fp, "audio_buff           %d\n",
283                 option_d[CONFIG_AUDIO_BUFF]);
284         fprintf(fp, "mouse_sense          %d\n",
285                 option_d[CONFIG_MOUSE_SENSE]);
286         fprintf(fp, "mouse_invert         %d\n",
287                 option_d[CONFIG_MOUSE_INVERT]);
288         fprintf(fp, "nice                 %d\n",
289                 option_d[CONFIG_NICE]);
290         fprintf(fp, "fps                  %d\n",
291                 option_d[CONFIG_FPS]);
292         fprintf(fp, "sound_volume         %d\n",
293                 option_d[CONFIG_SOUND_VOLUME]);
294         fprintf(fp, "music_volume         %d\n",
295                 option_d[CONFIG_MUSIC_VOLUME]);
296         fprintf(fp, "joystick             %d\n",
297                 option_d[CONFIG_JOYSTICK]);
298         fprintf(fp, "joystick_device      %d\n",
299                 option_d[CONFIG_JOYSTICK_DEVICE]);
300         fprintf(fp, "joystick_axis_x      %d\n",
301                 option_d[CONFIG_JOYSTICK_AXIS_X]);
302         fprintf(fp, "joystick_axis_y      %d\n",
303                 option_d[CONFIG_JOYSTICK_AXIS_Y]);
304         fprintf(fp, "joystick_button_a    %d\n",
305                 option_d[CONFIG_JOYSTICK_BUTTON_A]);
306         fprintf(fp, "joystick_button_b    %d\n",
307                 option_d[CONFIG_JOYSTICK_BUTTON_B]);
308         fprintf(fp, "joystick_button_r    %d\n",
309                 option_d[CONFIG_JOYSTICK_BUTTON_R]);
310         fprintf(fp, "joystick_button_l    %d\n",
311                 option_d[CONFIG_JOYSTICK_BUTTON_L]);
312         fprintf(fp, "joystick_button_exit %d\n",
313                 option_d[CONFIG_JOYSTICK_BUTTON_EXIT]);
314         fprintf(fp, "joystick_camera_1    %d\n",
315                 option_d[CONFIG_JOYSTICK_CAMERA_1]);
316         fprintf(fp, "joystick_camera_2    %d\n",
317                 option_d[CONFIG_JOYSTICK_CAMERA_2]);
318         fprintf(fp, "joystick_camera_3    %d\n",
319                 option_d[CONFIG_JOYSTICK_CAMERA_3]);
320         fprintf(fp, "view_fov             %d\n",
321                 option_d[CONFIG_VIEW_FOV]);
322         fprintf(fp, "view_dp              %d\n",
323                 option_d[CONFIG_VIEW_DP]);
324         fprintf(fp, "view_dc              %d\n",
325                 option_d[CONFIG_VIEW_DC]);
326         fprintf(fp, "view_dz              %d\n",
327                 option_d[CONFIG_VIEW_DZ]);
328         fprintf(fp, "rotate_fast          %d\n",
329                 option_d[CONFIG_ROTATE_FAST]);
330         fprintf(fp, "rotate_slow          %d\n",
331                 option_d[CONFIG_ROTATE_SLOW]);
332
333         fprintf(fp, "key_forward          %s\n",
334                 SDL_GetKeyName(option_d[CONFIG_KEY_FORWARD]));
335         fprintf(fp, "key_backward         %s\n",
336                 SDL_GetKeyName(option_d[CONFIG_KEY_BACKWARD]));
337         fprintf(fp, "key_left             %s\n",
338                 SDL_GetKeyName(option_d[CONFIG_KEY_LEFT]));
339         fprintf(fp, "key_right            %s\n",
340                 SDL_GetKeyName(option_d[CONFIG_KEY_RIGHT]));
341
342         fprintf(fp, "key_camera_1         %s\n",
343                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_1]));
344         fprintf(fp, "key_camera_2         %s\n",
345                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_2]));
346         fprintf(fp, "key_camera_3         %s\n",
347                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_3]));
348         fprintf(fp, "key_camera_r         %s\n",
349                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_R]));
350         fprintf(fp, "key_camera_l         %s\n",
351                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_L]));
352
353         fprintf(fp, "key_pause            %s\n",
354                 SDL_GetKeyName(option_d[CONFIG_KEY_PAUSE]));
355         fprintf(fp, "key_restart          %s\n",
356                 SDL_GetKeyName(option_d[CONFIG_KEY_RESTART]));
357
358         fprintf(fp, "player               %s\n", option_s[CONFIG_PLAYER]);
359         fprintf(fp, "ball                 %s\n", option_s[CONFIG_BALL]);
360
361         if (config_cheat())
362             fprintf(fp, "cheat                %d\n", option_d[CONFIG_CHEAT]);
363
364         fclose(fp);
365     }
366
367     dirty = 0;
368 }
369
370 /*---------------------------------------------------------------------------*/
371
372 int check_extension(const char *needle)
373 {
374     const GLubyte *haystack, *c;
375
376     /* Search for the given string in the OpenGL extension strings. */
377
378     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
379     {
380         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
381             if (*c != *haystack)
382                 break;
383
384         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
385             return 1;
386     }
387
388     return 0;
389 }
390
391 int config_mode(int f, int w, int h)
392 {
393     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
394     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
395     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
396     int samples = config_get_d(CONFIG_MULTISAMPLE);
397
398     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
399     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
400     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
401     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
402
403     /* Try to set the currently specified mode. */
404
405     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
406     {
407         config_set_d(CONFIG_FULLSCREEN, f);
408         config_set_d(CONFIG_WIDTH, w);
409         config_set_d(CONFIG_HEIGHT, h);
410
411         glViewport(0, 0, w, h);
412         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
413
414         glEnable(GL_NORMALIZE);
415         glEnable(GL_CULL_FACE);
416         glEnable(GL_DEPTH_TEST);
417         glEnable(GL_TEXTURE_2D);
418         glEnable(GL_LIGHTING);
419
420         /* If GL supports multisample, and SDL got a multisample buffer... */
421
422 #ifdef GL_ARB_multisample
423         if (check_extension("ARB_multisample"))
424         {
425             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
426             if (buffers)
427                 glEnable(GL_MULTISAMPLE_ARB);
428         }
429 #endif
430
431         return 1;
432     }
433
434     /* If the mode failed, try it without stereo. */
435
436     else if (stereo)
437     {
438         config_set_d(CONFIG_STEREO, 0);
439         return config_mode(f, w, h);
440     }
441
442     /* If the mode failed, try decreasing the level of multisampling. */
443
444     else if (buffers)
445     {
446         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
447         return config_mode(f, w, h);
448     }
449
450     /* If that mode failed, try it without reflections. */
451
452     else if (stencil)
453     {
454         config_set_d(CONFIG_REFLECTION, 0);
455         return config_mode(f, w, h);
456     }
457
458     /* If THAT mode failed, punt. */
459
460     return 0;
461 }
462
463 /*---------------------------------------------------------------------------*/
464
465 void config_set_d(int i, int d)
466 {
467     option_d[i] = d;
468     dirty = 1;
469 }
470
471 void config_tgl_d(int i)
472 {
473     option_d[i] = (option_d[i] ? 0 : 1);
474     dirty = 1;
475 }
476
477 int config_tst_d(int i, int d)
478 {
479     return (option_d[i] == d) ? 1 : 0;
480 }
481
482 int config_get_d(int i)
483 {
484     return option_d[i];
485 }
486
487 /*---------------------------------------------------------------------------*/
488
489 void config_set_s(int i, const char *src)
490 {
491     int len = (int) strlen(src);
492
493     if (option_s[i])
494         free(option_s[i]);
495
496     if ((option_s[i] = (char *) malloc(len + 1)))
497         strncpy(option_s[i], src, len + 1);
498
499     dirty = 1;
500 }
501
502 void config_get_s(int i, char *dst, int len)
503 {
504     strncpy(dst, option_s[i], len);
505 }
506
507 /*---------------------------------------------------------------------------*/
508
509 static int grabbed = 0;
510
511 void config_set_grab(int w)
512 {
513     if (w)
514         SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
515                       config_get_d(CONFIG_HEIGHT) / 2);
516     SDL_WM_GrabInput(SDL_GRAB_ON);
517     SDL_ShowCursor(SDL_DISABLE);
518     grabbed = 1;
519 }
520
521 void config_clr_grab(void)
522 {
523     SDL_WM_GrabInput(SDL_GRAB_OFF);
524     SDL_ShowCursor(SDL_ENABLE);
525     grabbed = 0;
526 }
527
528 int  config_get_grab(void)
529 {
530     return grabbed;
531 }
532
533 /*---------------------------------------------------------------------------*/
534
535 int config_cheat(void)
536 {
537     return config_get_d(CONFIG_CHEAT);
538 }
539
540 void config_set_cheat(void)
541 {
542     config_set_d(CONFIG_CHEAT, 1);
543 }
544
545 void config_clr_cheat(void)
546 {
547     config_set_d(CONFIG_CHEAT, 0);
548 }
549
550 /*---------------------------------------------------------------------------*/
551
552 void config_push_persp(float fov, float n, float f)
553 {
554     GLdouble m[4][4];
555
556     GLdouble r = fov / 2 * V_PI / 180;
557     GLdouble s = sin(r);
558     GLdouble c = cos(r) / s;
559
560     GLdouble a = ((GLdouble) option_d[CONFIG_WIDTH] /
561                   (GLdouble) option_d[CONFIG_HEIGHT]);
562
563     glMatrixMode(GL_PROJECTION);
564     {
565         glPushMatrix();
566         glLoadIdentity();
567
568         m[0][0] =  c/a;
569         m[0][1] =  0.0;
570         m[0][2] =  0.0;
571         m[0][3] =  0.0;
572         m[1][0] =  0.0;
573         m[1][1] =    c;
574         m[1][2] =  0.0;
575         m[1][3] =  0.0;
576         m[2][0] =  0.0;
577         m[2][1] =  0.0;
578         m[2][2] = -(f + n) / (f - n);
579         m[2][3] = -1.0;
580         m[3][0] =  0.0;
581         m[3][1] =  0.0;
582         m[3][2] = -2.0 * n * f / (f - n);
583         m[3][3] =  0.0;
584
585         glMultMatrixd(&m[0][0]);
586     }
587     glMatrixMode(GL_MODELVIEW);
588 }
589
590 void config_push_ortho(void)
591 {
592     GLdouble w = (GLdouble) option_d[CONFIG_WIDTH];
593     GLdouble h = (GLdouble) option_d[CONFIG_HEIGHT];
594
595     glMatrixMode(GL_PROJECTION);
596     {
597         glPushMatrix();
598         glLoadIdentity();
599         glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
600     }
601     glMatrixMode(GL_MODELVIEW);
602 }
603
604 void config_pop_matrix(void)
605 {
606     glMatrixMode(GL_PROJECTION);
607     {
608         glPopMatrix();
609     }
610     glMatrixMode(GL_MODELVIEW);
611 }
612
613 void config_clear(void)
614 {
615     if (option_d[CONFIG_REFLECTION])
616         glClear(GL_COLOR_BUFFER_BIT |
617                 GL_DEPTH_BUFFER_BIT |
618                 GL_STENCIL_BUFFER_BIT);
619     else
620         glClear(GL_COLOR_BUFFER_BIT |
621                 GL_DEPTH_BUFFER_BIT);
622 }
623
624 /*---------------------------------------------------------------------------*/