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 #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")
26 /*---------------------------------------------------------------------------*/
29 #include <SDL_image.h>
50 #define TITLE "Neverball"
52 /*---------------------------------------------------------------------------*/
54 static void shot(void)
56 static char filename[MAXSTR];
59 sprintf(filename, "screen%02d.png", num++);
64 /*---------------------------------------------------------------------------*/
66 static void toggle_wire(void)
72 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
73 glEnable(GL_TEXTURE_2D);
74 glEnable(GL_LIGHTING);
79 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
80 glDisable(GL_TEXTURE_2D);
81 glDisable(GL_LIGHTING);
86 static void toggle_fullscreen(void)
90 SDL_GetMouseState(&x, &y);
91 config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
92 config_get_d(CONFIG_HEIGHT));
96 /*---------------------------------------------------------------------------*/
104 while (d && SDL_PollEvent(&e))
111 case SDL_MOUSEMOTION:
112 st_point(+e.motion.x,
113 -e.motion.y + config_get_d(CONFIG_HEIGHT),
115 config_get_d(CONFIG_MOUSE_INVERT)
116 ? +e.motion.yrel : -e.motion.yrel);
119 case SDL_MOUSEBUTTONDOWN:
120 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
123 case SDL_MOUSEBUTTONUP:
124 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
129 c = e.key.keysym.sym;
131 if (config_tst_d(CONFIG_KEY_FORWARD, c))
132 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
134 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
135 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
137 else if (config_tst_d(CONFIG_KEY_LEFT, c))
138 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
140 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
141 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
145 case SDLK_F11: toggle_fullscreen(); break;
146 case SDLK_F10: shot(); break;
147 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
148 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
151 if (config_get_cheat())
156 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
159 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
163 if (SDL_EnableUNICODE(-1))
164 d = st_keybd(e.key.keysym.unicode, 1);
166 d = st_keybd(e.key.keysym.sym, 1);
173 c = e.key.keysym.sym;
175 if (config_tst_d(CONFIG_KEY_FORWARD, c))
176 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
178 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
179 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
181 else if (config_tst_d(CONFIG_KEY_LEFT, c))
182 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
184 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
185 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
190 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
193 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
197 d = st_keybd(e.key.keysym.sym, 0);
200 case SDL_ACTIVEEVENT:
201 if (e.active.state == SDL_APPINPUTFOCUS)
202 if (e.active.gain == 0 && config_get_grab())
206 case SDL_JOYAXISMOTION:
207 st_stick(e.jaxis.axis, e.jaxis.value);
210 case SDL_JOYBUTTONDOWN:
211 d = st_buttn(e.jbutton.button, 1);
214 case SDL_JOYBUTTONUP:
215 d = st_buttn(e.jbutton.button, 0);
222 /*---------------------------------------------------------------------------*/
226 static char *data_path = NULL;
227 static char *replay_path = NULL;
228 static int display_info = 0;
230 /* Option handling */
232 static void parse_args(int argc, char **argv)
234 char *exec = *(argv++);
237 const char *usage = _(
238 "Usage: %s [options ...]\n"
239 "-r, --replay file play the replay 'file'.\n"
240 "-i, --info display info about a replay.\n"
241 " --data dir use 'dir' as game data directory.\n"
242 "-v, --version show version.\n"
243 "-h, -?, --help show this usage message.\n"
246 # define CASE(x) (strcmp(*argv, (x)) == 0) /* Check current option */
247 # define MAND !(missing = (argv[1] == NULL)) /* Argument is mandatory */
249 while (*argv != NULL)
252 if (CASE("-h") || CASE("-?") || CASE("--help"))
257 else if (CASE("-v") || CASE("--version"))
259 printf("%s\n", VERSION);
262 else if (CASE("--data") && MAND)
263 data_path = *(++argv);
264 else if ((CASE("-r") || CASE("--replay")) && MAND)
265 replay_path = *(++argv);
266 else if ((CASE("-i") || CASE("--info")))
270 fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
271 fprintf(stderr, usage, exec);
276 fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
278 fprintf(stderr, usage, exec);
290 int main(int argc, char *argv[])
292 SDL_Joystick *joy = NULL;
297 language_init("neverball", CONFIG_LOCALE);
299 parse_args(argc, argv);
301 if (!config_data_path(data_path, SET_FILE))
303 fprintf(stderr, _("Failure to establish game data directory\n"));
307 if (!config_user_path(NULL))
309 fprintf(stderr, _("Failure to establish config directory\n"));
313 /* Initialize SDL system and subsystems */
315 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
317 fprintf(stderr, "%s\n", SDL_GetError());
321 /* Intitialize the configuration */
326 /* Initialize the language */
328 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
330 /* Prepare run without GUI */
334 if (!level_replay(replay_path))
336 fprintf(stderr, _("Replay file '%s': %s\n"), replay_path,
337 errno ? strerror(errno) : _("Not a replay file"));
340 else if (display_info)
341 demo_replay_dump_info();
346 if (replay_path == NULL)
348 fprintf(stderr, _("%s: --info requires --replay\n"),
355 /* Initialize the joystick. */
357 if (SDL_NumJoysticks() > 0)
359 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
361 SDL_JoystickEventState(SDL_ENABLE);
364 /* Initialize the audio. */
366 audio_bind(AUD_MENU, 3, "snd/menu.wav");
367 audio_bind(AUD_START, 1, _("snd/select.ogg"));
368 audio_bind(AUD_READY, 1, _("snd/ready.ogg"));
369 audio_bind(AUD_SET, 1, _("snd/set.ogg"));
370 audio_bind(AUD_GO, 1, _("snd/go.ogg"));
371 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
372 audio_bind(AUD_BUMPS, 3, "snd/bumplil.ogg");
373 audio_bind(AUD_BUMPM, 3, "snd/bump.ogg");
374 audio_bind(AUD_BUMPL, 3, "snd/bumpbig.ogg");
375 audio_bind(AUD_COIN, 2, "snd/coin.wav");
376 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
377 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
378 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
379 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
380 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
381 audio_bind(AUD_SCORE, 1, _("snd/record.ogg"));
382 audio_bind(AUD_FALL, 1, _("snd/fall.ogg"));
383 audio_bind(AUD_TIME, 1, _("snd/time.ogg"));
384 audio_bind(AUD_OVER, 1, _("snd/over.ogg"));
385 audio_bind(AUD_GROW, 5, "snd/grow.ogg");
386 audio_bind(AUD_SHRINK, 5, "snd/shrink.ogg");
390 /* Require 16-bit double buffer with 16-bit depth buffer. */
392 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
393 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
394 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
395 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
396 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);
408 /* Initialize the video. */
410 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
411 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
413 fprintf(stderr, "%s\n", SDL_GetError());
417 SDL_WM_SetCaption(TITLE, TITLE);
419 /* Initialize the run state. */
421 init_state(&st_null);
425 level_replay(replay_path);
427 goto_state(&st_demo_play);
430 goto_state(&st_title);
432 /* Run the main game loop. */
436 if ((t1 = SDL_GetTicks()) > t0)
438 st_timer((t1 - t0) / 1000.f);
440 SDL_GL_SwapBuffers();
444 if (config_get_d(CONFIG_NICE))
448 /* Gracefully close the game */
450 if (SDL_JoystickOpened(0))
451 SDL_JoystickClose(joy);
460 /*---------------------------------------------------------------------------*/