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 /*---------------------------------------------------------------------------*/
40 #define TITLE "Neverball " VERSION
42 /*---------------------------------------------------------------------------*/
44 static void shot(void)
46 static char filename[MAXSTR];
48 sprintf(filename, "screen%05d.png", config_screenshot());
49 image_snap(config_user(filename));
52 /*---------------------------------------------------------------------------*/
54 static void toggle_wire(void)
60 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
61 glEnable(GL_TEXTURE_2D);
62 glEnable(GL_LIGHTING);
67 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
68 glDisable(GL_TEXTURE_2D);
69 glDisable(GL_LIGHTING);
74 /*---------------------------------------------------------------------------*/
82 /* Process SDL events. */
84 while (d && SDL_PollEvent(&e))
93 -e.motion.y + config_get_d(CONFIG_HEIGHT),
95 config_get_d(CONFIG_MOUSE_INVERT)
96 ? +e.motion.yrel : -e.motion.yrel);
99 case SDL_MOUSEBUTTONDOWN:
100 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
103 case SDL_MOUSEBUTTONUP:
104 d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
109 c = e.key.keysym.sym;
111 if (config_tst_d(CONFIG_KEY_FORWARD, c))
112 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
114 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
115 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
117 else if (config_tst_d(CONFIG_KEY_LEFT, c))
118 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
120 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
121 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
125 case SDLK_F10: shot(); break;
126 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
127 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
135 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
138 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
142 if (SDL_EnableUNICODE(-1))
143 d = st_keybd(e.key.keysym.unicode, 1);
145 d = st_keybd(e.key.keysym.sym, 1);
152 c = e.key.keysym.sym;
154 if (config_tst_d(CONFIG_KEY_FORWARD, c))
155 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
157 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
158 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
160 else if (config_tst_d(CONFIG_KEY_LEFT, c))
161 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
163 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
164 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
169 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
172 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
176 d = st_keybd(e.key.keysym.sym, 0);
179 case SDL_ACTIVEEVENT:
180 if (e.active.state == SDL_APPINPUTFOCUS)
181 if (e.active.gain == 0 && config_get_grab())
185 case SDL_JOYAXISMOTION:
186 st_stick(e.jaxis.axis, e.jaxis.value);
189 case SDL_JOYBUTTONDOWN:
190 d = st_buttn(e.jbutton.button, 1);
193 case SDL_JOYBUTTONUP:
194 d = st_buttn(e.jbutton.button, 0);
199 /* Process events via the tilt sensor API. */
206 st_angle((int) tilt_get_x(),
209 while (tilt_get_button(&b, &s))
211 const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
212 const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
213 const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
214 const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
215 const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
216 const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
218 if (b == L || b == R || b == U || b == D)
220 static int pad[4] = { 0, 0, 0, 0 };
222 /* Track the state of the D-pad buttons. */
224 if (b == L) pad[0] = s;
225 else if (b == R) pad[1] = s;
226 else if (b == U) pad[2] = s;
227 else if (b == D) pad[3] = s;
229 /* Convert D-pad button events into joystick axis motion. */
231 if (pad[0] && !pad[1]) st_stick(X, -JOY_MAX);
232 else if (pad[1] && !pad[0]) st_stick(X, +JOY_MAX);
235 if (pad[2] && !pad[3]) st_stick(Y, -JOY_MAX);
236 else if (pad[3] && !pad[2]) st_stick(Y, +JOY_MAX);
239 else d = st_buttn(b, s);
246 /*---------------------------------------------------------------------------*/
248 static char *data_path = NULL;
249 static char *demo_path = NULL;
251 static unsigned int display_info = 0;
252 static unsigned int replay_demo = 0;
256 "Usage: %s [options ...]\n" \
258 " -h, --help show this usage message.\n" \
259 " -v, --version show version.\n" \
260 " -d, --data <dir> use 'dir' as game data directory.\n" \
261 " -r, --replay <file> play the replay 'file'.\n" \
262 " -i, --info display info about a replay.\n" \
265 #define argument_error(option) { \
266 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option); \
269 static void parse_args(int argc, char **argv)
273 /* Scan argument list. */
275 for (i = 1; i < argc; i++)
277 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
279 printf(usage, argv[0]);
283 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
285 printf("%s\n", VERSION);
289 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
293 argument_error(argv[i]);
296 data_path = argv[++i];
300 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
304 argument_error(argv[i]);
307 demo_path = argv[++i];
311 if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info") == 0)
318 /* Resolve conflicts. */
321 replay_demo = display_info ? 0 : 1;
325 /* FIXME, I'm a required option. */
326 fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
332 #undef argument_error
334 /*---------------------------------------------------------------------------*/
336 int main(int argc, char *argv[])
338 SDL_Joystick *joy = NULL;
341 lang_init("neverball", CONFIG_LOCALE);
345 parse_args(argc, argv);
347 if (!config_data_path(data_path, SET_FILE))
349 fputs(L_("Failure to establish game data directory\n"), stderr);
353 if (!config_user_path(NULL))
355 fputs(L_("Failure to establish config directory\n"), stderr);
359 /* Initialize SDL system and subsystems */
361 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
363 fprintf(stderr, "%s\n", SDL_GetError());
367 /* Intitialize the configuration */
372 /* Dump replay information and exit. */
376 if (!progress_replay(demo_path))
378 fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
379 errno ? strerror(errno) : L_("Not a replay file"));
382 demo_replay_dump_info();
386 /* Initialize the joystick. */
388 if (SDL_NumJoysticks() > 0)
390 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
392 SDL_JoystickEventState(SDL_ENABLE);
395 /* Initialize the audio. */
400 /* Require 16-bit double buffer with 16-bit depth buffer. */
402 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
403 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
404 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
405 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
406 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
408 /* This has to happen before mode setting... */
410 set_SDL_icon("icon/neverball.png");
412 /* Initialize the video. */
414 if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
415 config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
417 fprintf(stderr, "%s\n", SDL_GetError());
421 /* ...and this has to happen after it. */
423 set_EWMH_icon("icon/neverball.png");
425 SDL_WM_SetCaption(TITLE, TITLE);
427 init_state(&st_null);
429 /* Initialise demo playback. */
431 if (replay_demo && progress_replay(demo_path))
434 goto_state(&st_demo_play);
437 goto_state(&st_title);
439 /* Run the main game loop. */
441 uniform = config_get_d(CONFIG_UNIFORM);
450 /* Step the game uniformly, as configured. */
454 for (u = 0; u < abs(uniform); ++u)
457 t0 += (int) (DT * 1000);
462 /* Step the game state at least up to the current time. */
467 t0 += (int) (DT * 1000);
473 st_paint(0.001f * t0);
479 if (config_get_d(CONFIG_NICE))
483 /* Gracefully close the game */
485 if (SDL_JoystickOpened(0))
486 SDL_JoystickClose(joy);
498 /*---------------------------------------------------------------------------*/