Add a "idle" method to the state structure
[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
21 #include "glext.h"
22 #include "config.h"
23 #include "video.h"
24 #include "image.h"
25 #include "audio.h"
26 #include "demo.h"
27 #include "progress.h"
28 #include "gui.h"
29 #include "set.h"
30 #include "tilt.h"
31 #include "fs.h"
32 #include "common.h"
33 #ifdef __MAEMO__
34 #include "maemo.h"
35 #endif
36
37 #include "st_conf.h"
38 #include "st_title.h"
39 #include "st_demo.h"
40 #include "st_level.h"
41 #include "st_pause.h"
42
43 const char TITLE[] = "Neverball " VERSION;
44 const char ICON[] = "icon/neverball.png";
45
46 /*---------------------------------------------------------------------------*/
47
48 static int shot_pending;
49
50 static void shot_prep(void)
51 {
52     shot_pending = 1;
53 }
54
55 static void shot_take(void)
56 {
57     static char filename[MAXSTR];
58
59     if (shot_pending)
60     {
61         sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
62         image_snap(filename);
63         shot_pending = 0;
64     }
65 }
66
67 /*---------------------------------------------------------------------------*/
68
69 static void toggle_wire(void)
70 {
71 #if !ENABLE_OPENGLES
72     static int wire = 0;
73
74     if (wire)
75     {
76         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
77         glEnable(GL_TEXTURE_2D);
78         glEnable(GL_LIGHTING);
79         wire = 0;
80     }
81     else
82     {
83         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
84         glDisable(GL_TEXTURE_2D);
85         glDisable(GL_LIGHTING);
86         wire = 1;
87     }
88 #endif
89 }
90
91 /*---------------------------------------------------------------------------*/
92
93 static int handle_key_dn(SDL_Event *e)
94 {
95     int d = 1;
96     int c;
97
98     c = e->key.keysym.sym;
99
100     if (config_tst_d(CONFIG_KEY_FORWARD, c))
101         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -1.0f);
102
103     else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
104         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +1.0f);
105
106     else if (config_tst_d(CONFIG_KEY_LEFT, c))
107         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -1.0f);
108
109     else if (config_tst_d(CONFIG_KEY_RIGHT, c))
110         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +1.0f);
111
112     else switch (c)
113     {
114     case SDLK_F10:   shot_prep();               break;
115     case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
116     case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
117
118     case SDLK_F7:
119         if (config_cheat())
120             toggle_wire();
121         break;
122     case SDLK_RETURN:
123         d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
124         break;
125     case SDLK_ESCAPE:
126         d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
127         break;
128
129     default:
130         if (SDL_EnableUNICODE(-1))
131             d = st_keybd(e->key.keysym.unicode, 1);
132         else
133             d = st_keybd(e->key.keysym.sym, 1);
134     }
135
136     return d;
137 }
138
139 static int handle_key_up(SDL_Event *e)
140 {
141     int d = 1;
142     int c;
143
144     c = e->key.keysym.sym;
145
146     if (config_tst_d(CONFIG_KEY_FORWARD, c))
147         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
148
149     else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
150         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
151
152     else if (config_tst_d(CONFIG_KEY_LEFT, c))
153         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
154
155     else if (config_tst_d(CONFIG_KEY_RIGHT, c))
156         st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
157
158     else switch (c)
159     {
160     case SDLK_RETURN:
161         d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
162         break;
163     case SDLK_ESCAPE:
164         d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
165         break;
166
167     default:
168         d = st_keybd(e->key.keysym.sym, 0);
169     }
170
171     return d;
172 }
173
174 static int loop(void)
175 {
176     SDL_Event e;
177     int d = 1;
178
179     st_idle();
180
181     /* Process SDL events. */
182
183     while (d && SDL_PollEvent(&e))
184     {
185         switch (e.type)
186         {
187         case SDL_QUIT:
188             return 0;
189
190         case SDL_MOUSEMOTION:
191             st_point(+e.motion.x,
192                      -e.motion.y + config_get_d(CONFIG_HEIGHT),
193                      +e.motion.xrel,
194                      config_get_d(CONFIG_MOUSE_INVERT)
195                      ? +e.motion.yrel : -e.motion.yrel);
196             break;
197
198         case SDL_MOUSEBUTTONDOWN:
199             d = st_click(e.button.button, 1);
200             break;
201
202         case SDL_MOUSEBUTTONUP:
203             d = st_click(e.button.button, 0);
204             break;
205
206         case SDL_KEYDOWN:
207             d = handle_key_dn(&e);
208             break;
209
210         case SDL_KEYUP:
211             d = handle_key_up(&e);
212             break;
213
214         case SDL_ACTIVEEVENT:
215             if (e.active.state == SDL_APPINPUTFOCUS)
216                 if (e.active.gain == 0 && video_get_grab())
217                     goto_state(&st_pause);
218             break;
219
220         case SDL_JOYAXISMOTION:
221             st_stick(e.jaxis.axis, JOY_VALUE(e.jaxis.value));
222             break;
223
224         case SDL_JOYBUTTONDOWN:
225             d = st_buttn(e.jbutton.button, 1);
226             break;
227
228         case SDL_JOYBUTTONUP:
229             d = st_buttn(e.jbutton.button, 0);
230             break;
231         }
232     }
233
234     /* Process events via the tilt sensor API. */
235
236     if (tilt_stat())
237     {
238         int b;
239         int s;
240
241         st_angle(tilt_get_x(),
242                  tilt_get_z());
243
244         while (tilt_get_button(&b, &s))
245         {
246             const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
247             const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
248             const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
249             const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
250             const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
251             const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
252
253             if (b == L || b == R || b == U || b == D)
254             {
255                 static int pad[4] = { 0, 0, 0, 0 };
256
257                 /* Track the state of the D-pad buttons. */
258
259                 if      (b == L) pad[0] = s;
260                 else if (b == R) pad[1] = s;
261                 else if (b == U) pad[2] = s;
262                 else if (b == D) pad[3] = s;
263
264                 /* Convert D-pad button events into joystick axis motion. */
265
266                 if      (pad[0] && !pad[1]) st_stick(X, -1.0f);
267                 else if (pad[1] && !pad[0]) st_stick(X, +1.0f);
268                 else                        st_stick(X,  0.0f);
269
270                 if      (pad[2] && !pad[3]) st_stick(Y, -1.0f);
271                 else if (pad[3] && !pad[2]) st_stick(Y, +1.0f);
272                 else                        st_stick(Y,  0.0f);
273             }
274             else d = st_buttn(b, s);
275         }
276     }
277
278     return d;
279 }
280
281 /*---------------------------------------------------------------------------*/
282
283 static char *opt_data;
284 static char *opt_replay;
285 static char *opt_level;
286
287 #define opt_usage \
288     L_(                                                                   \
289         "Usage: %s [options ...]\n"                                       \
290         "Options:\n"                                                      \
291         "  -h, --help                show this usage message.\n"          \
292         "  -v, --version             show version.\n"                     \
293         "  -d, --data <dir>          use 'dir' as game data directory.\n" \
294         "  -r, --replay <file>       play the replay 'file'.\n"           \
295         "  -l, --level <file>        load the level 'file'\n"             \
296     )
297
298 #define opt_error(option) \
299     fprintf(stderr, L_("Option '%s' requires an argument.\n"), option)
300
301 static void opt_parse(int argc, char **argv)
302 {
303     int i;
304
305     /* Scan argument list. */
306
307     for (i = 1; i < argc; i++)
308     {
309         if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help")    == 0)
310         {
311             printf(opt_usage, argv[0]);
312             exit(EXIT_SUCCESS);
313         }
314
315         if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
316         {
317             printf("%s\n", VERSION);
318             exit(EXIT_SUCCESS);
319         }
320
321         if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data")    == 0)
322         {
323             if (i + 1 == argc)
324             {
325                 opt_error(argv[i]);
326                 exit(EXIT_FAILURE);
327             }
328             opt_data = argv[++i];
329             continue;
330         }
331
332         if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay")  == 0)
333         {
334             if (i + 1 == argc)
335             {
336                 opt_error(argv[i]);
337                 exit(EXIT_FAILURE);
338             }
339             opt_replay = argv[++i];
340             continue;
341         }
342
343         if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level")  == 0)
344         {
345             if (i + 1 == argc)
346             {
347                 opt_error(argv[i]);
348                 exit(EXIT_FAILURE);
349             }
350             opt_level = argv[++i];
351             continue;
352         }
353
354         /* Perform magic on a single unrecognized argument. */
355
356         if (argc == 2)
357         {
358             size_t len = strlen(argv[i]);
359             int level = 0;
360
361             if (len > 4)
362             {
363                 char *ext = argv[i] + len - 4;
364
365                 if (strcmp(ext, ".map") == 0)
366                     strncpy(ext, ".sol", 4);
367
368                 if (strcmp(ext, ".sol") == 0)
369                     level = 1;
370             }
371
372             if (level)
373                 opt_level = argv[i];
374             else
375                 opt_replay = argv[i];
376
377             break;
378         }
379     }
380 }
381
382 #undef opt_usage
383 #undef opt_error
384
385 /*---------------------------------------------------------------------------*/
386
387 static int is_replay(struct dir_item *item)
388 {
389     return str_ends_with(item->path, ".nbr");
390 }
391
392 static int is_score_file(struct dir_item *item)
393 {
394     return str_starts_with(item->path, "neverballhs-");
395 }
396
397 static void make_dirs_and_migrate(void)
398 {
399     Array items;
400     int i;
401
402     const char *src;
403     char *dst;
404
405     if (fs_mkdir("Replays"))
406     {
407         if ((items = fs_dir_scan("", is_replay)))
408         {
409             for (i = 0; i < array_len(items); i++)
410             {
411                 src = DIR_ITEM_GET(items, i)->path;
412                 dst = concat_string("Replays/", src, NULL);
413                 fs_rename(src, dst);
414                 free(dst);
415             }
416
417             fs_dir_free(items);
418         }
419     }
420
421     if (fs_mkdir("Scores"))
422     {
423         if ((items = fs_dir_scan("", is_score_file)))
424         {
425             for (i = 0; i < array_len(items); i++)
426             {
427                 src = DIR_ITEM_GET(items, i)->path;
428                 dst = concat_string("Scores/",
429                                     src + sizeof ("neverballhs-") - 1,
430                                     ".txt",
431                                     NULL);
432                 fs_rename(src, dst);
433                 free(dst);
434             }
435
436             fs_dir_free(items);
437         }
438     }
439
440     fs_mkdir("Screenshots");
441 }
442
443 /*---------------------------------------------------------------------------*/
444
445 int main(int argc, char *argv[])
446 {
447     SDL_Joystick *joy = NULL;
448     int t1, t0;
449
450     if (!fs_init(argv[0]))
451     {
452         fprintf(stderr, "Failure to initialize virtual file system: %s\n",
453                 fs_error());
454         return 1;
455     }
456
457     lang_init("neverball");
458
459     opt_parse(argc, argv);
460
461     config_paths(opt_data);
462     make_dirs_and_migrate();
463
464     /* Initialize SDL. */
465
466     if (SDL_Init(SDL_INIT_VIDEO |
467                  SDL_INIT_AUDIO |
468 #ifdef __MAEMO__
469                  SDL_INIT_TIMER |
470 #endif
471                  SDL_INIT_JOYSTICK) == -1)
472     {
473         fprintf(stderr, "%s\n", SDL_GetError());
474         return 1;
475     }
476
477     /* Intitialize configuration. */
478
479     config_init();
480     config_load();
481
482     /* Initialize joystick. */
483
484     if (config_get_d(CONFIG_JOYSTICK) && SDL_NumJoysticks() > 0)
485     {
486         joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
487         if (joy)
488             SDL_JoystickEventState(SDL_ENABLE);
489     }
490
491     /* Initialize audio. */
492
493     audio_init();
494     tilt_init();
495
496     /* Initialize video. */
497
498     if (!video_init(TITLE, ICON))
499         return 1;
500
501     init_state(&st_null);
502
503     /* Initialize demo playback or load the level. */
504
505     if (opt_replay &&
506         fs_add_path(dir_name(opt_replay)) &&
507         progress_replay(base_name(opt_replay)))
508     {
509         demo_play_goto(1);
510         goto_state(&st_demo_play);
511     }
512     else if (opt_level)
513     {
514         const char *path = fs_resolve(opt_level);
515         int loaded = 0;
516
517         if (path)
518         {
519             /* HACK: must be around for the duration of the game. */
520             static struct level level;
521
522             if (level_load(path, &level))
523             {
524                 progress_init(MODE_STANDALONE);
525
526                 if (progress_play(&level))
527                 {
528                     goto_state(&st_level);
529                     loaded = 1;
530                 }
531             }
532         }
533         else fprintf(stderr, "%s: file is not in game path\n", opt_level);
534
535         if (!loaded)
536             goto_state(&st_title);
537     }
538     else
539         goto_state(&st_title);
540
541 #ifdef __MAEMO__
542     maemo_init("neverball");
543 #endif
544
545     /* Run the main game loop. */
546
547     t0 = SDL_GetTicks();
548
549     while (loop())
550     {
551         if ((t1 = SDL_GetTicks()) > t0)
552         {
553             /* Step the game state. */
554
555             st_timer(0.001f * (t1 - t0));
556
557             t0 = t1;
558
559             /* Render. */
560
561             st_paint(0.001f * t0);
562             shot_take();
563             video_swap();
564
565             if (config_get_d(CONFIG_NICE))
566                 SDL_Delay(1);
567         }
568     }
569
570     config_save();
571
572     if (joy)
573         SDL_JoystickClose(joy);
574
575     tilt_free();
576 #ifdef __MAEMO__
577     maemo_quit();
578 #endif
579     SDL_Quit();
580
581     return 0;
582 }
583
584 /*---------------------------------------------------------------------------*/
585