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 /*---------------------------------------------------------------------------*/
41 const char TITLE[] = "Neverball " VERSION;
42 const char ICON[] = "icon/neverball.png";
44 /*---------------------------------------------------------------------------*/
46 static void shot(void)
48 static char filename[MAXSTR];
50 sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
54 /*---------------------------------------------------------------------------*/
56 static void toggle_wire(void)
62 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
63 glEnable(GL_TEXTURE_2D);
64 glEnable(GL_LIGHTING);
69 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
70 glDisable(GL_TEXTURE_2D);
71 glDisable(GL_LIGHTING);
76 /*---------------------------------------------------------------------------*/
84 /* Process SDL events. */
86 while (d && SDL_PollEvent(&e))
95 -e.motion.y + config_get_d(CONFIG_HEIGHT),
97 config_get_d(CONFIG_MOUSE_INVERT)
98 ? +e.motion.yrel : -e.motion.yrel);
101 case SDL_MOUSEBUTTONDOWN:
102 d = st_click(e.button.button, 1);
105 case SDL_MOUSEBUTTONUP:
106 d = st_click(e.button.button, 0);
111 c = e.key.keysym.sym;
113 if (config_tst_d(CONFIG_KEY_FORWARD, c))
114 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
116 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
117 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
119 else if (config_tst_d(CONFIG_KEY_LEFT, c))
120 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
122 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
123 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
127 case SDLK_F10: shot(); break;
128 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
129 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
137 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
140 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
144 if (SDL_EnableUNICODE(-1))
145 d = st_keybd(e.key.keysym.unicode, 1);
147 d = st_keybd(e.key.keysym.sym, 1);
154 c = e.key.keysym.sym;
156 if (config_tst_d(CONFIG_KEY_FORWARD, c))
157 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
159 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
160 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
162 else if (config_tst_d(CONFIG_KEY_LEFT, c))
163 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
165 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
166 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
171 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
174 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
178 d = st_keybd(e.key.keysym.sym, 0);
181 case SDL_ACTIVEEVENT:
182 if (e.active.state == SDL_APPINPUTFOCUS)
183 if (e.active.gain == 0 && video_get_grab())
187 case SDL_JOYAXISMOTION:
188 st_stick(e.jaxis.axis, e.jaxis.value);
191 case SDL_JOYBUTTONDOWN:
192 d = st_buttn(e.jbutton.button, 1);
195 case SDL_JOYBUTTONUP:
196 d = st_buttn(e.jbutton.button, 0);
201 /* Process events via the tilt sensor API. */
208 st_angle((int) tilt_get_x(),
211 while (tilt_get_button(&b, &s))
213 const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
214 const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
215 const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
216 const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
217 const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
218 const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
220 if (b == L || b == R || b == U || b == D)
222 static int pad[4] = { 0, 0, 0, 0 };
224 /* Track the state of the D-pad buttons. */
226 if (b == L) pad[0] = s;
227 else if (b == R) pad[1] = s;
228 else if (b == U) pad[2] = s;
229 else if (b == D) pad[3] = s;
231 /* Convert D-pad button events into joystick axis motion. */
233 if (pad[0] && !pad[1]) st_stick(X, -JOY_MAX);
234 else if (pad[1] && !pad[0]) st_stick(X, +JOY_MAX);
237 if (pad[2] && !pad[3]) st_stick(Y, -JOY_MAX);
238 else if (pad[3] && !pad[2]) st_stick(Y, +JOY_MAX);
241 else d = st_buttn(b, s);
248 /*---------------------------------------------------------------------------*/
250 static char *data_path = NULL;
251 static char *demo_path = NULL;
253 static unsigned int display_info = 0;
254 static unsigned int replay_demo = 0;
258 "Usage: %s [options ...]\n" \
260 " -h, --help show this usage message.\n" \
261 " -v, --version show version.\n" \
262 " -d, --data <dir> use 'dir' as game data directory.\n" \
263 " -r, --replay <file> play the replay 'file'.\n" \
264 " -i, --info display info about a replay.\n" \
267 #define argument_error(option) { \
268 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option); \
271 static void parse_args(int argc, char **argv)
275 /* Scan argument list. */
277 for (i = 1; i < argc; i++)
279 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
281 printf(usage, argv[0]);
285 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
287 printf("%s\n", VERSION);
291 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
295 argument_error(argv[i]);
298 data_path = argv[++i];
302 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
306 argument_error(argv[i]);
309 demo_path = argv[++i];
313 if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info") == 0)
319 /* Assume a single unrecognised argument is a replay name. */
328 /* Resolve conflicts. */
331 replay_demo = display_info ? 0 : 1;
335 /* FIXME, I'm a required option. */
336 fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
342 #undef argument_error
344 /*---------------------------------------------------------------------------*/
346 static int is_replay(struct dir_item *item)
348 return strcmp(item->path + strlen(item->path) - 4, ".nbr") == 0;
351 static int is_score(struct dir_item *item)
353 return strncmp(item->path, "neverballhs-", sizeof ("neverballhs-") - 1) == 0;
356 static void make_dirs_and_migrate(void)
364 if (fs_mkdir("Replays"))
366 if ((items = fs_dir_scan("", is_replay)))
368 for (i = 0; i < array_len(items); i++)
370 src = DIR_ITEM_GET(items, i)->path;
371 dst = concat_string("Replays/", src, NULL);
380 if (fs_mkdir("Scores"))
382 if ((items = fs_dir_scan("", is_score)))
384 for (i = 0; i < array_len(items); i++)
386 src = DIR_ITEM_GET(items, i)->path;
387 dst = concat_string("Scores/",
388 src + sizeof ("neverballhs-") - 1,
399 fs_mkdir("Screenshots");
402 int main(int argc, char *argv[])
404 SDL_Joystick *joy = NULL;
408 if (!fs_init(argv[0]))
410 fputs("Failure to initialize virtual file system\n", stderr);
414 lang_init("neverball");
416 parse_args(argc, argv);
418 config_paths(data_path);
419 make_dirs_and_migrate();
421 /* Initialize SDL system and subsystems */
423 flags |= SDL_INIT_VIDEO;
424 flags |= SDL_INIT_AUDIO;
425 flags |= config_get_d(CONFIG_JOYSTICK) ? SDL_INIT_JOYSTICK : 0;
427 if (SDL_Init(flags) == -1)
429 fprintf(stderr, "%s\n", SDL_GetError());
433 /* Intitialize the configuration */
438 /* Dump replay information and exit. */
440 if (display_info && fs_add_path(dir_name(demo_path)))
442 if (!progress_replay(base_name(demo_path, NULL)))
444 fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
445 errno ? strerror(errno) : L_("Not a replay file"));
448 demo_replay_dump_info();
452 /* Initialize the joystick. */
454 if (SDL_WasInit(SDL_INIT_JOYSTICK) && SDL_NumJoysticks() > 0)
456 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
458 SDL_JoystickEventState(SDL_ENABLE);
461 /* Initialize the audio. */
466 /* Initialize the video. */
468 if (!video_init(TITLE, ICON))
471 init_state(&st_null);
473 /* Initialise demo playback. */
475 if (replay_demo && fs_add_path(dir_name(demo_path)) &&
476 progress_replay(base_name(demo_path, NULL)))
479 goto_state(&st_demo_play);
482 goto_state(&st_title);
484 /* Run the main game loop. */
486 uniform = config_get_d(CONFIG_UNIFORM);
495 /* Step the game uniformly, as configured. */
499 for (u = 0; u < abs(uniform); ++u)
502 t0 += (int) (DT * 1000);
507 /* Step the game state at least up to the current time. */
512 t0 += (int) (DT * 1000);
518 st_paint(0.001f * t0);
524 if (config_get_d(CONFIG_NICE))
528 /* Gracefully close the game */
531 SDL_JoystickClose(joy);
541 /*---------------------------------------------------------------------------*/