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++);
60 image_snap(filename, config_get_d(CONFIG_WIDTH),
61 config_get_d(CONFIG_HEIGHT));
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 /*---------------------------------------------------------------------------*/
103 while (d && SDL_PollEvent(&e))
105 if (e.type == SDL_QUIT)
108 if (e.type == SDL_KEYDOWN)
109 switch (e.key.keysym.sym)
111 case SDLK_SPACE: config_tgl_pause(); break;
112 case SDLK_F11: toggle_fullscreen(); break;
113 case SDLK_F10: shot(); break;
114 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
115 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
116 case SDLK_F7: toggle_wire(); break;
120 if (!config_get_pause())
123 case SDL_MOUSEMOTION:
124 st_point(+e.motion.x,
125 -e.motion.y + config_get_d(CONFIG_HEIGHT),
127 config_get_d(CONFIG_MOUSE_INVERT)
128 ? +e.motion.yrel : -e.motion.yrel);
131 case SDL_MOUSEBUTTONDOWN:
132 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
135 case SDL_MOUSEBUTTONUP:
136 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
141 switch (e.key.keysym.sym)
145 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
148 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
151 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
154 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
157 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
160 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
164 if (SDL_EnableUNICODE(-1))
165 d = st_keybd(e.key.keysym.unicode, 1);
167 d = st_keybd(e.key.keysym.sym, 1);
173 switch (e.key.keysym.sym)
176 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
179 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
183 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
187 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
191 d = st_keybd(e.key.keysym.sym, 0);
196 case SDL_ACTIVEEVENT:
197 if (e.active.state == SDL_APPINPUTFOCUS)
198 if (e.active.gain == 0 && config_get_grab())
202 case SDL_JOYAXISMOTION:
203 st_stick(e.jaxis.axis, e.jaxis.value);
206 case SDL_JOYBUTTONDOWN:
207 d = st_buttn(e.jbutton.button, 1);
210 case SDL_JOYBUTTONUP:
211 d = st_buttn(e.jbutton.button, 0);
218 /*---------------------------------------------------------------------------*/
221 static char *data_path = NULL;
222 static char *replay_path = NULL;
223 static char *level_path = NULL;
224 static int display_info = 0;
226 /* Option handling */
229 "Usage: %s [options ...]\n" \
230 "-r, --replay file play the replay 'file'.\n" \
231 "-l, --level file.sol play the level 'file.sol'.\n" \
232 "-i, --info display info about level or replay.\n" \
233 " --data dir use 'dir' as game data directory.\n" \
234 "-v, --version show version.\n" \
235 "-h, -?, --help show this usage message.\n")
237 static void parse_args(int argc, char **argv)
239 #define CASE(x) (strcmp(*argv, (x)) == 0) /* Check current option */
240 #define MAND !(missing = (argv[1] == NULL)) /* Argument is mandatory */
241 char *exec = *(argv++);
242 int missing; /* Argument is missing. */
244 while (*argv != NULL)
247 if (CASE("-h") || CASE("-?") || CASE("--help"))
252 else if (CASE("-v") || CASE("--version"))
254 printf(_("%s: %s version %s\n"), exec, 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("-l") || CASE("--level")) && MAND)
262 level_path = *(++argv);
263 else if ((CASE("-i") || CASE("--info")))
267 fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
268 fprintf(stderr, USAGE, exec);
273 fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
275 fprintf(stderr, USAGE, exec);
283 int main(int argc, char *argv[])
285 SDL_Joystick *joy = NULL;
286 int t1, t0; /* ticks */
287 SDL_Surface *icon; /* WM icon */
289 language_init("neverball", CONFIG_LOCALE);
291 parse_args(argc, argv);
293 if (!config_data_path(data_path, SET_FILE))
295 fprintf(stderr, _("Failure to establish game data directory\n"));
299 if (!config_user_path(NULL))
301 fprintf(stderr, _("Failure to establish config directory\n"));
305 /* Intitialize the configuration */
310 /* Initialize the language */
312 language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
314 /* Prepare run without sdl */
316 if (replay_path != NULL)
318 if (!level_replay(replay_path))
320 fprintf(stderr, _("Replay file '%s': "), replay_path);
324 fprintf(stderr, _("Not a replay file\n"));
327 else if (display_info)
328 demo_replay_dump_info();
331 if (level_path != NULL)
334 if (!level_load(level_path, &l))
336 else if (display_info)
342 if (replay_path == NULL && level_path == NULL)
344 fprintf(stderr, _("%s: --info requires --replay or --level\n"),
351 /* Initialize SDL system and subsystems */
353 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
355 fprintf(stderr, "%s\n", SDL_GetError());
359 /* Initialize the joystick. */
361 if (SDL_NumJoysticks() > 0)
363 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
365 SDL_JoystickEventState(SDL_ENABLE);
368 /* Initialize the audio. */
370 audio_bind(AUD_MENU, 3, "snd/menu.wav");
371 audio_bind(AUD_START, 1, "snd/select.ogg");
372 audio_bind(AUD_READY, 1, "snd/ready.ogg");
373 audio_bind(AUD_SET, 1, "snd/set.ogg");
374 audio_bind(AUD_GO, 1, "snd/go.ogg");
375 audio_bind(AUD_BALL, 2, "snd/ball.ogg");
376 audio_bind(AUD_BUMP, 3, "snd/bump.ogg");
377 audio_bind(AUD_COIN, 2, "snd/coin.wav");
378 audio_bind(AUD_TICK, 4, "snd/tick.ogg");
379 audio_bind(AUD_TOCK, 4, "snd/tock.ogg");
380 audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
381 audio_bind(AUD_JUMP, 5, "snd/jump.ogg");
382 audio_bind(AUD_GOAL, 5, "snd/goal.wav");
383 audio_bind(AUD_SCORE, 1, "snd/record.ogg");
384 audio_bind(AUD_FALL, 1, "snd/fall.ogg");
385 audio_bind(AUD_TIME, 1, "snd/time.ogg");
386 audio_bind(AUD_OVER, 1, "snd/over.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 /* Initialize the video. */
400 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
401 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
403 fprintf(stderr, "%s\n", SDL_GetError());
407 /* Set the WM icon */
409 icon = IMG_Load(config_data("icon/neverball.png"));
410 SDL_WM_SetIcon(icon, NULL);
411 SDL_WM_SetCaption(TITLE, TITLE);
413 /* Initialize the run state. */
415 init_state(&st_null);
416 if (replay_path != NULL)
418 level_replay(replay_path);
421 else if (level_path != NULL)
423 level_play_single(level_path);
424 goto_state(&st_level);
427 goto_state(&st_title);
429 /* Run the main game loop. */
433 if ((t1 = SDL_GetTicks()) > t0)
435 if (config_get_pause())
439 SDL_Delay(10); /* Be nice! */
443 st_timer((t1 - t0) / 1000.f);
446 SDL_GL_SwapBuffers();
450 if (config_get_d(CONFIG_NICE))
454 /* Gracefully close the game */
456 if (SDL_JoystickOpened(0))
457 SDL_JoystickClose(joy);
466 /*---------------------------------------------------------------------------*/