Oops, forgot to enable the stats only on config.
[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 "levels.h"
28 #include "game.h"
29 #include "gui.h"
30 #include "set.h"
31 #include "text.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 /*---------------------------------------------------------------------------*/
76
77 static int loop(void)
78 {
79     SDL_Event e;
80     int d = 1;
81     int c;
82
83     while (d && SDL_PollEvent(&e))
84     {
85         switch (e.type)
86         {
87         case SDL_QUIT:
88             return 0;
89
90         case SDL_MOUSEMOTION:
91             st_point(+e.motion.x,
92                      -e.motion.y + config_get_d(CONFIG_HEIGHT),
93                      +e.motion.xrel,
94                      config_get_d(CONFIG_MOUSE_INVERT)
95                      ? +e.motion.yrel : -e.motion.yrel);
96             break;
97
98         case SDL_MOUSEBUTTONDOWN:
99             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
100             break;
101
102         case SDL_MOUSEBUTTONUP:
103             d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
104             break;
105
106         case SDL_KEYDOWN:
107
108             c = e.key.keysym.sym;
109
110             if (config_tst_d(CONFIG_KEY_FORWARD, c))
111                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
112
113             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
114                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
115
116             else if (config_tst_d(CONFIG_KEY_LEFT, c))
117                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
118
119             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
120                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
121
122             else switch (c)
123             {
124             case SDLK_F10:   shot();                    break;
125             case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
126             case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
127
128             case SDLK_F7:
129                 if (config_cheat())
130                     toggle_wire();
131                 break;
132
133             case SDLK_RETURN:
134                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
135                 break;
136             case SDLK_ESCAPE:
137                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
138                 break;
139
140             default:
141                 if (SDL_EnableUNICODE(-1))
142                     d = st_keybd(e.key.keysym.unicode, 1);
143                 else
144                     d = st_keybd(e.key.keysym.sym, 1);
145             }
146
147             break;
148
149         case SDL_KEYUP:
150
151             c = e.key.keysym.sym;
152
153             if (config_tst_d(CONFIG_KEY_FORWARD, c))
154                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
155
156             else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
157                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
158
159             else if (config_tst_d(CONFIG_KEY_LEFT, c))
160                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
161
162             else if (config_tst_d(CONFIG_KEY_RIGHT, c))
163                 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
164
165             else switch (c)
166             {
167             case SDLK_RETURN:
168                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
169                 break;
170             case SDLK_ESCAPE:
171                 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
172                 break;
173
174             default:
175                 d = st_keybd(e.key.keysym.sym, 0);
176             }
177
178         case SDL_ACTIVEEVENT:
179             if (e.active.state == SDL_APPINPUTFOCUS)
180                 if (e.active.gain == 0 && config_get_grab())
181                     goto_pause();
182             break;
183
184         case SDL_JOYAXISMOTION:
185             st_stick(e.jaxis.axis, e.jaxis.value);
186             break;
187
188         case SDL_JOYBUTTONDOWN:
189             d = st_buttn(e.jbutton.button, 1);
190             break;
191
192         case SDL_JOYBUTTONUP:
193             d = st_buttn(e.jbutton.button, 0);
194             break;
195         }
196     }
197     return d;
198 }
199
200 /*---------------------------------------------------------------------------*/
201
202 static char *data_path = NULL;
203 static char *demo_path = NULL;
204
205 static unsigned int display_info = 0;
206 static unsigned int replay_demo  = 0;
207
208 #define usage \
209     L_(                                                                   \
210         "Usage: %s [options ...]\n"                                       \
211         "Options:\n"                                                      \
212         "  -h, --help                show this usage message.\n"          \
213         "  -v, --version             show version.\n"                     \
214         "  -d, --data <dir>          use 'dir' as game data directory.\n" \
215         "  -r, --replay <file>       play the replay 'file'.\n"           \
216         "  -i, --info                display info about a replay.\n"      \
217     )
218
219 #define argument_error(option) { \
220     fprintf(stderr, L_("Option '%s' requires an argument.\n"),  option); \
221 }
222
223 static void parse_args(int argc, char **argv)
224 {
225     int i;
226
227     /* Scan argument list. */
228
229     for (i = 1; i < argc; i++)
230     {
231         if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help")    == 0)
232         {
233             printf(usage, argv[0]);
234             exit(EXIT_SUCCESS);
235         }
236
237         if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
238         {
239             printf("%s\n", VERSION);
240             exit(EXIT_SUCCESS);
241         }
242
243         if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data")    == 0)
244         {
245             if (i + 1 == argc)
246             {
247                 argument_error(argv[i]);
248                 exit(EXIT_FAILURE);
249             }
250             data_path = argv[++i];
251             continue;
252         }
253
254         if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay")  == 0)
255         {
256             if (i + 1 == argc)
257             {
258                 argument_error(argv[i]);
259                 exit(EXIT_FAILURE);
260             }
261             demo_path = argv[++i];
262             continue;
263         }
264
265         if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info")    == 0)
266         {
267             display_info = 1;
268             continue;
269         }
270     }
271
272     /* Resolve conflicts. */
273
274     if (demo_path)
275         replay_demo = display_info ? 0 : 1;
276     else
277         if (display_info)
278         {
279             /* FIXME, I'm a required option. */
280             fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
281             exit(EXIT_FAILURE);
282         }
283 }
284
285 #undef usage
286 #undef argument_error
287
288 /*---------------------------------------------------------------------------*/
289
290 int main(int argc, char *argv[])
291 {
292     SDL_Joystick *joy = NULL;
293     SDL_Surface *icon;
294
295     int t1, t0;
296
297     lang_init("neverball", CONFIG_LOCALE);
298
299     text_init();
300
301     parse_args(argc, argv);
302
303     if (!config_data_path(data_path, SET_FILE))
304     {
305         fputs(L_("Failure to establish game data directory\n"), stderr);
306         return 1;
307     }
308
309     if (!config_user_path(NULL))
310     {
311         fputs(L_("Failure to establish config directory\n"), stderr);
312         return 1;
313     }
314
315     /* Initialize SDL system and subsystems */
316
317     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
318     {
319         fprintf(stderr, "%s\n", SDL_GetError());
320         return 1;
321     }
322
323     /* Intitialize the configuration */
324
325     config_init();
326     config_load();
327
328     /* Dump replay information and exit. */
329
330     if (display_info)
331     {
332         if (!level_replay(demo_path))
333         {
334             fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
335                     errno ?  strerror(errno) : L_("Not a replay file"));
336             return 1;
337         }
338         demo_replay_dump_info();
339         return 0;
340     }
341
342     /* Initialize the joystick. */
343
344     if (SDL_NumJoysticks() > 0)
345     {
346         joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
347         if (joy)
348             SDL_JoystickEventState(SDL_ENABLE);
349     }
350
351     /* Initialize the audio. */
352
353     audio_init();
354
355     /* Require 16-bit double buffer with 16-bit depth buffer. */
356
357     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
358     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
359     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
360     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
361     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
362
363 #ifndef __APPLE__
364     if ((icon = load_surface("icon/neverball.png")))
365     {
366         SDL_WM_SetIcon(icon, NULL);
367         free(icon->pixels);
368         SDL_FreeSurface(icon);
369     }
370 #endif /* __APPLE__ */
371
372     /* Initialize the video. */
373
374     if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
375                      config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
376     {
377         fprintf(stderr, "%s\n", SDL_GetError());
378         return 1;
379     }
380
381     SDL_WM_SetCaption(TITLE, TITLE);
382
383     init_state(&st_null);
384
385     /* Initialise demo playback. */
386
387     if (replay_demo)
388     {
389         level_replay(demo_path);
390         demo_play_goto(1);
391         goto_state(&st_demo_play);
392     }
393     else
394         goto_state(&st_title);
395
396     /* Run the main game loop. */
397
398     t0 = SDL_GetTicks();
399     while (loop())
400         if ((t1 = SDL_GetTicks()) > t0)
401         {
402             st_timer((t1 - t0) / 1000.f);
403             st_paint();
404             SDL_GL_SwapBuffers();
405
406             t0 = t1;
407
408             if (config_get_d(CONFIG_NICE))
409                 SDL_Delay(1);
410         }
411
412     /* Gracefully close the game */
413
414     if (SDL_JoystickOpened(0))
415         SDL_JoystickClose(joy);
416
417     SDL_Quit();
418
419     config_save();
420
421     text_quit();
422
423     return 0;
424 }
425
426 /*---------------------------------------------------------------------------*/
427