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>
49 #define TITLE "Neverball"
51 /*---------------------------------------------------------------------------*/
53 static void shot(void)
55 static char filename[MAXSTR];
58 sprintf(filename, _("screen%02d.png"), num++);
63 /*---------------------------------------------------------------------------*/
65 static void toggle_wire(void)
71 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
72 glEnable(GL_TEXTURE_2D);
73 glEnable(GL_LIGHTING);
78 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
79 glDisable(GL_TEXTURE_2D);
80 glDisable(GL_LIGHTING);
85 static void toggle_fullscreen(void)
89 SDL_GetMouseState(&x, &y);
90 config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
91 config_get_d(CONFIG_HEIGHT));
95 /*---------------------------------------------------------------------------*/
102 while (d && SDL_PollEvent(&e))
104 if (e.type == SDL_QUIT)
107 if (e.type == SDL_KEYDOWN)
108 switch (e.key.keysym.sym)
110 case SDLK_SPACE: config_tgl_pause(); break;
111 case SDLK_F11: toggle_fullscreen(); break;
112 case SDLK_F10: shot(); break;
113 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
114 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
115 case SDLK_F7: toggle_wire(); break;
119 if (!config_get_pause())
122 case SDL_MOUSEMOTION:
123 st_point(+e.motion.x,
124 -e.motion.y + config_get_d(CONFIG_HEIGHT),
126 config_get_d(CONFIG_MOUSE_INVERT)
127 ? +e.motion.yrel : -e.motion.yrel);
130 case SDL_MOUSEBUTTONDOWN:
131 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
134 case SDL_MOUSEBUTTONUP:
135 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
140 switch (e.key.keysym.sym)
144 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
147 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
150 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
153 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
156 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
159 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
163 if (SDL_EnableUNICODE(-1))
164 d = st_keybd(e.key.keysym.unicode, 1);
166 d = st_keybd(e.key.keysym.sym, 1);
172 switch (e.key.keysym.sym)
175 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
178 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
182 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
186 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
190 d = st_keybd(e.key.keysym.sym, 0);
195 case SDL_ACTIVEEVENT:
196 if (e.active.state == SDL_APPINPUTFOCUS)
197 if (e.active.gain == 0 && config_get_grab())
201 case SDL_JOYAXISMOTION:
202 st_stick(e.jaxis.axis, e.jaxis.value);
205 case SDL_JOYBUTTONDOWN:
206 d = st_buttn(e.jbutton.button, 1);
209 case SDL_JOYBUTTONUP:
210 d = st_buttn(e.jbutton.button, 0);
217 /*---------------------------------------------------------------------------*/
221 static char *data_path = NULL;
222 static char *replay_path = NULL;
223 static int display_info = 0;
225 /* Option handling */
227 static void parse_args(int argc, char **argv)
229 char *exec = *(argv++);
230 int missing; /* Argument is missing. */
232 const char *usage = _(
233 "Usage: %s [options ...]\n"
234 "-r, --replay file play the replay 'file'.\n"
235 "-i, --info display info about level or replay.\n"
236 " --data dir use 'dir' as game data directory.\n"
237 "-v, --version show version.\n"
238 "-h, -?, --help show this usage message.\n"
241 #define CASE(x) (strcmp(*argv, (x)) == 0) /* Check current option */
242 #define MAND !(missing = (argv[1] == NULL)) /* Argument is mandatory */
244 while (*argv != NULL)
247 if (CASE("-h") || CASE("-?") || CASE("--help"))
252 else if (CASE("-v") || CASE("--version"))
254 printf("%s %s\n", TITLE, VERSION);
257 else if (CASE("--data") && MAND)
258 data_path = *(++argv);
259 else if ((CASE("-r") || CASE("--replay")) && MAND)
260 replay_path = *(++argv);
261 else if ((CASE("-i") || CASE("--info")))
265 fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
266 fprintf(stderr, usage, exec);
271 fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
273 fprintf(stderr, usage, exec);
281 int main(int argc, char *argv[])
283 SDL_Joystick *joy = NULL;
288 language_init("neverball", CONFIG_LOCALE);
290 parse_args(argc, argv);
292 if (!config_data_path(data_path, SET_FILE))
294 fprintf(stderr, _("Failure to establish game data directory\n"));
298 if (!config_user_path(NULL))
300 fprintf(stderr, _("Failure to establish config directory\n"));
304 /* Intitialize the configuration */
309 /* Initialize the language */
311 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
313 /* Prepare run without SDL */
317 if (!level_replay(replay_path))
319 fprintf(stderr, _("Replay file '%s': %s\n"), replay_path,
320 errno ? strerror(errno) : _("Not a replay file"));
323 else if (display_info)
324 demo_replay_dump_info();
329 if (replay_path == NULL)
331 fprintf(stderr, _("%s: --info requires --replay\n"),
338 /* Initialize SDL system and subsystems */
340 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
342 fprintf(stderr, "%s\n", SDL_GetError());
346 /* Initialize the joystick. */
348 if (SDL_NumJoysticks() > 0)
350 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
352 SDL_JoystickEventState(SDL_ENABLE);
355 /* Initialize the audio. */
357 audio_bind(AUD_MENU, 3, "snd/menu.wav");
358 audio_bind(AUD_START, 1, _("snd/select.ogg"));
359 audio_bind(AUD_READY, 1, _("snd/ready.ogg"));
360 audio_bind(AUD_SET, 1, _("snd/set.ogg"));
361 audio_bind(AUD_GO, 1, _("snd/go.ogg"));
362 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
363 audio_bind(AUD_BUMP, 3, "snd/bump.ogg");
364 audio_bind(AUD_COIN, 2, "snd/coin.wav");
365 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
366 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
367 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
368 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
369 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
370 audio_bind(AUD_SCORE, 1, _("snd/record.ogg"));
371 audio_bind(AUD_FALL, 1, _("snd/fall.ogg"));
372 audio_bind(AUD_TIME, 1, _("snd/time.ogg"));
373 audio_bind(AUD_OVER, 1, _("snd/over.ogg"));
377 /* Require 16-bit double buffer with 16-bit depth buffer. */
379 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
380 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
381 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
382 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
383 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
385 /* Set the WM icon */
387 icon = IMG_Load(config_data("icon/neverball.png"));
391 SDL_WM_SetIcon(icon, NULL);
392 SDL_FreeSurface(icon);
395 /* Initialize the video. */
397 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
398 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
400 fprintf(stderr, "%s\n", SDL_GetError());
404 SDL_WM_SetCaption(TITLE, TITLE);
406 /* Initialize the run state. */
408 init_state(&st_null);
412 level_replay(replay_path);
414 goto_state(&st_demo_play);
417 goto_state(&st_title);
419 /* Run the main game loop. */
423 if ((t1 = SDL_GetTicks()) > t0)
425 if (config_get_pause())
433 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 /*---------------------------------------------------------------------------*/