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"
50 #define VERSION "1.4.1svn"
52 /*---------------------------------------------------------------------------*/
54 static void shot(void)
56 static char filename[MAXSTR];
59 sprintf(filename, _("screen%02d.png"), num++);
61 image_snap(filename, config_get_d(CONFIG_WIDTH),
62 config_get_d(CONFIG_HEIGHT));
65 /*---------------------------------------------------------------------------*/
67 static void toggle_wire(void)
73 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
74 glEnable(GL_TEXTURE_2D);
75 glEnable(GL_LIGHTING);
80 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
81 glDisable(GL_TEXTURE_2D);
82 glDisable(GL_LIGHTING);
87 static void toggle_fullscreen(void)
91 SDL_GetMouseState(&x, &y);
92 config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
93 config_get_d(CONFIG_HEIGHT));
97 /*---------------------------------------------------------------------------*/
104 while (d && SDL_PollEvent(&e))
106 if (e.type == SDL_QUIT)
109 if (e.type == SDL_KEYDOWN)
110 switch (e.key.keysym.sym)
112 case SDLK_SPACE: config_tgl_pause(); break;
113 case SDLK_F11: toggle_fullscreen(); break;
114 case SDLK_F10: shot(); break;
115 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
116 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
117 case SDLK_F7: toggle_wire(); break;
121 if (!config_get_pause())
124 case SDL_MOUSEMOTION:
125 st_point(+e.motion.x,
126 -e.motion.y + config_get_d(CONFIG_HEIGHT),
128 config_get_d(CONFIG_MOUSE_INVERT)
129 ? +e.motion.yrel : -e.motion.yrel);
132 case SDL_MOUSEBUTTONDOWN:
133 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
136 case SDL_MOUSEBUTTONUP:
137 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
142 switch (e.key.keysym.sym)
146 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
149 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
152 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
155 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
158 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
161 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
165 if (SDL_EnableUNICODE(-1))
166 d = st_keybd(e.key.keysym.unicode, 1);
168 d = st_keybd(e.key.keysym.sym, 1);
174 switch (e.key.keysym.sym)
177 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
180 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
184 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
188 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
192 d = st_keybd(e.key.keysym.sym, 0);
197 case SDL_ACTIVEEVENT:
198 if (e.active.state == SDL_APPINPUTFOCUS)
199 if (e.active.gain == 0 && config_get_grab())
203 case SDL_JOYAXISMOTION:
204 st_stick(e.jaxis.axis, e.jaxis.value);
207 case SDL_JOYBUTTONDOWN:
208 d = st_buttn(e.jbutton.button, 1);
211 case SDL_JOYBUTTONUP:
212 d = st_buttn(e.jbutton.button, 0);
219 /*---------------------------------------------------------------------------*/
222 static char *data_path = NULL;
223 static char *replay_path = NULL;
224 static char *level_path = NULL;
225 static int display_info = 0;
227 /* Option handling */
230 "Usage: %s [options ...]\n" \
231 "-r, --replay file play the replay 'file'.\n" \
232 "-l, --level file.sol play the level 'file.sol'.\n" \
233 "-i, --info display info about level or replay.\n" \
234 " --data dir use 'dir' as game data directory.\n" \
235 "-v, --version show version.\n" \
236 "-h, -?, --help show this usage message.\n")
238 static void parse_args(int argc, char **argv)
240 #define CASE(x) (strcmp(*argv, (x)) == 0) /* Check current option */
241 #define MAND !(missing = (argv[1] == NULL)) /* Argument is mandatory */
242 char *exec = *(argv++);
243 int missing; /* Argument is missing. */
245 while (*argv != NULL)
248 if (CASE("-h") || CASE("-?") || CASE("--help"))
253 else if (CASE("-v") || CASE("--version"))
255 printf(_("%s: %s version %s\n"), exec, TITLE, VERSION);
258 else if (CASE("--data") && MAND)
259 data_path = *(++argv);
260 else if ((CASE("-r") || CASE("--replay")) && MAND)
261 replay_path = *(++argv);
262 else if ((CASE("-l") || CASE("--level")) && MAND)
263 level_path = *(++argv);
264 else if ((CASE("-i") || CASE("--info")))
268 fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
269 fprintf(stderr, USAGE, exec);
274 fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
276 fprintf(stderr, USAGE, exec);
284 int main(int argc, char *argv[])
286 SDL_Joystick *joy = NULL;
287 int t1, t0; /* ticks */
288 SDL_Surface *icon; /* WM icon */
290 language_init("neverball", CONFIG_LOCALE);
292 parse_args(argc, argv);
294 if (!config_data_path(data_path, SET_FILE))
296 fprintf(stderr, _("Failure to establish game data directory\n"));
300 if (!config_user_path(NULL))
302 fprintf(stderr, _("Failure to establish config directory\n"));
306 /* Intitialize the configuration */
311 /* Initialize the language */
313 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
315 /* Prepare run without sdl */
317 if (replay_path != NULL)
319 if (!level_replay(replay_path))
321 fprintf(stderr, _("Replay file '%s': "), replay_path);
325 fprintf(stderr, _("Not a replay file\n"));
328 else if (display_info)
329 demo_replay_dump_info();
332 if (level_path != NULL)
335 if (!level_load(level_path, &l))
337 else if (display_info)
343 if (replay_path == NULL && level_path == NULL)
345 fprintf(stderr, _("%s: --info requires --replay or --level\n"),
352 /* Initialize SDL system and subsystems */
354 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
356 fprintf(stderr, "%s\n", SDL_GetError());
360 /* Initialize the joystick. */
362 if (SDL_NumJoysticks() > 0)
364 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
366 SDL_JoystickEventState(SDL_ENABLE);
369 /* Initialize the audio. */
371 audio_bind(AUD_MENU, 3, "snd/menu.wav");
372 audio_bind(AUD_START, 1, "snd/select.ogg");
373 audio_bind(AUD_READY, 1, "snd/ready.ogg");
374 audio_bind(AUD_SET, 1, "snd/set.ogg");
375 audio_bind(AUD_GO, 1, "snd/go.ogg");
376 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
377 audio_bind(AUD_BUMP, 3, "snd/bump.ogg");
378 audio_bind(AUD_COIN, 2, "snd/coin.wav");
379 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
380 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
381 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
382 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
383 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
384 audio_bind(AUD_SCORE, 1, "snd/record.ogg");
385 audio_bind(AUD_FALL, 1, "snd/fall.ogg");
386 audio_bind(AUD_TIME, 1, "snd/time.ogg");
387 audio_bind(AUD_OVER, 1, "snd/over.ogg");
391 /* Require 16-bit double buffer with 16-bit depth buffer. */
393 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
394 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
395 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
396 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
397 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
399 /* Initialize the video. */
401 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
402 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
404 fprintf(stderr, "%s\n", SDL_GetError());
408 /* Set the WM icon */
410 icon = IMG_Load(config_data("icon/neverball.png"));
411 SDL_WM_SetIcon(icon, NULL);
412 SDL_WM_SetCaption(TITLE, TITLE);
414 /* Initialize the run state. */
416 init_state(&st_null);
417 if (replay_path != NULL)
419 level_replay(replay_path);
422 else if (level_path != NULL)
424 level_play_single(level_path);
425 goto_state(&st_level);
428 goto_state(&st_title);
430 /* Run the main game loop. */
434 if ((t1 = SDL_GetTicks()) > t0)
436 if (config_get_pause())
440 SDL_Delay(10); /* Be nice! */
444 st_timer((t1 - t0) / 1000.f);
447 SDL_GL_SwapBuffers();
451 if (config_get_d(CONFIG_NICE))
455 /* Gracefully close the game */
457 if (SDL_JoystickOpened(0))
458 SDL_JoystickClose(joy);
467 /*---------------------------------------------------------------------------*/