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