2 * Copyright (C) 2003 Robert Kooima
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.
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.
15 /*---------------------------------------------------------------------------*/
18 #include <SDL_image.h>
39 #define TITLE "Neverball"
41 /*---------------------------------------------------------------------------*/
43 static void shot(void)
45 static char filename[MAXSTR];
48 sprintf(filename, "screen%02d.png", num++);
53 /*---------------------------------------------------------------------------*/
55 static void toggle_wire(void)
61 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
62 glEnable(GL_TEXTURE_2D);
63 glEnable(GL_LIGHTING);
68 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
69 glDisable(GL_TEXTURE_2D);
70 glDisable(GL_LIGHTING);
75 static void toggle_fullscreen(void)
79 SDL_GetMouseState(&x, &y);
80 config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
81 config_get_d(CONFIG_HEIGHT));
85 /*---------------------------------------------------------------------------*/
93 while (d && SDL_PollEvent(&e))
100 case SDL_MOUSEMOTION:
101 st_point(+e.motion.x,
102 -e.motion.y + config_get_d(CONFIG_HEIGHT),
104 config_get_d(CONFIG_MOUSE_INVERT)
105 ? +e.motion.yrel : -e.motion.yrel);
108 case SDL_MOUSEBUTTONDOWN:
109 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
112 case SDL_MOUSEBUTTONUP:
113 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
118 c = e.key.keysym.sym;
120 if (config_tst_d(CONFIG_KEY_FORWARD, c))
121 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
123 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
124 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
126 else if (config_tst_d(CONFIG_KEY_LEFT, c))
127 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
129 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
130 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
134 case SDLK_F11: toggle_fullscreen(); break;
135 case SDLK_F10: shot(); break;
136 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
137 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
145 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
148 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
152 if (SDL_EnableUNICODE(-1))
153 d = st_keybd(e.key.keysym.unicode, 1);
155 d = st_keybd(e.key.keysym.sym, 1);
162 c = e.key.keysym.sym;
164 if (config_tst_d(CONFIG_KEY_FORWARD, c))
165 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
167 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
168 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
170 else if (config_tst_d(CONFIG_KEY_LEFT, c))
171 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
173 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
174 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
179 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
182 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
186 d = st_keybd(e.key.keysym.sym, 0);
189 case SDL_ACTIVEEVENT:
190 if (e.active.state == SDL_APPINPUTFOCUS)
191 if (e.active.gain == 0 && config_get_grab())
195 case SDL_JOYAXISMOTION:
196 st_stick(e.jaxis.axis, e.jaxis.value);
199 case SDL_JOYBUTTONDOWN:
200 d = st_buttn(e.jbutton.button, 1);
203 case SDL_JOYBUTTONUP:
204 d = st_buttn(e.jbutton.button, 0);
211 /*---------------------------------------------------------------------------*/
213 static char *data_path = NULL;
214 static char *demo_path = NULL;
216 static unsigned int display_info = 0;
217 static unsigned int replay_demo = 0;
219 #define argument_error(option) { \
220 fprintf(stderr, _("Option '%s' requires an argument.\n"), option); \
223 static void parse_args(int argc, char **argv)
227 const char *usage = _(
228 "Usage: %s [options ...]\n"
230 " -h, --help show this usage message.\n"
231 " -v, --version show version.\n"
232 " -d, --data <dir> use 'dir' as game data directory.\n"
233 " -r, --replay <file> play the replay 'file'.\n"
234 " -i, --info display info about a replay.\n"
237 /* Scan argument list. */
239 for (i = 1; i < argc; i++)
241 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
243 printf(usage, argv[0]);
247 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
249 printf("%s\n", VERSION);
253 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
257 argument_error(argv[i]);
260 data_path = argv[++i];
264 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
268 argument_error(argv[i]);
271 demo_path = argv[++i];
275 if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info") == 0)
282 /* Resolve conflicts. */
285 replay_demo = display_info ? 0 : 1;
289 /* FIXME, I'm a required option. */
290 fputs(_("Option '--info' requires '--replay'.\n"), stderr);
295 #undef argument_error
297 /*---------------------------------------------------------------------------*/
299 int main(int argc, char *argv[])
301 SDL_Joystick *joy = NULL;
306 lang_init("neverball", CONFIG_LOCALE);
308 parse_args(argc, argv);
310 if (!config_data_path(data_path, SET_FILE))
312 fprintf(stderr, _("Failure to establish game data directory\n"));
316 if (!config_user_path(NULL))
318 fprintf(stderr, _("Failure to establish config directory\n"));
322 /* Initialize SDL system and subsystems */
324 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
326 fprintf(stderr, "%s\n", SDL_GetError());
330 /* Intitialize the configuration */
335 /* Dump replay information and exit. */
339 if (!level_replay(demo_path))
341 fprintf(stderr, _("Replay file '%s': %s\n"), demo_path,
342 errno ? strerror(errno) : _("Not a replay file"));
345 demo_replay_dump_info();
349 /* Initialize the joystick. */
351 if (SDL_NumJoysticks() > 0)
353 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
355 SDL_JoystickEventState(SDL_ENABLE);
358 /* Initialize the audio. */
360 audio_bind(AUD_MENU, 3, "snd/menu.wav");
361 audio_bind(AUD_START, 1, _("snd/select.ogg"));
362 audio_bind(AUD_READY, 1, _("snd/ready.ogg"));
363 audio_bind(AUD_SET, 1, _("snd/set.ogg"));
364 audio_bind(AUD_GO, 1, _("snd/go.ogg"));
365 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
366 audio_bind(AUD_BUMPS, 3, "snd/bumplil.ogg");
367 audio_bind(AUD_BUMPM, 3, "snd/bump.ogg");
368 audio_bind(AUD_BUMPL, 3, "snd/bumpbig.ogg");
369 audio_bind(AUD_COIN, 2, "snd/coin.wav");
370 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
371 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
372 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
373 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
374 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
375 audio_bind(AUD_SCORE, 1, _("snd/record.ogg"));
376 audio_bind(AUD_FALL, 1, _("snd/fall.ogg"));
377 audio_bind(AUD_TIME, 1, _("snd/time.ogg"));
378 audio_bind(AUD_OVER, 1, _("snd/over.ogg"));
379 audio_bind(AUD_GROW, 5, "snd/grow.ogg");
380 audio_bind(AUD_SHRINK, 5, "snd/shrink.ogg");
384 /* Require 16-bit double buffer with 16-bit depth buffer. */
386 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
387 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
388 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
389 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
390 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
393 /* Set the WM icon */
395 icon = IMG_Load(config_data("icon/neverball.png"));
399 SDL_WM_SetIcon(icon, NULL);
400 SDL_FreeSurface(icon);
402 #endif /* __APPLE__ */
404 /* Initialize the video. */
406 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
407 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
409 fprintf(stderr, "%s\n", SDL_GetError());
413 SDL_WM_SetCaption(TITLE, TITLE);
415 init_state(&st_null);
417 /* Initialise demo playback. */
421 level_replay(demo_path);
423 goto_state(&st_demo_play);
426 goto_state(&st_title);
428 /* Run the main game loop. */
432 if ((t1 = SDL_GetTicks()) > t0)
434 st_timer((t1 - t0) / 1000.f);
436 SDL_GL_SwapBuffers();
440 if (config_get_d(CONFIG_NICE))
444 /* Gracefully close the game */
446 if (SDL_JoystickOpened(0))
447 SDL_JoystickClose(joy);
456 /*---------------------------------------------------------------------------*/