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