Fixed a problem with Makefile.mingw where make wasn't rebuilding targets
[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_d(CONFIG_MODE,                 DEFAULT_MODE);
110     config_set_d(CONFIG_CHEAT,                DEFAULT_CHEAT);
111     config_set_s(CONFIG_PLAYER,               DEFAULT_PLAYER);
112     config_set_s(CONFIG_BALL,                 DEFAULT_BALL);
113     config_set_s(CONFIG_BALL_BONUS,           DEFAULT_BALL_BONUS);
114     config_set_s(CONFIG_LANG,                 DEFAULT_LANG);
115 }
116
117 void config_load(void)
118 {
119     FILE *fp;
120
121     if ((fp = fopen(config_user(USER_CONFIG_FILE), "r")))
122     {
123         char buf[MAXSTR];
124         char key[MAXSTR];
125         char val[MAXSTR];
126
127         while (fgets(buf, MAXSTR, fp))
128             if (sscanf(buf, "%s %s", key, val) == 2)
129             {
130                 if      (strcmp(key, "fullscreen")            == 0)
131                     config_set_d(CONFIG_FULLSCREEN,           atoi(val));
132                 else if (strcmp(key, "width")                 == 0)
133                     config_set_d(CONFIG_WIDTH,                atoi(val));
134                 else if (strcmp(key, "height")                == 0)
135                     config_set_d(CONFIG_HEIGHT,               atoi(val));
136                 else if (strcmp(key, "stereo")                == 0)
137                     config_set_d(CONFIG_STEREO,               atoi(val));
138                 else if (strcmp(key, "camera")                == 0)
139                     config_set_d(CONFIG_CAMERA,               atoi(val));
140                 else if (strcmp(key, "textures")              == 0)
141                     config_set_d(CONFIG_TEXTURES,             atoi(val));
142                 else if (strcmp(key, "geometry")              == 0)
143                     config_set_d(CONFIG_GEOMETRY,             atoi(val));
144                 else if (strcmp(key, "reflection")            == 0)
145                     config_set_d(CONFIG_REFLECTION,           atoi(val));
146                 else if (strcmp(key, "multisample")           == 0)
147                     config_set_d(CONFIG_MULTISAMPLE,          atoi(val));
148                 else if (strcmp(key, "background")            == 0)
149                     config_set_d(CONFIG_BACKGROUND,           atoi(val));
150                 else if (strcmp(key, "shadow")                == 0)
151                     config_set_d(CONFIG_SHADOW,               atoi(val));
152                 else if (strcmp(key, "audio_rate")            == 0)
153                     config_set_d(CONFIG_AUDIO_RATE,           atoi(val));
154                 else if (strcmp(key, "audio_buff")            == 0)
155                     config_set_d(CONFIG_AUDIO_BUFF,           atoi(val));
156                 else if (strcmp(key, "mouse_sense")           == 0)
157                     config_set_d(CONFIG_MOUSE_SENSE,          atoi(val));
158                 else if (strcmp(key, "mouse_invert")          == 0)
159                     config_set_d(CONFIG_MOUSE_INVERT,         atoi(val));
160                 else if (strcmp(key, "nice")                  == 0)
161                     config_set_d(CONFIG_NICE,                 atoi(val));
162                 else if (strcmp(key, "fps")                   == 0)
163                     config_set_d(CONFIG_FPS,                  atoi(val));
164                 else if (strcmp(key, "sound_volume")          == 0)
165                     config_set_d(CONFIG_SOUND_VOLUME,         atoi(val));
166                 else if (strcmp(key, "music_volume")          == 0)
167                     config_set_d(CONFIG_MUSIC_VOLUME,         atoi(val));
168                 else if (strcmp(key, "joystick")              == 0)
169                     config_set_d(CONFIG_JOYSTICK,             atoi(val));
170                 else if (strcmp(key, "joystick_device")       == 0)
171                     config_set_d(CONFIG_JOYSTICK_DEVICE,      atoi(val));
172                 else if (strcmp(key, "joystick_axis_x")       == 0)
173                     config_set_d(CONFIG_JOYSTICK_AXIS_X,      atoi(val));
174                 else if (strcmp(key, "joystick_axis_y")       == 0)
175                     config_set_d(CONFIG_JOYSTICK_AXIS_Y,      atoi(val));
176                 else if (strcmp(key, "joystick_button_a")     == 0)
177                     config_set_d(CONFIG_JOYSTICK_BUTTON_A,    atoi(val));
178                 else if (strcmp(key, "joystick_button_b")     == 0)
179                     config_set_d(CONFIG_JOYSTICK_BUTTON_B,    atoi(val));
180                 else if (strcmp(key, "joystick_button_r")     == 0)
181                     config_set_d(CONFIG_JOYSTICK_BUTTON_R,    atoi(val));
182                 else if (strcmp(key, "joystick_button_l")     == 0)
183                     config_set_d(CONFIG_JOYSTICK_BUTTON_L,    atoi(val));
184                 else if (strcmp(key, "joystick_button_exit")  == 0)
185                     config_set_d(CONFIG_JOYSTICK_BUTTON_EXIT, atoi(val));
186                 else if (strcmp(key, "joystick_camera_1")     == 0)
187                     config_set_d(CONFIG_JOYSTICK_CAMERA_1,    atoi(val));
188                 else if (strcmp(key, "joystick_camera_2")     == 0)
189                     config_set_d(CONFIG_JOYSTICK_CAMERA_2,    atoi(val));
190                 else if (strcmp(key, "joystick_camera_3")     == 0)
191                     config_set_d(CONFIG_JOYSTICK_CAMERA_3,    atoi(val));
192                 else if (strcmp(key, "view_fov")              == 0)
193                     config_set_d(CONFIG_VIEW_FOV,             atoi(val));
194                 else if (strcmp(key, "view_dp")               == 0)
195                     config_set_d(CONFIG_VIEW_DP,              atoi(val));
196                 else if (strcmp(key, "view_dc")               == 0)
197                     config_set_d(CONFIG_VIEW_DC,              atoi(val));
198                 else if (strcmp(key, "view_dz")               == 0)
199                     config_set_d(CONFIG_VIEW_DZ,              atoi(val));
200                 else if (strcmp(key, "rotate_fast")           == 0)
201                     config_set_d(CONFIG_ROTATE_FAST,          atoi(val));
202                 else if (strcmp(key, "rotate_slow")           == 0)
203                     config_set_d(CONFIG_ROTATE_SLOW,          atoi(val));
204                 else if (strcmp(key, "mode")                  == 0)
205                     config_set_d(CONFIG_MODE,                 atoi(val));
206                 else if (strcmp(key, "cheat") == 0 && ALLOW_CHEAT)
207                     config_set_d(CONFIG_CHEAT,                atoi(val));
208
209                 else if (strcmp(key, "key_camera_1")  == 0)
210                     config_key(val, CONFIG_KEY_CAMERA_1, DEFAULT_KEY_CAMERA_1);
211                 else if (strcmp(key, "key_camera_2")  == 0)
212                     config_key(val, CONFIG_KEY_CAMERA_2, DEFAULT_KEY_CAMERA_2);
213                 else if (strcmp(key, "key_camera_3")  == 0)
214                     config_key(val, CONFIG_KEY_CAMERA_3, DEFAULT_KEY_CAMERA_3);
215                 else if (strcmp(key, "key_camera_r")  == 0)
216                     config_key(val, CONFIG_KEY_CAMERA_R, DEFAULT_KEY_CAMERA_R);
217                 else if (strcmp(key, "key_camera_l")  == 0)
218                     config_key(val, CONFIG_KEY_CAMERA_L, DEFAULT_KEY_CAMERA_L);
219
220                 else if (strcmp(key, "player")     == 0)
221                     config_set_s(CONFIG_PLAYER,     val);
222                 else if (strcmp(key, "ball")       == 0)
223                     config_set_s(CONFIG_BALL,       val);
224                 else if (strcmp(key, "ball_bonus") == 0)
225                     config_set_s(CONFIG_BALL_BONUS, val);
226                 else if (strcmp(key, "lang")       == 0)
227                     config_set_s(CONFIG_LANG,       val);
228             }
229
230         fclose(fp);
231
232         dirty = 0;
233     }
234 }
235
236 void config_save(void)
237 {
238     FILE *fp;
239
240     if (dirty && (fp = fopen(config_user(USER_CONFIG_FILE), "w")))
241     {
242         fprintf(fp, "fullscreen           %d\n",
243                 option_d[CONFIG_FULLSCREEN]);
244         fprintf(fp, "width                %d\n",
245                 option_d[CONFIG_WIDTH]);
246         fprintf(fp, "height               %d\n",
247                 option_d[CONFIG_HEIGHT]);
248         fprintf(fp, "stereo               %d\n",
249                 option_d[CONFIG_STEREO]);
250         fprintf(fp, "camera               %d\n",
251                 option_d[CONFIG_CAMERA]);
252         fprintf(fp, "textures             %d\n",
253                 option_d[CONFIG_TEXTURES]);
254         fprintf(fp, "geometry             %d\n",
255                 option_d[CONFIG_GEOMETRY]);
256         fprintf(fp, "reflection           %d\n",
257                 option_d[CONFIG_REFLECTION]);
258         fprintf(fp, "multisample          %d\n",
259                 option_d[CONFIG_MULTISAMPLE]);
260         fprintf(fp, "background           %d\n",
261                 option_d[CONFIG_BACKGROUND]);
262         fprintf(fp, "shadow               %d\n",
263                 option_d[CONFIG_SHADOW]);
264         fprintf(fp, "audio_rate           %d\n",
265                 option_d[CONFIG_AUDIO_RATE]);
266         fprintf(fp, "audio_buff           %d\n",
267                 option_d[CONFIG_AUDIO_BUFF]);
268         fprintf(fp, "mouse_sense          %d\n",
269                 option_d[CONFIG_MOUSE_SENSE]);
270         fprintf(fp, "mouse_invert         %d\n",
271                 option_d[CONFIG_MOUSE_INVERT]);
272         fprintf(fp, "nice                 %d\n",
273                 option_d[CONFIG_NICE]);
274         fprintf(fp, "fps                  %d\n",
275                 option_d[CONFIG_FPS]);
276         fprintf(fp, "sound_volume         %d\n",
277                 option_d[CONFIG_SOUND_VOLUME]);
278         fprintf(fp, "music_volume         %d\n",
279                 option_d[CONFIG_MUSIC_VOLUME]);
280         fprintf(fp, "joystick             %d\n",
281                 option_d[CONFIG_JOYSTICK]);
282         fprintf(fp, "joystick_device      %d\n",
283                 option_d[CONFIG_JOYSTICK_DEVICE]);
284         fprintf(fp, "joystick_axis_x      %d\n",
285                 option_d[CONFIG_JOYSTICK_AXIS_X]);
286         fprintf(fp, "joystick_axis_y      %d\n",
287                 option_d[CONFIG_JOYSTICK_AXIS_Y]);
288         fprintf(fp, "joystick_button_a    %d\n",
289                 option_d[CONFIG_JOYSTICK_BUTTON_A]);
290         fprintf(fp, "joystick_button_b    %d\n",
291                 option_d[CONFIG_JOYSTICK_BUTTON_B]);
292         fprintf(fp, "joystick_button_r    %d\n",
293                 option_d[CONFIG_JOYSTICK_BUTTON_R]);
294         fprintf(fp, "joystick_button_l    %d\n",
295                 option_d[CONFIG_JOYSTICK_BUTTON_L]);
296         fprintf(fp, "joystick_button_exit %d\n",
297                 option_d[CONFIG_JOYSTICK_BUTTON_EXIT]);
298         fprintf(fp, "joystick_camera_1    %d\n",
299                 option_d[CONFIG_JOYSTICK_CAMERA_1]);
300         fprintf(fp, "joystick_camera_2    %d\n",
301                 option_d[CONFIG_JOYSTICK_CAMERA_2]);
302         fprintf(fp, "joystick_camera_3    %d\n",
303                 option_d[CONFIG_JOYSTICK_CAMERA_3]);
304         fprintf(fp, "view_fov             %d\n",
305                 option_d[CONFIG_VIEW_FOV]);
306         fprintf(fp, "view_dp              %d\n",
307                 option_d[CONFIG_VIEW_DP]);
308         fprintf(fp, "view_dc              %d\n",
309                 option_d[CONFIG_VIEW_DC]);
310         fprintf(fp, "view_dz              %d\n",
311                 option_d[CONFIG_VIEW_DZ]);
312         fprintf(fp, "rotate_fast          %d\n",
313                 option_d[CONFIG_ROTATE_FAST]);
314         fprintf(fp, "rotate_slow          %d\n",
315                 option_d[CONFIG_ROTATE_SLOW]);
316         fprintf(fp, "mode                 %d\n",
317                 option_d[CONFIG_MODE]);
318
319         if (option_d[CONFIG_CHEAT])
320             fprintf(fp,
321                     "cheat                %d\n",
322                     option_d[CONFIG_CHEAT]);
323
324         fprintf(fp, "key_camera_1         %s\n",
325                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_1]));
326         fprintf(fp, "key_camera_2         %s\n",
327                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_2]));
328         fprintf(fp, "key_camera_3         %s\n",
329                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_3]));
330         fprintf(fp, "key_camera_r         %s\n",
331                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_R]));
332         fprintf(fp, "key_camera_l         %s\n",
333                 SDL_GetKeyName(option_d[CONFIG_KEY_CAMERA_L]));
334
335         fprintf(fp, "player               %s\n", option_s[CONFIG_PLAYER]);
336         fprintf(fp, "ball                 %s\n", option_s[CONFIG_BALL]);
337         fprintf(fp, "ball_bonus           %s\n", option_s[CONFIG_BALL_BONUS]);
338         fprintf(fp, "lang                 %s\n", option_s[CONFIG_LANG]);
339
340         fclose(fp);
341     }
342
343     dirty = 0;
344 }
345
346 /*---------------------------------------------------------------------------*/
347
348 int check_extension(const char *needle)
349 {
350     const GLubyte *haystack, *c;
351
352     /* Search for the given string in the OpenGL extension strings. */
353
354     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
355     {
356         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
357             if (*c != *haystack)
358                 break;
359
360         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
361             return 1;
362     }
363
364     return 0;
365 }
366
367 int config_mode(int f, int w, int h)
368 {
369     int stereo  = config_get_d(CONFIG_STEREO)      ? 1 : 0;
370     int stencil = config_get_d(CONFIG_REFLECTION)  ? 1 : 0;
371     int buffers = config_get_d(CONFIG_MULTISAMPLE) ? 1 : 0;
372     int samples = config_get_d(CONFIG_MULTISAMPLE);
373
374     SDL_GL_SetAttribute(SDL_GL_STEREO,             stereo);
375     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,       stencil);
376     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, buffers);
377     SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
378
379     /* Try to set the currently specified mode. */
380
381     if (SDL_SetVideoMode(w, h, 0, SDL_OPENGL | (f ? SDL_FULLSCREEN : 0)))
382     {
383         config_set_d(CONFIG_FULLSCREEN, f);
384         config_set_d(CONFIG_WIDTH, w);
385         config_set_d(CONFIG_HEIGHT, h);
386
387         glViewport(0, 0, w, h);
388         glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
389
390         glEnable(GL_NORMALIZE);
391         glEnable(GL_CULL_FACE);
392         glEnable(GL_DEPTH_TEST);
393         glEnable(GL_TEXTURE_2D);
394         glEnable(GL_LIGHTING);
395
396         /* If GL supports multisample, and SDL got a multisample buffer... */
397
398 #ifdef GL_ARB_multisample
399         if (check_extension("ARB_multisample"))
400         {
401             SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
402             if (buffers)
403                 glEnable(GL_MULTISAMPLE_ARB);
404         }
405 #endif
406
407         return 1;
408     }
409
410     /* If the mode failed, try it without stereo. */
411
412     else if (stereo)
413     {
414         config_set_d(CONFIG_STEREO, 0);
415         return config_mode(f, w, h);
416     }
417
418     /* If the mode failed, try decreasing the level of multisampling. */
419
420     else if (buffers)
421     {
422         config_set_d(CONFIG_MULTISAMPLE, samples / 2);
423         return config_mode(f, w, h);
424     }
425
426     /* If that mode failed, try it without reflections. */
427
428     else if (stencil)
429     {
430         config_set_d(CONFIG_REFLECTION, 0);
431         return config_mode(f, w, h);
432     }
433
434     /* If THAT mode failed, punt. */
435
436     return 0;
437 }
438
439 /*---------------------------------------------------------------------------*/
440
441 void config_set_d(int i, int d)
442 {
443     option_d[i] = d;
444     dirty = 1;
445 }
446
447 void config_tgl_d(int i)
448 {
449     option_d[i] = (option_d[i] ? 0 : 1);
450     dirty = 1;
451 }
452
453 int config_tst_d(int i, int d)
454 {
455     return (option_d[i] == d) ? 1 : 0;
456 }
457
458 int config_get_d(int i)
459 {
460     return option_d[i];
461 }
462
463 /*---------------------------------------------------------------------------*/
464
465 void config_set_s(int i, const char *src)
466 {
467     int len = (int) strlen(src);
468
469     if (option_s[i])
470         free(option_s[i]);
471
472     if ((option_s[i] = (char *) malloc(len + 1)))
473         strncpy(option_s[i], src, len + 1);
474
475     dirty = 1;
476 }
477
478 void config_get_s(int i, char *dst, int len)
479 {
480     strncpy(dst, option_s[i], len);
481 }
482
483 const char *config_simple_get_s(int i)
484 {
485     return option_s[i];
486 }
487
488 /*---------------------------------------------------------------------------*/
489
490 static int grabbed = 0;
491 static int paused  = 0;
492
493 void config_set_grab(void)
494 {
495     SDL_WarpMouse(config_get_d(CONFIG_WIDTH)  / 2,
496                   config_get_d(CONFIG_HEIGHT) / 2);
497     SDL_WM_GrabInput(SDL_GRAB_ON);
498     SDL_ShowCursor(SDL_DISABLE);
499     grabbed = 1;
500 }
501
502 void config_clr_grab(void)
503 {
504     SDL_WM_GrabInput(SDL_GRAB_OFF);
505     SDL_ShowCursor(SDL_ENABLE);
506     grabbed = 0;
507 }
508
509 int  config_get_grab(void)
510 {
511     return grabbed;
512 }
513
514 int  config_get_pause(void)
515 {
516     return paused;
517 }
518
519 void config_set_pause(void)
520 {
521     Mix_PauseMusic();
522     paused = 1;
523
524     if (grabbed)
525     {
526         SDL_ShowCursor(SDL_ENABLE);
527         SDL_WM_GrabInput(SDL_GRAB_OFF);
528     }
529 }
530
531 void config_clr_pause(void)
532 {
533     Mix_ResumeMusic();
534     paused = 0;
535
536     if (grabbed)
537     {
538         SDL_WM_GrabInput(SDL_GRAB_ON);
539         SDL_ShowCursor(SDL_DISABLE);
540     }
541 }
542
543 void config_tgl_pause(void)
544 {
545     if (paused)
546         config_clr_pause();
547     else
548         config_set_pause();
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 /*---------------------------------------------------------------------------*/