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