fix translation
[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
33 #include "glext.h"
34 #include "config.h"
35 #include "image.h"
36 #include "audio.h"
37 #include "demo.h"
38 #include "game.h"
39 #include "gui.h"
40 #include "set.h"
41
42 #include "st_conf.h"
43 #include "st_title.h"
44
45 #define TITLE _("Neverball")
46
47 /*---------------------------------------------------------------------------*/
48
49 static void shot(void)
50 {
51     static char filename[MAXSTR];
52     static int  num = 0;
53
54     sprintf(filename, "screen%02d.bmp", num++);
55
56     image_snap(filename);
57 }
58
59 /*---------------------------------------------------------------------------*/
60
61 static void toggle_wire(void)
62 {
63     static int wire = 0;
64
65     if (wire)
66     {
67         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
68         glEnable(GL_TEXTURE_2D);
69         glEnable(GL_LIGHTING);
70         wire = 0;
71     }
72     else
73     {
74         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
75         glDisable(GL_TEXTURE_2D);
76         glDisable(GL_LIGHTING);
77         wire = 1;
78     }
79 }
80
81 /*---------------------------------------------------------------------------*/
82
83 static int loop(void)
84 {
85     SDL_Event e;
86     int d = 1;
87
88     while (d && SDL_PollEvent(&e))
89     {
90         if (e.type == SDL_QUIT)
91             return 0;
92
93         if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
94             config_tgl_pause();
95
96         if (!config_get_pause())
97             switch (e.type)
98             {
99             case SDL_MOUSEMOTION:
100                 st_point(+e.motion.x,
101 #ifdef __APPLE__
102                          +e.motion.y,
103 #else
104                          -e.motion.y + config_get_d(CONFIG_HEIGHT),
105 #endif
106                          +e.motion.xrel,
107                          config_get_d(CONFIG_MOUSE_INVERT)
108                          ? +e.motion.yrel : -e.motion.yrel);
109                 break;
110
111             case SDL_MOUSEBUTTONDOWN:
112                 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
113                 break;
114                 
115             case SDL_MOUSEBUTTONUP:
116                 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
117                 break;
118
119             case SDL_KEYDOWN:
120                 
121                 switch (e.key.keysym.sym)
122                 {
123                 case SDLK_F10:   shot();                    break;
124                 case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
125                 case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
126                 case SDLK_F7:    toggle_wire();             break;
127                 
128                 case SDLK_RETURN:
129                     d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
130                     break;
131                 case SDLK_LEFT:
132                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
133                     break;
134                 case SDLK_RIGHT:
135                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
136                     break;
137                 case SDLK_UP:
138                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
139                     break;
140                 case SDLK_DOWN:
141                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
142                     break;
143                              
144                 default: 
145                     d = st_keybd(e.key.keysym.sym, 1);
146                 }
147                 break;
148
149             case SDL_KEYUP:
150
151                 switch (e.key.keysym.sym)
152                 {
153                 case SDLK_RETURN:
154                     d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
155                     break;
156                 case SDLK_LEFT:
157                 case SDLK_RIGHT:
158                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
159                     break;
160                 case SDLK_DOWN:
161                 case SDLK_UP:
162                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
163                     break;
164
165                 default:
166                     d = st_keybd(e.key.keysym.sym, 0);
167                 }
168
169                 break;
170
171             case SDL_ACTIVEEVENT:
172                 if (e.active.state == SDL_APPINPUTFOCUS)
173                     if (e.active.gain == 0 && config_get_grab())
174                         config_set_pause();
175                 break;
176
177             case SDL_JOYAXISMOTION:
178                 st_stick(e.jaxis.axis, e.jaxis.value);
179                 break;
180
181             case SDL_JOYBUTTONDOWN:
182                 d = st_buttn(e.jbutton.button, 1);
183                 break;
184
185             case SDL_JOYBUTTONUP:
186                 d = st_buttn(e.jbutton.button, 0);
187                 break;
188             }
189     }
190     return d;
191 }
192
193 int main(int argc, char *argv[])
194 {
195     language_init("neverball", CONFIG_LOCALE);
196     
197     if (config_data_path((argc > 1 ? argv[1] : NULL), SET_FILE))
198     {
199         if (config_user_path(NULL))
200         {
201             if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK))
202             {
203                 SDL_Joystick *joy = NULL;
204
205                 config_init();
206                 config_load();
207
208                 /* Initialize the language. */
209                 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
210
211                 /* Initialize the joystick. */
212
213                 if (SDL_NumJoysticks() > 0)
214                 {
215                     joy=SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
216                     if (joy)
217                         SDL_JoystickEventState(SDL_ENABLE);
218                 }
219
220                 /* Initialize the audio. */
221
222                 audio_bind(AUD_MENU,   3, "snd/menu.wav");
223                 audio_bind(AUD_START,  1, "snd/select.ogg");
224                 audio_bind(AUD_READY,  1, "snd/ready.ogg");
225                 audio_bind(AUD_SET,    1, "snd/set.ogg");
226                 audio_bind(AUD_GO,     1, "snd/go.ogg");
227                 audio_bind(AUD_BALL,   2, "snd/ball.ogg");
228                 audio_bind(AUD_BUMP,   3, "snd/bump.ogg");
229                 audio_bind(AUD_COIN,   2, "snd/coin.wav");
230                 audio_bind(AUD_TICK,   4, "snd/tick.ogg");
231                 audio_bind(AUD_TOCK,   4, "snd/tock.ogg");
232                 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
233                 audio_bind(AUD_JUMP,   5, "snd/jump.ogg");
234                 audio_bind(AUD_GOAL,   5, "snd/goal.wav");
235                 audio_bind(AUD_SCORE,  1, "snd/record.ogg");
236                 audio_bind(AUD_FALL,   1, "snd/fall.ogg");
237                 audio_bind(AUD_TIME,   1, "snd/time.ogg");
238                 audio_bind(AUD_OVER,   1, "snd/over.ogg");
239
240                 audio_init();
241
242                 /* Require 16-bit double buffer with 16-bit depth buffer. */
243
244                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
245                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
246                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
247                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
248                 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
249
250                 /* Initialize the video. */
251
252                 if (config_mode(config_get_d(CONFIG_FULLSCREEN),
253                                 config_get_d(CONFIG_WIDTH),
254                                 config_get_d(CONFIG_HEIGHT)))
255                 {
256                     int t1, t0 = SDL_GetTicks();
257
258                     SDL_Surface *icon = IMG_Load(config_data("icon/neverball.png"));
259                     SDL_WM_SetIcon(icon, NULL);
260                     SDL_WM_SetCaption(TITLE, TITLE); 
261
262                     /* Initialize the run state and the title display. */
263
264                     init_state(&st_null);
265                     goto_state(&st_title);
266
267                     /* Run the main game loop. */
268
269                     while (loop())
270                         if ((t1 = SDL_GetTicks()) > t0)
271                         {
272                             if (config_get_pause())
273                             {
274                                 st_paint();
275                                 gui_blank();
276                             }
277                             else
278                             {
279                                 st_timer((t1 - t0) / 1000.f);
280                                 st_paint();
281                             }
282                             SDL_GL_SwapBuffers();
283
284                             t0 = t1;
285
286                             if (config_get_d(CONFIG_NICE))
287                                 SDL_Delay(1);
288                         }
289                 }
290                 else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
291
292                 config_save();
293
294                 if (SDL_JoystickOpened(0))
295                     SDL_JoystickClose(joy);
296
297                 SDL_Quit();
298             }
299             else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
300         }
301         else fprintf(stderr, "Failure to establish config directory\n");
302     }
303     else fprintf(stderr, "Failure to establish game data directory\n");
304
305     return 0;
306 }
307
308 /*---------------------------------------------------------------------------*/
309