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