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