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>
40 #define TITLE "Neverball"
42 /*---------------------------------------------------------------------------*/
44 static void shot(void)
46 static char filename[MAXSTR];
49 sprintf(filename, "screen%02d.png", num++);
54 /*---------------------------------------------------------------------------*/
56 static void toggle_wire(void)
62 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
63 glEnable(GL_TEXTURE_2D);
64 glEnable(GL_LIGHTING);
69 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
70 glDisable(GL_TEXTURE_2D);
71 glDisable(GL_LIGHTING);
76 static void toggle_fullscreen(void)
80 SDL_GetMouseState(&x, &y);
81 config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
82 config_get_d(CONFIG_HEIGHT));
86 /*---------------------------------------------------------------------------*/
94 while (d && SDL_PollEvent(&e))
101 case SDL_MOUSEMOTION:
102 st_point(+e.motion.x,
103 -e.motion.y + config_get_d(CONFIG_HEIGHT),
105 config_get_d(CONFIG_MOUSE_INVERT)
106 ? +e.motion.yrel : -e.motion.yrel);
109 case SDL_MOUSEBUTTONDOWN:
110 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
113 case SDL_MOUSEBUTTONUP:
114 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
119 c = e.key.keysym.sym;
121 if (config_tst_d(CONFIG_KEY_FORWARD, c))
122 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
124 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
125 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
127 else if (config_tst_d(CONFIG_KEY_LEFT, c))
128 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
130 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
131 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
135 case SDLK_F11: toggle_fullscreen(); break;
136 case SDLK_F10: shot(); break;
137 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
138 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
146 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
149 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
153 if (SDL_EnableUNICODE(-1))
154 d = st_keybd(e.key.keysym.unicode, 1);
156 d = st_keybd(e.key.keysym.sym, 1);
163 c = e.key.keysym.sym;
165 if (config_tst_d(CONFIG_KEY_FORWARD, c))
166 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
168 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
169 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
171 else if (config_tst_d(CONFIG_KEY_LEFT, c))
172 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
174 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
175 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
180 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
183 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
187 d = st_keybd(e.key.keysym.sym, 0);
190 case SDL_ACTIVEEVENT:
191 if (e.active.state == SDL_APPINPUTFOCUS)
192 if (e.active.gain == 0 && config_get_grab())
196 case SDL_JOYAXISMOTION:
197 st_stick(e.jaxis.axis, e.jaxis.value);
200 case SDL_JOYBUTTONDOWN:
201 d = st_buttn(e.jbutton.button, 1);
204 case SDL_JOYBUTTONUP:
205 d = st_buttn(e.jbutton.button, 0);
212 /*---------------------------------------------------------------------------*/
214 static char *data_path = NULL;
215 static char *demo_path = NULL;
217 static unsigned int display_info = 0;
218 static unsigned int replay_demo = 0;
222 "Usage: %s [options ...]\n" \
224 " -h, --help show this usage message.\n" \
225 " -v, --version show version.\n" \
226 " -d, --data <dir> use 'dir' as game data directory.\n" \
227 " -r, --replay <file> play the replay 'file'.\n" \
228 " -i, --info display info about a replay.\n" \
231 #define argument_error(option) { \
232 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option); \
235 static void parse_args(int argc, char **argv)
239 /* Scan argument list. */
241 for (i = 1; i < argc; i++)
243 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
245 printf(usage, argv[0]);
249 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
251 printf("%s\n", VERSION);
255 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
259 argument_error(argv[i]);
262 data_path = argv[++i];
266 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
270 argument_error(argv[i]);
273 demo_path = argv[++i];
277 if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info") == 0)
284 /* Resolve conflicts. */
287 replay_demo = display_info ? 0 : 1;
291 /* FIXME, I'm a required option. */
292 fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
298 #undef argument_error
300 /*---------------------------------------------------------------------------*/
302 int main(int argc, char *argv[])
304 SDL_Joystick *joy = NULL;
309 lang_init("neverball", CONFIG_LOCALE);
313 parse_args(argc, argv);
315 if (!config_data_path(data_path, SET_FILE))
317 fputs(L_("Failure to establish game data directory\n"), stderr);
321 if (!config_user_path(NULL))
323 fputs(L_("Failure to establish config directory\n"), stderr);
327 /* Initialize SDL system and subsystems */
329 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
331 fprintf(stderr, "%s\n", SDL_GetError());
335 /* Intitialize the configuration */
340 /* Dump replay information and exit. */
344 if (!level_replay(demo_path))
346 fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
347 errno ? strerror(errno) : L_("Not a replay file"));
350 demo_replay_dump_info();
354 /* Initialize the joystick. */
356 if (SDL_NumJoysticks() > 0)
358 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
360 SDL_JoystickEventState(SDL_ENABLE);
363 /* Initialize the audio. */
365 audio_bind(AUD_MENU, 3, "snd/menu.wav");
366 audio_bind(AUD_START, 1, _("snd/select.ogg"));
367 audio_bind(AUD_READY, 1, _("snd/ready.ogg"));
368 audio_bind(AUD_SET, 1, _("snd/set.ogg"));
369 audio_bind(AUD_GO, 1, _("snd/go.ogg"));
370 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
371 audio_bind(AUD_BUMPS, 3, "snd/bumplil.ogg");
372 audio_bind(AUD_BUMPM, 3, "snd/bump.ogg");
373 audio_bind(AUD_BUMPL, 3, "snd/bumpbig.ogg");
374 audio_bind(AUD_COIN, 2, "snd/coin.wav");
375 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
376 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
377 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
378 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
379 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
380 audio_bind(AUD_SCORE, 1, _("snd/record.ogg"));
381 audio_bind(AUD_FALL, 1, _("snd/fall.ogg"));
382 audio_bind(AUD_TIME, 1, _("snd/time.ogg"));
383 audio_bind(AUD_OVER, 1, _("snd/over.ogg"));
384 audio_bind(AUD_GROW, 5, "snd/grow.ogg");
385 audio_bind(AUD_SHRINK, 5, "snd/shrink.ogg");
389 /* Require 16-bit double buffer with 16-bit depth buffer. */
391 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
392 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
393 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
394 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
395 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
398 /* Set the WM icon */
400 icon = IMG_Load(config_data("icon/neverball.png"));
404 SDL_WM_SetIcon(icon, NULL);
405 SDL_FreeSurface(icon);
407 #endif /* __APPLE__ */
409 /* Initialize the video. */
411 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
412 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
414 fprintf(stderr, "%s\n", SDL_GetError());
418 SDL_WM_SetCaption(TITLE, TITLE);
420 init_state(&st_null);
422 /* Initialise demo playback. */
426 level_replay(demo_path);
428 goto_state(&st_demo_play);
431 goto_state(&st_title);
433 /* Run the main game loop. */
437 if ((t1 = SDL_GetTicks()) > t0)
439 st_timer((t1 - t0) / 1000.f);
441 SDL_GL_SwapBuffers();
445 if (config_get_d(CONFIG_NICE))
449 /* Gracefully close the game */
451 if (SDL_JoystickOpened(0))
452 SDL_JoystickClose(joy);
463 /*---------------------------------------------------------------------------*/