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 const char TITLE[] = "Neverball " VERSION;
41 const char ICON[] = "icon/neverball.png";
43 /*---------------------------------------------------------------------------*/
45 static int shot_pending;
47 static void shot_prep(void)
52 static void shot_take(void)
54 static char filename[MAXSTR];
58 sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
64 /*---------------------------------------------------------------------------*/
66 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);
88 /*---------------------------------------------------------------------------*/
90 static int handle_key_dn(SDL_Event *e)
95 c = e->key.keysym.sym;
97 if (config_tst_d(CONFIG_KEY_FORWARD, c))
98 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -1.0f);
100 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
101 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +1.0f);
103 else if (config_tst_d(CONFIG_KEY_LEFT, c))
104 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -1.0f);
106 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
107 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +1.0f);
111 case SDLK_F10: shot_prep(); break;
112 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
113 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
120 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
123 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
127 if (SDL_EnableUNICODE(-1))
128 d = st_keybd(e->key.keysym.unicode, 1);
130 d = st_keybd(e->key.keysym.sym, 1);
136 static int handle_key_up(SDL_Event *e)
141 c = e->key.keysym.sym;
143 if (config_tst_d(CONFIG_KEY_FORWARD, c))
144 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
146 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
147 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
149 else if (config_tst_d(CONFIG_KEY_LEFT, c))
150 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
152 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
153 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
158 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
161 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
165 d = st_keybd(e->key.keysym.sym, 0);
171 static int loop(void)
176 /* Process SDL events. */
178 while (d && SDL_PollEvent(&e))
185 case SDL_MOUSEMOTION:
186 st_point(+e.motion.x,
187 -e.motion.y + config_get_d(CONFIG_HEIGHT),
189 config_get_d(CONFIG_MOUSE_INVERT)
190 ? +e.motion.yrel : -e.motion.yrel);
193 case SDL_MOUSEBUTTONDOWN:
194 d = st_click(e.button.button, 1);
197 case SDL_MOUSEBUTTONUP:
198 d = st_click(e.button.button, 0);
202 d = handle_key_dn(&e);
206 d = handle_key_up(&e);
209 case SDL_ACTIVEEVENT:
210 if (e.active.state == SDL_APPINPUTFOCUS)
211 if (e.active.gain == 0 && video_get_grab())
212 goto_state(&st_pause);
215 case SDL_JOYAXISMOTION:
216 st_stick(e.jaxis.axis, JOY_VALUE(e.jaxis.value));
219 case SDL_JOYBUTTONDOWN:
220 d = st_buttn(e.jbutton.button, 1);
223 case SDL_JOYBUTTONUP:
224 d = st_buttn(e.jbutton.button, 0);
229 /* Process events via the tilt sensor API. */
236 st_angle(tilt_get_x(),
239 while (tilt_get_button(&b, &s))
241 const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
242 const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
243 const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
244 const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
245 const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
246 const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
248 if (b == L || b == R || b == U || b == D)
250 static int pad[4] = { 0, 0, 0, 0 };
252 /* Track the state of the D-pad buttons. */
254 if (b == L) pad[0] = s;
255 else if (b == R) pad[1] = s;
256 else if (b == U) pad[2] = s;
257 else if (b == D) pad[3] = s;
259 /* Convert D-pad button events into joystick axis motion. */
261 if (pad[0] && !pad[1]) st_stick(X, -1.0f);
262 else if (pad[1] && !pad[0]) st_stick(X, +1.0f);
263 else st_stick(X, 0.0f);
265 if (pad[2] && !pad[3]) st_stick(Y, -1.0f);
266 else if (pad[3] && !pad[2]) st_stick(Y, +1.0f);
267 else st_stick(Y, 0.0f);
269 else d = st_buttn(b, s);
276 /*---------------------------------------------------------------------------*/
278 static char *opt_data;
279 static char *opt_replay;
280 static char *opt_level;
284 "Usage: %s [options ...]\n" \
286 " -h, --help show this usage message.\n" \
287 " -v, --version show version.\n" \
288 " -d, --data <dir> use 'dir' as game data directory.\n" \
289 " -r, --replay <file> play the replay 'file'.\n" \
290 " -l, --level <file> load the level 'file'\n" \
293 #define opt_error(option) \
294 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option)
296 static void opt_parse(int argc, char **argv)
300 /* Scan argument list. */
302 for (i = 1; i < argc; i++)
304 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
306 printf(opt_usage, argv[0]);
310 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
312 printf("%s\n", VERSION);
316 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
323 opt_data = argv[++i];
327 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
334 opt_replay = argv[++i];
338 if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level") == 0)
345 opt_level = argv[++i];
349 /* Perform magic on a single unrecognized argument. */
353 size_t len = strlen(argv[i]);
358 char *ext = argv[i] + len - 4;
360 if (strcmp(ext, ".map") == 0)
361 strncpy(ext, ".sol", 4);
363 if (strcmp(ext, ".sol") == 0)
370 opt_replay = argv[i];
380 /*---------------------------------------------------------------------------*/
382 static int is_replay(struct dir_item *item)
384 return str_ends_with(item->path, ".nbr");
387 static int is_score_file(struct dir_item *item)
389 return str_starts_with(item->path, "neverballhs-");
392 static void make_dirs_and_migrate(void)
400 if (fs_mkdir("Replays"))
402 if ((items = fs_dir_scan("", is_replay)))
404 for (i = 0; i < array_len(items); i++)
406 src = DIR_ITEM_GET(items, i)->path;
407 dst = concat_string("Replays/", src, NULL);
416 if (fs_mkdir("Scores"))
418 if ((items = fs_dir_scan("", is_score_file)))
420 for (i = 0; i < array_len(items); i++)
422 src = DIR_ITEM_GET(items, i)->path;
423 dst = concat_string("Scores/",
424 src + sizeof ("neverballhs-") - 1,
435 fs_mkdir("Screenshots");
438 /*---------------------------------------------------------------------------*/
440 int main(int argc, char *argv[])
442 SDL_Joystick *joy = NULL;
445 if (!fs_init(argv[0]))
447 fprintf(stderr, "Failure to initialize virtual file system: %s\n",
452 lang_init("neverball");
454 opt_parse(argc, argv);
456 config_paths(opt_data);
457 make_dirs_and_migrate();
459 /* Initialize SDL. */
461 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
463 fprintf(stderr, "%s\n", SDL_GetError());
467 /* Intitialize configuration. */
472 /* Initialize joystick. */
474 if (config_get_d(CONFIG_JOYSTICK) && SDL_NumJoysticks() > 0)
476 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
478 SDL_JoystickEventState(SDL_ENABLE);
481 /* Initialize audio. */
486 /* Initialize video. */
488 if (!video_init(TITLE, ICON))
491 init_state(&st_null);
493 /* Initialize demo playback or load the level. */
496 fs_add_path(dir_name(opt_replay)) &&
497 progress_replay(base_name(opt_replay)))
500 goto_state(&st_demo_play);
504 const char *path = fs_resolve(opt_level);
509 /* HACK: must be around for the duration of the game. */
510 static struct level level;
512 if (level_load(path, &level))
514 progress_init(MODE_STANDALONE);
516 if (progress_play(&level))
518 goto_state(&st_level);
523 else fprintf(stderr, "%s: file is not in game path\n", opt_level);
526 goto_state(&st_title);
529 goto_state(&st_title);
531 /* Run the main game loop. */
537 if ((t1 = SDL_GetTicks()) > t0)
539 /* Step the game state. */
541 st_timer(0.001f * (t1 - t0));
547 st_paint(0.001f * t0);
551 if (config_get_d(CONFIG_NICE))
559 SDL_JoystickClose(joy);
567 /*---------------------------------------------------------------------------*/