fa62cc67446a504d179942ccaf0b05e2a790286f
[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 <SDL_mixer.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <math.h>
22
23 #include "config.h"
24 #include "glext.h"
25 #include "vec3.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(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_BACKGROUND,           DEFAULT_BACKGROUND);
77     config_set_d(CONFIG_SHADOW,               DEFAULT_SHADOW);
78     config_set_d(CONFIG_AUDIO_RATE,           DEFAULT_AUDIO_RATE);
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_s(CONFIG_BALL_BONUS,           DEFAULT_BALL_BONUS);
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, "background")            == 0)
152                     config_set_d(CONFIG_BACKGROUND,           atoi(val));
153                 else if (strcmp(key, "shadow")                == 0)
154                     config_set_d(CONFIG_SHADOW,               atoi(val));
155                 else if (strcmp(key, "audio_rate")            == 0)
156                     config_set_d(CONFIG_AUDIO_RATE,           atoi(val));
157                 else if (strcmp(key, "audio_buff")            == 0)
158                     config_set_d(CONFIG_AUDIO_BUFF,           atoi(val));
159                 else if (strcmp(key, "mouse_sense")           == 0)
160                     config_set_d(CONFIG_MOUSE_SENSE,          atoi(val));
161                 else if (strcmp(key, "mouse_invert")          == 0)
162                     config_set_d(CONFIG_MOUSE_INVERT,         atoi(val));
163                 else if (strcmp(key, "nice")                  == 0)
164                     config_set_d(CONFIG_NICE,                 atoi(val));
165                 else if (strcmp(key, "fps")                   == 0)
166                     config_set_d(CONFIG_FPS,                  atoi(val));
167                 else if (strcmp(key, "sound_volume")          == 0)
168                     config_set_d(CONFIG_SOUND_VOLUME,         atoi(val));
169                 else if (strcmp(key, "music_volume")          == 0)
170                     config_set_d(CONFIG_MUSIC_VOLUME,         atoi(val));
171                 else if (strcmp(key, "joystick")              == 0)
172                     config_set_d(CONFIG_JOYSTICK,             atoi(val));
173                 else if (strcmp(key, "joystick_device")       == 0)
174                     config_set_d(CONFIG_JOYSTICK_DEVICE,      atoi(val));
175                 else if (strcmp(key, "joystick_axis_x")       == 0)
176                     config_set_d(CONFIG_JOYSTICK_AXIS_X,      atoi(val));
177                 else if (strcmp(key, "joystick_axis_y")       == 0)
178                     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      atoi(val));
179                 else if (strcmp(key, "joystick_button_a")     == 0)
180                     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    atoi(val));
181                 else if (strcmp(key, "joystick_button_b")     == 0)
182                     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    atoi(val));
183                 else if (strcmp(key, "joystick_button_r")     == 0)
184                     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    atoi(val));
185                 else if (strcmp(key, "joystick_button_l")     == 0)
186                     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    atoi(val));
187                 else if (strcmp(key, "joystick_button_exit")  == 0)
188                     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, atoi(val));
189                 else if (strcmp(key, "joystick_camera_1")     == 0)
190                     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    atoi(val));
191                 else if (strcmp(key, "joystick_camera_2")     == 0)
192                     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    atoi(val));
193                 else if (strcmp(key, "joystick_camera_3")     == 0)
194                     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    atoi(val));
195                 else if (strcmp(key, "view_fov")              == 0)
196                     config_set_d(CONFIG_VIEW_FOV,             atoi(val));
197                 else if (strcmp(key, "view_dp")               == 0)
198                     config_set_d(CONFIG_VIEW_DP,              atoi(val));
199                 else if (strcmp(key, "view_dc")               == 0)
200                     config_set_d(CONFIG_VIEW_DC,              atoi(val));
201                 else if (strcmp(key, "view_dz")               == 0)
202                     config_set_d(CONFIG_VIEW_DZ,              atoi(val));
203                 else if (strcmp(key, "rotate_fast")           == 0)
204                     config_set_d(CONFIG_ROTATE_FAST,          atoi(val));
205                 else if (strcmp(key, "rotate_slow")           == 0)
206                     config_set_d(CONFIG_ROTATE_SLOW,          atoi(val));
207
208                 else if (strcmp(key, "key_forward")  == 0)
209                     config_key(val, CONFIG_KEY_FORWARD, DEFAULT_KEY_FORWARD);
210                 else if (strcmp(key, "key_backward")  == 0)
211                     config_key(val, CONFIG_KEY_BACKWARD, DEFAULT_KEY_BACKWARD);
212                 else if (strcmp(key, "key_left")  == 0)
213                     config_key(val, CONFIG_KEY_LEFT, DEFAULT_KEY_LEFT);
214                 else if (strcmp(key, "key_right")  == 0)
215                     config_key(val, CONFIG_KEY_RIGHT, DEFAULT_KEY_RIGHT);
216
217                 else if (strcmp(key, "key_camera_1")  == 0)
218                     config_key(val, CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1);
219                 else if (strcmp(key, "key_camera_2")  == 0)
220                     config_key(val, CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2);
221                 else if (strcmp(key, "key_camera_3")  == 0)
222                     config_key(val, CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3);
223                 else if (strcmp(key, "key_camera_r")  == 0)
224                     config_key(val, CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R);
225                 else if (strcmp(key, "key_camera_l")  == 0)
226                     config_key(val, CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L);
227
228                 else if (strcmp(key, "key_pause")  == 0)
229                     config_key(val, CONFIG_KEY_PAUSE,    DEFAULT_KEY_PAUSE);
230                 else if (strcmp(key, "key_restart")  == 0)
231                     config_key(val, CONFIG_KEY_RESTART,    DEFAULT_KEY_RESTART);
232
233                 else if (strcmp(key, "player")     == 0)
234                     config_set_s(CONFIG_PLAYER,     val);
235                 else if (strcmp(key, "ball")       == 0)
236                     config_set_s(CONFIG_BALL,       val);
237                 else if (strcmp(key, "ball_bonus") == 0)
238                     config_set_s(CONFIG_BALL_BONUS, val);
239             }
240
241         fclose(fp);
242
243         dirty = 0;
244     }
245 }
246
247 void config_save(void)
248 {
249     FILE *fp;
250
251     if (dirty && (fp = fopen(config_user(USER_CONFIG_FILE), "w")))
252     {
253         fprintf(fp, "fullscreen           %d\n",
254                 option_d[CONFIG_FULLSCREEN]);
255         fprintf(fp, "width                %d\n",
256                 option_d[CONFIG_WIDTH]);
257         fprintf(fp, "height               %d\n",
258                 option_d[CONFIG_HEIGHT]);
259         fprintf(fp, "stereo               %d\n",
260                 option_d[CONFIG_STEREO]);
261         fprintf(fp, "camera               %d\n",
262                 option_d[CONFIG_CAMERA]);
263         fprintf(fp, "textures             %d\n",
264                 option_d[CONFIG_TEXTURES]);
265         fprintf(fp, "geometry             %d\n",
266                 option_d[CONFIG_GEOMETRY]);
267         fprintf(fp, "reflection           %d\n",
268                 option_d[CONFIG_REFLECTION]);
269         fprintf(fp, "multisample          %d\n",
270                 option_d[CONFIG_MULTISAMPLE]);
271         fprintf(fp, "background           %d\n",
272                 option_d[CONFIG_BACKGROUND]);
273         fprintf(fp, "shadow               %d\n",
274                 option_d[CONFIG_SHADOW]);
275         fprintf(fp, "audio_rate           %d\n",
276                 option_d[CONFIG_AUDIO_RATE]);
277         fprintf(fp, "audio_buff           %d\n",
278                 option_d[CONFIG_AUDIO_BUFF]);
279         fprintf(fp, "mouse_sense          %d\n",
280                 option_d[CONFIG_MOUSE_SENSE]);
281         fprintf(fp, "mouse_invert         %d\n",
282                 option_d[CONFIG_MOUSE_INVERT]);
283         fprintf(fp, "nice                 %d\n",
284                 option_d[CONFIG_NICE]);
285         fprintf(fp, "fps                  %d\n",
286                 option_d[CONFIG_FPS]);
287         fprintf(fp, "sound_volume         %d\n",
288                 option_d[CONFIG_SOUND_VOLUME]);
289         fprintf(fp, "music_volume         %d\n",
290                 option_d[CONFIG_MUSIC_VOLUME]);
291         fprintf(fp, "joystick             %d\n",
292                 option_d[CONFIG_JOYSTICK]);
293         fprintf(fp, "joystick_device      %d\n",
294                 option_d[CONFIG_JOYSTICK_DEVICE]);
295         fprintf(fp, "joystick_axis_x      %d\n",
296                 option_d[CONFIG_JOYSTICK_AXIS_X]);
297         fprintf(fp, "joystick_axis_y      %d\n",
298                 option_d[CONFIG_JOYSTICK_AXIS_Y]);
299         fprintf(fp, "joystick_button_a    %d\n",
300                 option_d[CONFIG_JOYSTICK_BUTTON_A]);
301         fprintf(fp, "joystick_button_b    %d\n",
302                 option_d[CONFIG_JOYSTICK_BUTTON_B]);
303         fprintf(fp, "joystick_button_r    %d\n",
304                 option_d[CONFIG_JOYSTICK_BUTTON_R]);
305         fprintf(fp, "joystick_button_l    %d\n",
306                 option_d[CONFIG_JOYSTICK_BUTTON_L]);
307         fprintf(fp, "joystick_button_exit %d\n",
308                 option_d[CONFIG_JOYSTICK_BUTTON_EXIT]);
309         fprintf(fp, "joystick_camera_1    %d\n",
310                 option_d[CONFIG_JOYSTICK_CAMERA_1]);
311         fprintf(fp, "joystick_camera_2    %d\n",
312                 option_d[CONFIG_JOYSTICK_CAMERA_2]);
313         fprintf(fp, "joystick_camera_3    %d\n",
314                 option_d[CONFIG_JOYSTICK_CAMERA_3]);
315         fprintf(fp, "view_fov             %d\n",
316                 option_d[CONFIG_VIEW_FOV]);
317         fprintf(fp, "view_dp              %d\n",
318                 option_d[CONFIG_VIEW_DP]);
319         fprintf(fp, "view_dc              %d\n",
320                 option_d[CONFIG_VIEW_DC]);
321         fprintf(fp, "view_dz              %d\n",
322                 option_d[CONFIG_VIEW_DZ]);
323         fprintf(fp, "rotate_fast          %d\n",
324                 option_d[CONFIG_ROTATE_FAST]);
325         fprintf(fp, "rotate_slow          %d\n",
326                 option_d[CONFIG_ROTATE_SLOW]);
327
328         fprintf(fp, "key_forward          %s\n",
329                 SDL_GetKeyName(option_d[CONFIG_KEY_FORWARD]));
330         fprintf(fp, "key_backward         %s\n",
331                 SDL_GetKeyName(option_d[CONFIG_KEY_BACKWARD]));
332         fprintf(fp, "key_left             %s\n",
333                 SDL_GetKeyName(option_d[CONFIG_KEY_LEFT]));
334         fprintf(fp, "key_right            %s\n",
335                 SDL_GetKeyName(option_d[CONFIG_KEY_RIGHT]));
336
337         fprintf(fp, "key_camera_1         %s\n",
338                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_1]));
339         fprintf(fp, "key_camera_2         %s\n",
340                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_2]));
341         fprintf(fp, "key_camera_3         %s\n",
342                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_3]));
343         fprintf(fp, "key_camera_r         %s\n",
344                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_R]));
345         fprintf(fp, "key_camera_l         %s\n",
346                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_L]));
347
348         fprintf(fp, "key_pause            %s\n",
349                 SDL_GetKeyName(option_d[CONFIG_KEY_PAUSE]));
350         fprintf(fp, "key_restart          %s\n",
351                 SDL_GetKeyName(option_d[CONFIG_KEY_RESTART]));
352
353         fprintf(fp, "player               %s\n", option_s[CONFIG_PLAYER]);
354         fprintf(fp, "ball                 %s\n", option_s[CONFIG_BALL]);
355         fprintf(fp, "ball_bonus           %s\n", option_s[CONFIG_BALL_BONUS]);
356
357         fclose(fp);
358     }
359
360     dirty = 0;
361 }
362
363 /*---------------------------------------------------------------------------*/
364
365 int check_extension(const char *needle)
366 {
367     const GLubyte *haystack, *c;
368
369     /* Search for the given string in the OpenGL extension strings. */
370
371     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
372     {
373         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
374             if (*c != *haystack)
375                 break;
376
377         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
378             return 1;
379     }
380
381     return 0;
382 }
383
384 int config_mode(int f, int w, int h)
385 {
386     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
387     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
388     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
389     int samples = config_get_d(CONFIG_MULTISAMPLE);
390
391     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
392     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
393     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
394     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
395
396     /* Try to set the currently specified mode. */
397
398     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
399     {
400         config_set_d(CONFIG_FULLSCREEN, f);
401         config_set_d(CONFIG_WIDTH, w);
402         config_set_d(CONFIG_HEIGHT, h);
403
404         glViewport(0, 0, w, h);
405         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
406
407         glEnable(GL_NORMALIZE);
408         glEnable(GL_CULL_FACE);
409         glEnable(GL_DEPTH_TEST);
410         glEnable(GL_TEXTURE_2D);
411         glEnable(GL_LIGHTING);
412
413         /* If GL supports multisample, and SDL got a multisample buffer... */
414
415 #ifdef GL_ARB_multisample
416         if (check_extension("ARB_multisample"))
417         {
418             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
419             if (buffers)
420                 glEnable(GL_MULTISAMPLE_ARB);
421         }
422 #endif
423
424         return 1;
425     }
426
427     /* If the mode failed, try it without stereo. */
428
429     else if (stereo)
430     {
431         config_set_d(CONFIG_STEREO, 0);
432         return config_mode(f, w, h);
433     }
434
435     /* If the mode failed, try decreasing the level of multisampling. */
436
437     else if (buffers)
438     {
439         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
440         return config_mode(f, w, h);
441     }
442
443     /* If that mode failed, try it without reflections. */
444
445     else if (stencil)
446     {
447         config_set_d(CONFIG_REFLECTION, 0);
448         return config_mode(f, w, h);
449     }
450
451     /* If THAT mode failed, punt. */
452
453     return 0;
454 }
455
456 /*---------------------------------------------------------------------------*/
457
458 void config_set_d(int i, int d)
459 {
460     option_d[i] = d;
461     dirty = 1;
462 }
463
464 void config_tgl_d(int i)
465 {
466     option_d[i] = (option_d[i] ? 0 : 1);
467     dirty = 1;
468 }
469
470 int config_tst_d(int i, int d)
471 {
472     return (option_d[i] == d) ? 1 : 0;
473 }
474
475 int config_get_d(int i)
476 {
477     return option_d[i];
478 }
479
480 /*---------------------------------------------------------------------------*/
481
482 void config_set_s(int i, const char *src)
483 {
484     int len = (int) strlen(src);
485
486     if (option_s[i])
487         free(option_s[i]);
488
489     if ((option_s[i] = (char *) malloc(len + 1)))
490         strncpy(option_s[i], src, len + 1);
491
492     dirty = 1;
493 }
494
495 void config_get_s(int i, char *dst, int len)
496 {
497     strncpy(dst, option_s[i], len);
498 }
499
500 /*---------------------------------------------------------------------------*/
501
502 static int grabbed = 0;
503
504 void config_set_grab(int w)
505 {
506     if (w)
507         SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
508                       config_get_d(CONFIG_HEIGHT) / 2);
509     SDL_WM_GrabInput(SDL_GRAB_ON);
510     SDL_ShowCursor(SDL_DISABLE);
511     grabbed = 1;
512 }
513
514 void config_clr_grab(void)
515 {
516     SDL_WM_GrabInput(SDL_GRAB_OFF);
517     SDL_ShowCursor(SDL_ENABLE);
518     grabbed = 0;
519 }
520
521 int  config_get_grab(void)
522 {
523     return grabbed;
524 }
525
526 /*---------------------------------------------------------------------------*/
527
528 int config_cheat(void)
529 {
530 #if CHEATER
531     return strcmp(option_s[CONFIG_PLAYER], "CHEATER") == 0;
532 #else
533     return 0;
534 #endif
535 }
536
537 /*---------------------------------------------------------------------------*/
538
539 void config_push_persp(float fov, float n, float f)
540 {
541     GLdouble m[4][4];
542
543     GLdouble r = fov / 2 * V_PI / 180;
544     GLdouble s = sin(r);
545     GLdouble c = cos(r) / s;
546
547     GLdouble a = ((GLdouble) option_d[CONFIG_WIDTH] /
548                   (GLdouble) option_d[CONFIG_HEIGHT]);
549
550     glMatrixMode(GL_PROJECTION);
551     {
552         glPushMatrix();
553         glLoadIdentity();
554
555         m[0][0] =  c/a;
556         m[0][1] =  0.0;
557         m[0][2] =  0.0;
558         m[0][3] =  0.0;
559         m[1][0] =  0.0;
560         m[1][1] =    c;
561         m[1][2] =  0.0;
562         m[1][3] =  0.0;
563         m[2][0] =  0.0;
564         m[2][1] =  0.0;
565         m[2][2] = -(f + n) / (f - n);
566         m[2][3] = -1.0;
567         m[3][0] =  0.0;
568         m[3][1] =  0.0;
569         m[3][2] = -2.0 * n * f / (f - n);
570         m[3][3] =  0.0;
571
572         glMultMatrixd(&m[0][0]);
573     }
574     glMatrixMode(GL_MODELVIEW);
575 }
576
577 void config_push_ortho(void)
578 {
579     GLdouble w = (GLdouble) option_d[CONFIG_WIDTH];
580     GLdouble h = (GLdouble) option_d[CONFIG_HEIGHT];
581
582     glMatrixMode(GL_PROJECTION);
583     {
584         glPushMatrix();
585         glLoadIdentity();
586         glOrtho(0.0, w, 0.0, h, -1.0, +1.0);
587     }
588     glMatrixMode(GL_MODELVIEW);
589 }
590
591 void config_pop_matrix(void)
592 {
593     glMatrixMode(GL_PROJECTION);
594     {
595         glPopMatrix();
596     }
597     glMatrixMode(GL_MODELVIEW);
598 }
599
600 void config_clear(void)
601 {
602     if (option_d[CONFIG_REFLECTION])
603         glClear(GL_COLOR_BUFFER_BIT |
604                 GL_DEPTH_BUFFER_BIT |
605                 GL_STENCIL_BUFFER_BIT);
606     else
607         glClear(GL_COLOR_BUFFER_BIT |
608                 GL_DEPTH_BUFFER_BIT);
609 }
610
611 /*---------------------------------------------------------------------------*/