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 void shot(void)
47 static char filename[MAXSTR];
49 sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
53 /*---------------------------------------------------------------------------*/
55 static void toggle_wire(void)
61 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
62 glEnable(GL_TEXTURE_2D);
63 glEnable(GL_LIGHTING);
68 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
69 glDisable(GL_TEXTURE_2D);
70 glDisable(GL_LIGHTING);
75 /*---------------------------------------------------------------------------*/
83 /* Process SDL events. */
85 while (d && SDL_PollEvent(&e))
94 -e.motion.y + config_get_d(CONFIG_HEIGHT),
96 config_get_d(CONFIG_MOUSE_INVERT)
97 ? +e.motion.yrel : -e.motion.yrel);
100 case SDL_MOUSEBUTTONDOWN:
101 d = st_click(e.button.button, 1);
104 case SDL_MOUSEBUTTONUP:
105 d = st_click(e.button.button, 0);
110 c = e.key.keysym.sym;
112 if (config_tst_d(CONFIG_KEY_FORWARD, c))
113 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
115 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
116 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
118 else if (config_tst_d(CONFIG_KEY_LEFT, c))
119 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
121 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
122 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
126 case SDLK_F10: shot(); break;
127 case SDLK_F9: config_tgl_d(CONFIG_FPS); break;
128 case SDLK_F8: config_tgl_d(CONFIG_NICE); break;
136 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
139 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
143 if (SDL_EnableUNICODE(-1))
144 d = st_keybd(e.key.keysym.unicode, 1);
146 d = st_keybd(e.key.keysym.sym, 1);
153 c = e.key.keysym.sym;
155 if (config_tst_d(CONFIG_KEY_FORWARD, c))
156 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
158 else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
159 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
161 else if (config_tst_d(CONFIG_KEY_LEFT, c))
162 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
164 else if (config_tst_d(CONFIG_KEY_RIGHT, c))
165 st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
170 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
173 d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
177 d = st_keybd(e.key.keysym.sym, 0);
180 case SDL_ACTIVEEVENT:
181 if (e.active.state == SDL_APPINPUTFOCUS)
182 if (e.active.gain == 0 && video_get_grab())
183 goto_state(&st_pause);
186 case SDL_JOYAXISMOTION:
187 st_stick(e.jaxis.axis, e.jaxis.value);
190 case SDL_JOYBUTTONDOWN:
191 d = st_buttn(e.jbutton.button, 1);
194 case SDL_JOYBUTTONUP:
195 d = st_buttn(e.jbutton.button, 0);
200 /* Process events via the tilt sensor API. */
207 st_angle((int) tilt_get_x(),
210 while (tilt_get_button(&b, &s))
212 const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
213 const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
214 const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
215 const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
216 const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
217 const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
219 if (b == L || b == R || b == U || b == D)
221 static int pad[4] = { 0, 0, 0, 0 };
223 /* Track the state of the D-pad buttons. */
225 if (b == L) pad[0] = s;
226 else if (b == R) pad[1] = s;
227 else if (b == U) pad[2] = s;
228 else if (b == D) pad[3] = s;
230 /* Convert D-pad button events into joystick axis motion. */
232 if (pad[0] && !pad[1]) st_stick(X, -JOY_MAX);
233 else if (pad[1] && !pad[0]) st_stick(X, +JOY_MAX);
236 if (pad[2] && !pad[3]) st_stick(Y, -JOY_MAX);
237 else if (pad[3] && !pad[2]) st_stick(Y, +JOY_MAX);
240 else d = st_buttn(b, s);
247 /*---------------------------------------------------------------------------*/
249 static char *opt_data;
250 static char *opt_replay;
254 "Usage: %s [options ...]\n" \
256 " -h, --help show this usage message.\n" \
257 " -v, --version show version.\n" \
258 " -d, --data <dir> use 'dir' as game data directory.\n" \
259 " -r, --replay <file> play the replay 'file'.\n" \
262 #define opt_error(option) \
263 fprintf(stderr, L_("Option '%s' requires an argument.\n"), option)
265 static void opt_parse(int argc, char **argv)
269 /* Scan argument list. */
271 for (i = 1; i < argc; i++)
273 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
275 printf(opt_usage, argv[0]);
279 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
281 printf("%s\n", VERSION);
285 if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data") == 0)
292 opt_data = argv[++i];
296 if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay") == 0)
303 opt_replay = argv[++i];
307 /* Assume a single unrecognized argument is a replay name. */
311 opt_replay = argv[i];
320 /*---------------------------------------------------------------------------*/
322 static int is_replay(struct dir_item *item)
324 return str_ends_with(item->path, ".nbr");
327 static int is_score_file(struct dir_item *item)
329 return str_starts_with(item->path, "neverballhs-");
332 static void make_dirs_and_migrate(void)
340 if (fs_mkdir("Replays"))
342 if ((items = fs_dir_scan("", is_replay)))
344 for (i = 0; i < array_len(items); i++)
346 src = DIR_ITEM_GET(items, i)->path;
347 dst = concat_string("Replays/", src, NULL);
356 if (fs_mkdir("Scores"))
358 if ((items = fs_dir_scan("", is_score_file)))
360 for (i = 0; i < array_len(items); i++)
362 src = DIR_ITEM_GET(items, i)->path;
363 dst = concat_string("Scores/",
364 src + sizeof ("neverballhs-") - 1,
375 fs_mkdir("Screenshots");
378 /*---------------------------------------------------------------------------*/
380 int main(int argc, char *argv[])
382 SDL_Joystick *joy = NULL;
385 if (!fs_init(argv[0]))
387 fprintf(stderr, "Failure to initialize virtual file system: %s\n",
392 lang_init("neverball");
394 opt_parse(argc, argv);
396 config_paths(opt_data);
397 make_dirs_and_migrate();
399 /* Initialize SDL. */
401 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
403 fprintf(stderr, "%s\n", SDL_GetError());
407 /* Intitialize configuration. */
412 /* Initialize joystick. */
414 if (config_get_d(CONFIG_JOYSTICK) && SDL_NumJoysticks() > 0)
416 joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
418 SDL_JoystickEventState(SDL_ENABLE);
421 /* Initialize audio. */
426 /* Initialize video. */
428 if (!video_init(TITLE, ICON))
431 init_state(&st_null);
433 /* Initialize demo playback. */
436 fs_add_path(dir_name(opt_replay)) &&
437 progress_replay(base_name(opt_replay)))
440 goto_state(&st_demo_play);
443 goto_state(&st_title);
445 /* Run the main game loop. */
451 if ((t1 = SDL_GetTicks()) > t0)
453 /* Step the game state. */
455 st_timer(0.001f * (t1 - t0));
461 st_paint(0.001f * t0);
464 if (config_get_d(CONFIG_NICE))
472 SDL_JoystickClose(joy);
480 /*---------------------------------------------------------------------------*/