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 /*---------------------------------------------------------------------------*/
43 const char TITLE[] = "Neverball " VERSION;
44 const char ICON[] = "icon/neverball.png";
46 /*---------------------------------------------------------------------------*/
48 static int shot_pending;
50 static void shot_prep(void)
55 static void shot_take(void)
57 static char filename[MAXSTR];
61 sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
67 /*---------------------------------------------------------------------------*/
69 static void toggle_wire(void)
76 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
77 glEnable(GL_TEXTURE_2D);
78 glEnable(GL_LIGHTING);
83 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
84 glDisable(GL_TEXTURE_2D);
85 glDisable(GL_LIGHTING);
91 /*---------------------------------------------------------------------------*/
93 static int handle_key_dn(SDL_Event *e)
98 c = e->key.keysym.sym;
100 if (config_tst_d(CONFIG_KEY_FORWARD, c))
101 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -1.0f);
103 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
104 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +1.0f);
106 else if (config_tst_d(CONFIG_KEY_LEFT, c))
107 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -1.0f);
109 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
110 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +1.0f);
114 case SDLK_F10: shot_prep(); break;
115 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
116 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
123 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
126 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
130 if (SDL_EnableUNICODE(-1))
131 d = st_keybd(e->key.keysym.unicode, 1);
133 d = st_keybd(e->key.keysym.sym, 1);
139 static int handle_key_up(SDL_Event *e)
144 c = e->key.keysym.sym;
146 if (config_tst_d(CONFIG_KEY_FORWARD, c))
147 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
149 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
150 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
152 else if (config_tst_d(CONFIG_KEY_LEFT, c))
153 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
155 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
156 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
161 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
164 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
168 d = st_keybd(e->key.keysym.sym, 0);
174 static int loop(void)
181 /* Process SDL events. */
183 while (d && SDL_PollEvent(&e))
190 case SDL_MOUSEMOTION:
191 st_point(+e.motion.x,
192 -e.motion.y + config_get_d(CONFIG_HEIGHT),
194 config_get_d(CONFIG_MOUSE_INVERT)
195 ? +e.motion.yrel : -e.motion.yrel);
198 case SDL_MOUSEBUTTONDOWN:
199 d = st_click(e.button.button, 1);
202 case SDL_MOUSEBUTTONUP:
203 d = st_click(e.button.button, 0);
207 d = handle_key_dn(&e);
211 d = handle_key_up(&e);
214 case SDL_ACTIVEEVENT:
215 if (e.active.state == SDL_APPINPUTFOCUS)
216 if (e.active.gain == 0 && video_get_grab())
217 goto_state(&st_pause);
220 case SDL_JOYAXISMOTION:
221 st_stick(e.jaxis.axis, JOY_VALUE(e.jaxis.value));
224 case SDL_JOYBUTTONDOWN:
225 d = st_buttn(e.jbutton.button, 1);
228 case SDL_JOYBUTTONUP:
229 d = st_buttn(e.jbutton.button, 0);
234 /* Process events via the tilt sensor API. */
241 st_angle(tilt_get_x(),
244 while (tilt_get_button(&b, &s))
246 const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
247 const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
248 const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
249 const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
250 const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
251 const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
253 if (b == L || b == R || b == U || b == D)
255 static int pad[4] = { 0, 0, 0, 0 };
257 /* Track the state of the D-pad buttons. */
259 if (b == L) pad[0] = s;
260 else if (b == R) pad[1] = s;
261 else if (b == U) pad[2] = s;
262 else if (b == D) pad[3] = s;
264 /* Convert D-pad button events into joystick axis motion. */
266 if (pad[0] && !pad[1]) st_stick(X, -1.0f);
267 else if (pad[1] && !pad[0]) st_stick(X, +1.0f);
268 else st_stick(X, 0.0f);
270 if (pad[2] && !pad[3]) st_stick(Y, -1.0f);
271 else if (pad[3] && !pad[2]) st_stick(Y, +1.0f);
272 else st_stick(Y, 0.0f);
274 else d = st_buttn(b, s);
281 /*---------------------------------------------------------------------------*/
283 static char *opt_data;
284 static char *opt_replay;
285 static char *opt_level;
289 "Usage: %s [options ...]\n" \
291 " -h, --help show this usage message.\n" \
292 " -v, --version show version.\n" \
293 " -d, --data <dir> use 'dir' as game data directory.\n" \
294 " -r, --replay <file> play the replay 'file'.\n" \
295 " -l, --level <file> load the level 'file'\n" \
298 #define opt_error(option) \
299 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option)
301 static void opt_parse(int argc, char **argv)
305 /* Scan argument list. */
307 for (i = 1; i < argc; i++)
309 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
311 printf(opt_usage, argv[0]);
315 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
317 printf("%s\n", VERSION);
321 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
328 opt_data = argv[++i];
332 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
339 opt_replay = argv[++i];
343 if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level") == 0)
350 opt_level = argv[++i];
354 /* Perform magic on a single unrecognized argument. */
358 size_t len = strlen(argv[i]);
363 char *ext = argv[i] + len - 4;
365 if (strcmp(ext, ".map") == 0)
366 strncpy(ext, ".sol", 4);
368 if (strcmp(ext, ".sol") == 0)
375 opt_replay = argv[i];
385 /*---------------------------------------------------------------------------*/
387 static int is_replay(struct dir_item *item)
389 return str_ends_with(item->path, ".nbr");
392 static int is_score_file(struct dir_item *item)
394 return str_starts_with(item->path, "neverballhs-");
397 static void make_dirs_and_migrate(void)
405 if (fs_mkdir("Replays"))
407 if ((items = fs_dir_scan("", is_replay)))
409 for (i = 0; i < array_len(items); i++)
411 src = DIR_ITEM_GET(items, i)->path;
412 dst = concat_string("Replays/", src, NULL);
421 if (fs_mkdir("Scores"))
423 if ((items = fs_dir_scan("", is_score_file)))
425 for (i = 0; i < array_len(items); i++)
427 src = DIR_ITEM_GET(items, i)->path;
428 dst = concat_string("Scores/",
429 src + sizeof ("neverballhs-") - 1,
440 fs_mkdir("Screenshots");
443 /*---------------------------------------------------------------------------*/
445 int main(int argc, char *argv[])
447 SDL_Joystick *joy = NULL;
450 if (!fs_init(argv[0]))
452 fprintf(stderr, "Failure to initialize virtual file system: %s\n",
457 lang_init("neverball");
459 opt_parse(argc, argv);
461 config_paths(opt_data);
462 make_dirs_and_migrate();
464 /* Initialize SDL. */
466 if (SDL_Init(SDL_INIT_VIDEO |
471 SDL_INIT_JOYSTICK) == -1)
473 fprintf(stderr, "%s\n", SDL_GetError());
477 /* Intitialize configuration. */
482 /* Initialize joystick. */
484 if (config_get_d(CONFIG_JOYSTICK) && SDL_NumJoysticks() > 0)
486 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
488 SDL_JoystickEventState(SDL_ENABLE);
491 /* Initialize audio. */
496 /* Initialize video. */
498 if (!video_init(TITLE, ICON))
501 init_state(&st_null);
503 /* Initialize demo playback or load the level. */
506 fs_add_path(dir_name(opt_replay)) &&
507 progress_replay(base_name(opt_replay)))
510 goto_state(&st_demo_play);
514 const char *path = fs_resolve(opt_level);
519 /* HACK: must be around for the duration of the game. */
520 static struct level level;
522 if (level_load(path, &level))
524 progress_init(MODE_STANDALONE);
526 if (progress_play(&level))
528 goto_state(&st_level);
533 else fprintf(stderr, "%s: file is not in game path\n", opt_level);
536 goto_state(&st_title);
539 goto_state(&st_title);
542 maemo_init("neverball");
545 /* Run the main game loop. */
551 if ((t1 = SDL_GetTicks()) > t0)
553 /* Step the game state. */
555 st_timer(0.001f * (t1 - t0));
561 st_paint(0.001f * t0);
565 if (config_get_d(CONFIG_NICE))
573 SDL_JoystickClose(joy);
584 /*---------------------------------------------------------------------------*/