Turn initial mode setting and WM setup into video_init
[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 "video.h"
25 #include "image.h"
26 #include "audio.h"
27 #include "demo.h"
28 #include "progress.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 " VERSION
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, 1);
101             break;
102
103         case SDL_MOUSEBUTTONUP:
104             d = st_click(e.button.button, 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 && video_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     int t1, t0, uniform;
340
341     lang_init("neverball", CONFIG_LOCALE);
342
343     text_init();
344
345     parse_args(argc, argv);
346
347     if (!config_data_path(data_path, SET_FILE))
348     {
349         fputs(L_("Failure to establish game data directory\n"), stderr);
350         return 1;
351     }
352
353     if (!config_user_path(NULL))
354     {
355         fputs(L_("Failure to establish config directory\n"), stderr);
356         return 1;
357     }
358
359     /* Initialize SDL system and subsystems */
360
361     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
362     {
363         fprintf(stderr, "%s\n", SDL_GetError());
364         return 1;
365     }
366
367     /* Intitialize the configuration */
368
369     config_init();
370     config_load();
371
372     /* Dump replay information and exit. */
373
374     if (display_info)
375     {
376         if (!progress_replay(demo_path))
377         {
378             fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
379                     errno ?  strerror(errno) : L_("Not a replay file"));
380             return 1;
381         }
382         demo_replay_dump_info();
383         return 0;
384     }
385
386     /* Initialize the joystick. */
387
388     if (SDL_NumJoysticks() > 0)
389     {
390         joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
391         if (joy)
392             SDL_JoystickEventState(SDL_ENABLE);
393     }
394
395     /* Initialize the audio. */
396
397     audio_init();
398     tilt_init();
399
400     /* Initialize the video. */
401
402     if (!video_init(TITLE, "icon/neverball.png"))
403         return 1;
404
405     init_state(&st_null);
406
407     /* Initialise demo playback. */
408
409     if (replay_demo && progress_replay(demo_path))
410     {
411         demo_play_goto(1);
412         goto_state(&st_demo_play);
413     }
414     else
415         goto_state(&st_title);
416
417     /* Run the main game loop. */
418
419     uniform = config_get_d(CONFIG_UNIFORM);
420     t0 = SDL_GetTicks();
421
422     while (loop())
423     {
424         t1 = SDL_GetTicks();
425
426         if (uniform)
427         {
428             /* Step the game uniformly, as configured. */
429
430             int u;
431
432             for (u = 0; u < abs(uniform); ++u)
433             {
434                 st_timer(DT);
435                 t0 += (int) (DT * 1000);
436             }
437         }
438         else
439         {
440             /* Step the game state at least up to the current time. */
441
442             while (t1 > t0)
443             {
444                 st_timer(DT);
445                 t0 += (int) (DT * 1000);
446             }
447         }
448
449         /* Render. */
450
451         st_paint(0.001f * t0);
452         video_swap();
453
454         if (uniform < 0)
455             shot();
456
457         if (config_get_d(CONFIG_NICE))
458             SDL_Delay(1);
459     }
460
461     /* Gracefully close the game */
462
463     if (SDL_JoystickOpened(0))
464         SDL_JoystickClose(joy);
465
466     tilt_free();
467     SDL_Quit();
468
469     config_save();
470
471     text_quit();
472
473     return 0;
474 }
475
476 /*---------------------------------------------------------------------------*/
477