Removed audio_rate config variable and changed config screen accordingly.
[neverball] / ball / main.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 /*---------------------------------------------------------------------------*/
16
17 #include <SDL.h>
18 #include <SDL_image.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #include "glext.h"
24 #include "config.h"
25 #include "image.h"
26 #include "audio.h"
27 #include "demo.h"
28 #include "levels.h"
29 #include "game.h"
30 #include "gui.h"
31 #include "set.h"
32 #include "text.h"
33
34 #include "st_conf.h"
35 #include "st_title.h"
36 #include "st_demo.h"
37 #include "st_level.h"
38 #include "st_pause.h"
39
40 #define TITLE "Neverball"
41
42 /*---------------------------------------------------------------------------*/
43
44 static void shot(void)
45 {
46     static char filename[MAXSTR];
47     static int  num = 0;
48
49     sprintf(filename, "screen%02d.png", num++);
50
51     image_snap(filename);
52 }
53
54 /*---------------------------------------------------------------------------*/
55
56 static void toggle_wire(void)
57 {
58     static int wire = 0;
59
60     if (wire)
61     {
62         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
63         glEnable(GL_TEXTURE_2D);
64         glEnable(GL_LIGHTING);
65         wire = 0;
66     }
67     else
68     {
69         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
70         glDisable(GL_TEXTURE_2D);
71         glDisable(GL_LIGHTING);
72         wire = 1;
73     }
74 }
75
76 static void toggle_fullscreen(void)
77 {
78     int x, y;
79
80     SDL_GetMouseState(&x, &y);
81     config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
82                 config_get_d(CONFIG_HEIGHT));
83     SDL_WarpMouse(x, y);
84 }
85
86 /*---------------------------------------------------------------------------*/
87
88 static int loop(void)
89 {
90     SDL_Event e;
91     int d = 1;
92     int c;
93
94     while (d && SDL_PollEvent(&e))
95     {
96         switch (e.type)
97         {
98         case SDL_QUIT:
99             return 0;
100
101         case SDL_MOUSEMOTION:
102             st_point(+e.motion.x,
103                      -e.motion.y + config_get_d(CONFIG_HEIGHT),
104                      +e.motion.xrel,
105                      config_get_d(CONFIG_MOUSE_INVERT)
106                      ? +e.motion.yrel : -e.motion.yrel);
107             break;
108
109         case SDL_MOUSEBUTTONDOWN:
110             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
111             break;
112
113         case SDL_MOUSEBUTTONUP:
114             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
115             break;
116
117         case SDL_KEYDOWN:
118
119             c = e.key.keysym.sym;
120
121             if (config_tst_d(CONFIG_KEY_FORWARD, c))
122                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
123
124             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
125                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
126
127             else if (config_tst_d(CONFIG_KEY_LEFT, c))
128                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
129
130             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
131                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
132
133             else switch (c)
134             {
135             case SDLK_F11:   toggle_fullscreen();       break;
136             case SDLK_F10:   shot();                    break;
137             case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
138             case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
139
140             case SDLK_F7:
141                 if (config_cheat())
142                     toggle_wire();
143                 break;
144
145             case SDLK_RETURN:
146                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
147                 break;
148             case SDLK_ESCAPE:
149                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
150                 break;
151
152             default:
153                 if (SDL_EnableUNICODE(-1))
154                     d = st_keybd(e.key.keysym.unicode, 1);
155                 else
156                     d = st_keybd(e.key.keysym.sym, 1);
157             }
158
159             break;
160
161         case SDL_KEYUP:
162
163             c = e.key.keysym.sym;
164
165             if (config_tst_d(CONFIG_KEY_FORWARD, c))
166                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
167
168             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
169                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
170
171             else if (config_tst_d(CONFIG_KEY_LEFT, c))
172                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
173
174             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
175                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
176
177             else switch (c)
178             {
179             case SDLK_RETURN:
180                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
181                 break;
182             case SDLK_ESCAPE:
183                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
184                 break;
185
186             default:
187                 d = st_keybd(e.key.keysym.sym, 0);
188             }
189
190         case SDL_ACTIVEEVENT:
191             if (e.active.state == SDL_APPINPUTFOCUS)
192                 if (e.active.gain == 0 && config_get_grab())
193                     goto_pause();
194             break;
195
196         case SDL_JOYAXISMOTION:
197             st_stick(e.jaxis.axis, e.jaxis.value);
198             break;
199
200         case SDL_JOYBUTTONDOWN:
201             d = st_buttn(e.jbutton.button, 1);
202             break;
203
204         case SDL_JOYBUTTONUP:
205             d = st_buttn(e.jbutton.button, 0);
206             break;
207         }
208     }
209     return d;
210 }
211
212 /*---------------------------------------------------------------------------*/
213
214 static char *data_path = NULL;
215 static char *demo_path = NULL;
216
217 static unsigned int display_info = 0;
218 static unsigned int replay_demo  = 0;
219
220 #define usage \
221     L_(                                                                   \
222         "Usage: %s [options ...]\n"                                       \
223         "Options:\n"                                                      \
224         "  -h, --help                show this usage message.\n"          \
225         "  -v, --version             show version.\n"                     \
226         "  -d, --data <dir>          use 'dir' as game data directory.\n" \
227         "  -r, --replay <file>       play the replay 'file'.\n"           \
228         "  -i, --info                display info about a replay.\n"      \
229     )
230
231 #define argument_error(option) { \
232     fprintf(stderr, L_("Option '%s' requires an argument.\n"),  option); \
233 }
234
235 static void parse_args(int argc, char **argv)
236 {
237     int i;
238
239     /* Scan argument list. */
240
241     for (i = 1; i < argc; i++)
242     {
243         if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help")    == 0)
244         {
245             printf(usage, argv[0]);
246             exit(EXIT_SUCCESS);
247         }
248
249         if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
250         {
251             printf("%s\n", VERSION);
252             exit(EXIT_SUCCESS);
253         }
254
255         if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data")    == 0)
256         {
257             if (i + 1 == argc)
258             {
259                 argument_error(argv[i]);
260                 exit(EXIT_FAILURE);
261             }
262             data_path = argv[++i];
263             continue;
264         }
265
266         if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay")  == 0)
267         {
268             if (i + 1 == argc)
269             {
270                 argument_error(argv[i]);
271                 exit(EXIT_FAILURE);
272             }
273             demo_path = argv[++i];
274             continue;
275         }
276
277         if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info")    == 0)
278         {
279             display_info = 1;
280             continue;
281         }
282     }
283
284     /* Resolve conflicts. */
285
286     if (demo_path)
287         replay_demo = display_info ? 0 : 1;
288     else
289         if (display_info)
290         {
291             /* FIXME, I'm a required option. */
292             fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
293             exit(EXIT_FAILURE);
294         }
295 }
296
297 #undef usage
298 #undef argument_error
299
300 /*---------------------------------------------------------------------------*/
301
302 int main(int argc, char *argv[])
303 {
304     SDL_Joystick *joy = NULL;
305     SDL_Surface *icon;
306
307     int t1, t0;
308
309     lang_init("neverball", CONFIG_LOCALE);
310
311     text_init();
312
313     parse_args(argc, argv);
314
315     if (!config_data_path(data_path, SET_FILE))
316     {
317         fputs(L_("Failure to establish game data directory\n"), stderr);
318         return 1;
319     }
320
321     if (!config_user_path(NULL))
322     {
323         fputs(L_("Failure to establish config directory\n"), stderr);
324         return 1;
325     }
326
327     /* Initialize SDL system and subsystems */
328
329     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
330     {
331         fprintf(stderr, "%s\n", SDL_GetError());
332         return 1;
333     }
334
335     /* Intitialize the configuration */
336
337     config_init();
338     config_load();
339
340     /* Dump replay information and exit. */
341
342     if (display_info)
343     {
344         if (!level_replay(demo_path))
345         {
346             fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
347                     errno ?  strerror(errno) : L_("Not a replay file"));
348             return 1;
349         }
350         demo_replay_dump_info();
351         return 0;
352     }
353
354     /* Initialize the joystick. */
355
356     if (SDL_NumJoysticks() > 0)
357     {
358         joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
359         if (joy)
360             SDL_JoystickEventState(SDL_ENABLE);
361     }
362
363     /* Initialize the audio. */
364
365     audio_init();
366
367     /* Require 16-bit double buffer with 16-bit depth buffer. */
368
369     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
370     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
371     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
372     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
373     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
374
375 #ifndef __APPLE__
376     /* Set the WM icon */
377
378     icon = IMG_Load(config_data("icon/neverball.png"));
379
380     if (icon)
381     {
382         SDL_WM_SetIcon(icon, NULL);
383         SDL_FreeSurface(icon);
384     }
385 #endif /* __APPLE__ */
386
387     /* Initialize the video. */
388
389     if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
390                      config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
391     {
392         fprintf(stderr, "%s\n", SDL_GetError());
393         return 1;
394     }
395
396     SDL_WM_SetCaption(TITLE, TITLE);
397
398     init_state(&st_null);
399
400     /* Initialise demo playback. */
401
402     if (replay_demo)
403     {
404         level_replay(demo_path);
405         demo_play_goto(1);
406         goto_state(&st_demo_play);
407     }
408     else
409         goto_state(&st_title);
410
411     /* Run the main game loop. */
412
413     t0 = SDL_GetTicks();
414     while (loop())
415         if ((t1 = SDL_GetTicks()) > t0)
416         {
417             st_timer((t1 - t0) / 1000.f);
418             st_paint();
419             SDL_GL_SwapBuffers();
420
421             t0 = t1;
422
423             if (config_get_d(CONFIG_NICE))
424                 SDL_Delay(1);
425         }
426
427     /* Gracefully close the game */
428
429     if (SDL_JoystickOpened(0))
430         SDL_JoystickClose(joy);
431
432     SDL_Quit();
433
434     config_save();
435
436     text_quit();
437
438     return 0;
439 }
440
441 /*---------------------------------------------------------------------------*/
442