Streamline the archive loading mechanism
[tennix.git] / tennix.cc
blob8b730a2c17edc4e50d4a69d5ca63f9215127923b
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <time.h>
26 #include <libgen.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #ifdef WIN32
31 #include <windows.h>
32 #endif
34 #include "tennix.h"
35 #include "archive.hh"
36 #include "game.h"
37 #include "graphics.h"
38 #include "sound.h"
39 #include "input.h"
40 #include "network.h"
41 #include "util.h"
42 #include "animation.h"
44 #include "locations.h"
46 SDL_Surface *screen;
48 #ifdef WIN32
50 /* IDs from the resource file */
51 #define START_BUTTON 1
52 #define CHECKBOX_FULLSCREEN 2
53 #define QUIT_BUTTON 3
55 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
57 static int checkbox_is_checked;
59 switch (uMsg) {
60 case WM_CLOSE:
61 EndDialog(hwndDlg, IDCANCEL);
62 break;
63 case WM_COMMAND:
64 switch (wParam) {
65 case START_BUTTON:
66 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
67 break;
68 case QUIT_BUTTON:
69 EndDialog(hwndDlg, IDCANCEL);
70 break;
71 case CHECKBOX_FULLSCREEN:
72 checkbox_is_checked ^= 1;
73 break;
75 break;
76 default:
77 return FALSE;
79 return TRUE;
81 #endif
83 #ifdef WIN32
84 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
85 LPSTR lpCmdLine, int nCmdShow) {
86 #else
87 int main( int argc, char** argv) {
88 #endif
89 int i, slide, slide_direction;
90 unsigned int x;
91 Uint32 ticks;
92 int mx, my;
93 unsigned int worldmap_xpos, worldmap_ypos;
94 Uint8 *keys;
95 Uint8 mb;
96 SDL_Event e;
97 int sdl_flags = SDL_SWSURFACE;
98 int btn_hovering = 0, btn_hovering_old = 0;
99 int slide_start;
100 bool mouse_pressed = false;
101 bool quit = false;
102 bool benchmark = false;
103 unsigned int night_start, night_end;
104 GameState *current_game = NULL, *prepared_game = NULL;
105 InputDevice* input_devices;
106 unsigned int input_device_count, input_device_id;
107 Animation *intro;
108 AnimationState *intro_playback;
109 float wiggle;
110 TennixNet* connection = NULL;
111 char* net_host = NULL;
112 bool net_master = false;
113 TennixArchive* tnxar = NULL;
115 MenuButton btn_back = {
116 NULL, /* not needed for image-based button */
117 MENU_OPTIONS_BORDER,
118 HEIGHT-MENU_OPTIONS_BORDER,
119 0, -1, /* width and height will be set by menu_button_init */
120 255, 0, 0,
121 GR_BACK
123 MenuButton btn_start = {
124 NULL, /* not needed for image-based button */
125 WIDTH-MENU_OPTIONS_BORDER,
126 HEIGHT-MENU_OPTIONS_BORDER,
127 -1, -1, /* width and height will be set by menu_button_init */
128 0, 255, 0,
129 GR_FORWARD
131 MenuButton btn_player1 = {
132 NULL,
133 CONTROLLER_SETUP_BORDER,
134 HEIGHT/2 + CONTROLLER_SETUP_SIZE/2 + 10,
135 CONTROLLER_SETUP_SIZE, MENU_OPTIONS_BUTTON_HEIGHT,
136 50, 50, 255,
137 GR_COUNT
139 MenuButton btn_player2 = {
140 NULL,
141 WIDTH-CONTROLLER_SETUP_SIZE-CONTROLLER_SETUP_BORDER,
142 HEIGHT/2 + CONTROLLER_SETUP_SIZE/2 + 10,
143 CONTROLLER_SETUP_SIZE, MENU_OPTIONS_BUTTON_HEIGHT,
144 255, 50, 50,
145 GR_COUNT
148 int highlight_location = -1;
149 float highlight_location_distance = 0.0;
150 float new_location_distance = 0.0;
151 bool location_info_visible = false;
152 unsigned int location_info_xpos = 0, location_info_ypos = 0;
154 const SDL_VideoInfo* vi = NULL;
156 bool do_help = false;
158 int state = MENU_STATE_STARTED;
160 #ifdef ENABLE_FPS_LIMIT
161 Uint32 ft, frames; /* frame timer and frames */
162 #endif
164 #if defined(MAEMO) || defined(MACOSX)
165 sdl_flags |= SDL_FULLSCREEN;
166 #endif
168 #ifdef WIN32
169 int mb_result;
170 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
172 switch (mb_result) {
173 case IDYES:
174 sdl_flags |= SDL_FULLSCREEN;
175 break;
176 case IDCANCEL:
177 return 0;
178 break;
179 default:
180 break;
182 #else
183 fprintf(stderr, "Tennix 2009 World Tennis Championship Tour (v" VERSION ")\n" COPYRIGHT "\n" URL "\n\n");
185 i = 1;
186 while (i < argc) {
187 /* A poor/lazy man's getopt */
188 #define OPTION_SET(longopt,shortopt) \
189 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
190 #define OPTION_VALUE \
191 ((i+1 < argc)?(argv[i+1]):(NULL))
192 #define OPTION_VALUE_PROCESSED \
193 (i++)
194 if (OPTION_SET("--fullscreen", "-f")) {
195 sdl_flags |= SDL_FULLSCREEN;
197 else if (OPTION_SET("--help", "-h")) {
198 do_help = true;
200 else if (OPTION_SET("--benchmark", "-b")) {
201 benchmark = true;
203 else if (OPTION_SET("--slave", "-s")) {
204 net_host = OPTION_VALUE;
205 assert(OPTION_VALUE != NULL);
206 OPTION_VALUE_PROCESSED;
207 net_master = false;
209 else if (OPTION_SET("--master", "-m")) {
210 net_host = OPTION_VALUE;
211 assert(OPTION_VALUE != NULL);
212 OPTION_VALUE_PROCESSED;
213 net_master = true;
215 else {
216 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
218 i++;
221 if (do_help == true) {
222 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h]\n", argv[0]);
223 return 0;
225 #endif
227 if (benchmark) {
228 srand(100);
229 } else {
230 srand((unsigned)time(NULL));
233 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
234 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
235 exit( 1);
238 vi = SDL_GetVideoInfo();
239 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
240 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
241 exit( 1);
244 SDL_WM_SetCaption( "Tennix 2009 World Tennis Championship Tour", "Tennix 2009");
245 SDL_ShowCursor( SDL_DISABLE);
246 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
248 tnxar = new TennixArchive(ARCHIVE_FILE, ARCHIVE_FILE_INSTALLED);
249 init_graphics(*tnxar);
250 init_sound(*tnxar);
251 init_input(*tnxar);
252 init_network();
253 delete tnxar;
255 if (net_host != NULL) {
256 connection = network_connect(net_host, net_master);
259 menu_button_init(&btn_back);
260 menu_button_init(&btn_start);
261 menu_button_init(&btn_player1);
262 menu_button_init(&btn_player2);
264 input_devices = find_input_devices(&input_device_count);
266 current_game = gamestate_load(GAMESTATE_FILE);
267 if (current_game != NULL) {
268 /* restore pointer to location */
269 current_game->location = &(locations[current_game->current_location]);
270 /* */
271 for (i=1; i<=MAXPLAYERS; i++) {
272 if (PLAYER(current_game, i).type == PLAYER_TYPE_HUMAN) {
273 input_device_id = PLAYER(current_game, i).input_device_index;
274 if (input_device_id < input_device_count) {
275 /* ok, we still have that device around */
276 PLAYER(current_game, i).input = &(input_devices[input_device_id]);
277 } else {
278 /* the device vanished - set to AI (FIXME: select new device) */
279 PLAYER(current_game, i).type = PLAYER_TYPE_AI;
280 PLAYER(current_game, i).input = NULL;
286 #ifdef ENABLE_FPS_LIMIT
287 frames = 0;
288 ft = SDL_GetTicks();
289 #endif
291 if (benchmark) {
292 GameState* g = gamestate_new();
293 PLAYER(g, 1).type = PLAYER_TYPE_AI;
294 PLAYER(g, 2).type = PLAYER_TYPE_AI;
295 g->location = &(locations[0]);
296 gameloop(g, connection);
297 free(g);
298 exit(0);
301 /*intro = create_intro();
302 intro_playback = animation_state_new(intro);
303 animation_state_run(intro_playback, 1);
304 animation_state_free(intro_playback);
305 animation_free(intro);
306 start_fade();*/
308 worldmap_xpos = (WIDTH-get_image_width(GR_WORLDMAP))/2;
309 worldmap_ypos = (HEIGHT-get_image_height(GR_WORLDMAP))/2;
311 i = 0;
312 /* Sliding initialization */
313 ticks = SDL_GetTicks();
314 slide = slide_start = get_image_width(GR_SIDEBAR);
315 slide_direction = 0;
316 while(!quit) {
317 /* State transitions */
318 switch (state) {
319 case MENU_STATE_STARTED:
320 state = MENU_STATE_SLIDE_TO_MAINMENU;
321 break;
322 case MENU_STATE_SLIDE_TO_MAINMENU:
323 clear_screen();
324 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
325 store_screen();
326 slide = slide_start;
327 slide_direction = -1;
328 state = MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS;
329 break;
330 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS:
331 if (slide == 0) {
332 slide_direction = 0;
333 state = MENU_STATE_MAINMENU;
335 break;
336 case MENU_STATE_MAINMENU:
337 free(prepared_game);
338 prepared_game = NULL;
339 break;
340 case MENU_STATE_SLIDE_TO_LOCATION:
341 slide = 1;
342 slide_direction = 3;
343 state = MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS;
344 break;
345 case MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS:
346 if (slide == slide_start) {
347 state = MENU_STATE_FADE_TO_LOCATION;
349 break;
350 case MENU_STATE_FADE_TO_LOCATION:
351 start_fade();
352 state = MENU_STATE_LOCATION;
353 clear_screen();
355 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
356 /* Draw and store the worldmap with day/night times */
357 show_image(GR_WORLDMAP, WIDTH/2-get_image_width(GR_WORLDMAP)/2, HEIGHT/2-get_image_height(GR_WORLDMAP)/2, 255);
358 day_night(get_image_width(GR_WORLDMAP), &night_start, &night_end);
359 if (night_start > night_end) {
360 rectangle_alpha(worldmap_xpos, worldmap_ypos, night_end, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
361 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, get_image_width(GR_WORLDMAP)-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
362 } else {
363 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, night_end-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
366 /* add misc items to screen */
367 font_draw_string(FONT_XLARGE, "Pick a location", (WIDTH-font_get_string_width(FONT_XLARGE, "Pick a location"))/2, 20);
369 store_screen();
370 break;
371 case MENU_STATE_FADE_TO_OPTIONS:
372 start_fade();
373 clear_screen();
374 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
375 rectangle(CONTROLLER_SETUP_BORDER, HEIGHT/2-CONTROLLER_SETUP_SIZE/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
376 rectangle(WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE, (HEIGHT-CONTROLLER_SETUP_SIZE)/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
377 font_draw_string(FONT_XLARGE, "Controller setup", (WIDTH-font_get_string_width(FONT_XLARGE, "Controller setup"))/2, 20);
378 font_draw_string(FONT_MEDIUM, "Player 1", 130 - font_get_string_width(FONT_MEDIUM, "Player 1")/2, (HEIGHT-CONTROLLER_SETUP_SIZE)/2 - 10 - font_get_height(FONT_MEDIUM));
379 font_draw_string(FONT_MEDIUM, "Player 1", 130 - font_get_string_width(FONT_MEDIUM, "Player 1")/2, (HEIGHT-CONTROLLER_SETUP_SIZE)/2 - 10 - font_get_height(FONT_MEDIUM));
380 font_draw_string(FONT_MEDIUM, "Player 2", WIDTH - CONTROLLER_SETUP_SIZE/2 - CONTROLLER_SETUP_BORDER - font_get_string_width(FONT_MEDIUM, "Player 2")/2, (HEIGHT-CONTROLLER_SETUP_SIZE)/2 - 10 - font_get_height(FONT_MEDIUM));
381 store_screen();
382 start_fade();
383 state = MENU_STATE_OPTIONS;
384 break;
385 case MENU_STATE_LOCATION:
386 case MENU_STATE_OPTIONS:
387 /* Prepare a new game */
388 if (prepared_game == NULL) {
389 prepared_game = gamestate_new();
390 prepared_game->location = NULL;
391 /* FIXME - this should not be written here, but taken from the input devices */
392 btn_player1.text = "Keyboard (arrows)";
393 btn_player2.text = "Carl Van Court";
394 #ifdef MAEMO
395 PLAYER(prepared_game, 1).input_device_index = 0;
396 #else
397 PLAYER(prepared_game, 1).input_device_index = 2;
398 #endif
399 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
400 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
401 location_info_visible = false;
402 prepared_game->current_location = -1;
403 highlight_location = -1;
405 break;
406 case MENU_STATE_SLIDE_TO_GAME:
407 /*slide = 1;
408 slide_direction = 2;
409 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
410 break;
411 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
412 if (slide == slide_start) {
413 state = MENU_STATE_GAME;
415 state = MENU_STATE_GAME;
416 break;
417 case MENU_STATE_SLIDE_TO_RESUME:
418 slide = 1;
419 slide_direction = 2;
420 state = MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS;
421 break;
422 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS:
423 if (slide == slide_start) {
424 state = MENU_STATE_RESUME;
426 break;
427 case MENU_STATE_GAME:
428 if (prepared_game == NULL) {
429 fprintf(stderr, "Game not yet prepared!\n");
430 exit(EXIT_FAILURE);
433 /* Cancel a possibly started game */
434 free(current_game);
435 current_game = prepared_game;
436 prepared_game = NULL;
437 /* no break - we are continuing with "resume" */
438 case MENU_STATE_RESUME:
439 if (current_game == NULL) {
440 fprintf(stderr, "Cannot resume game!\n");
441 exit(EXIT_FAILURE);
443 start_fade();
444 gameloop(current_game, connection);
445 SDL_Delay(150);
446 while(SDL_PollEvent(&e));
447 #ifdef ENABLE_FPS_LIMIT
448 frames = 0;
449 ft = SDL_GetTicks();
450 #endif
451 start_fade();
452 state = MENU_STATE_SLIDE_TO_MAINMENU;
453 break;
454 case MENU_STATE_SLIDE_TO_QUIT:
455 slide = 1;
456 slide_direction = 3;
457 state = MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS;
458 break;
459 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS:
460 if (slide == slide_start) {
461 state = MENU_STATE_QUIT;
463 break;
464 case MENU_STATE_QUIT:
465 quit = true;
466 break;
467 default:
468 fprintf(stderr, "State error: %d\n", state);
469 exit(EXIT_FAILURE);
472 /* Sliding */
473 if (SDL_GetTicks() > ticks + 20) {
474 if (slide >= 1 && slide <= slide_start) {
475 slide += slide_direction+(slide_direction*slide/(sqrt(2*slide)));
476 slide = MAX(0, MIN(slide_start, slide));
477 } else if (slide_direction != 0) {
478 slide_direction = 0;
480 ticks = SDL_GetTicks();
483 /* Graphics */
484 #ifdef DEBUG
485 if (state != MENU_STATE_OPTIONS) {
486 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -i, 0);
488 #endif
489 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
490 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20-slide, 255);
491 if (state != MENU_STATE_OPTIONS && state != MENU_STATE_LOCATION) {
492 /* Main Menu */
493 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+(slide/7)+3-(3*(btn_hovering==MENU_START)), 150, 255);
494 if (current_game != NULL) {
495 show_image(GR_BTN_RESUME, WIDTH-get_image_width(GR_BTN_RESUME)+slide+(slide/7)+3-(3*(btn_hovering==MENU_RESUME)), 230, 255);
496 font_draw_string(FONT_SMALL, "match paused", 10, 10);
497 } else {
498 font_draw_string(FONT_MEDIUM, "Tennix 2009 World Tennis Championship Tour", 10, 10);
500 font_draw_string_color(FONT_MEDIUM, URL, 10-1, HEIGHT-10-1-font_get_height(FONT_MEDIUM), 130, 130, 130);
501 font_draw_string_color(FONT_MEDIUM, URL, 10, HEIGHT-10-font_get_height(FONT_MEDIUM), 30, 30, 30);
502 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+(slide/7)+3-(3*(btn_hovering==MENU_QUIT)), 350, 255);
503 } else if (state == MENU_STATE_OPTIONS) {
504 /* Options screen */
505 draw_button_object(&btn_back, mx, my);
506 draw_button_object(&btn_start, mx, my);
507 draw_button_object(&btn_player1, mx, my);
508 draw_button_object(&btn_player2, mx, my);
509 wiggle = 15*sinf((float)ticks/300.);
510 if (PLAYER(prepared_game, 1).input_device_index > -1) {
511 show_image_rotozoom(input_devices[PLAYER(prepared_game, 1).input_device_index].icon, CONTROLLER_SETUP_BORDER + CONTROLLER_SETUP_SIZE/2, HEIGHT/2, wiggle, 1.0);
512 } else {
513 show_image_rotozoom(GR_INPUT_AI, CONTROLLER_SETUP_BORDER + CONTROLLER_SETUP_SIZE/2, HEIGHT/2, wiggle, 1.0);
515 if (PLAYER(prepared_game, 2).input_device_index > -1) {
516 show_image_rotozoom(input_devices[PLAYER(prepared_game, 2).input_device_index].icon, WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE/2, HEIGHT/2, -wiggle, 1.0);
517 } else {
518 show_image_rotozoom(GR_INPUT_AI, WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE/2, HEIGHT/2, -wiggle, 1.0);
520 } else if (state == MENU_STATE_LOCATION) {
521 /* Location selection screen */
522 for (x=0; x<location_count(); x++) {
523 new_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[x].worldmap_x,
524 my-worldmap_ypos-locations[x].worldmap_y);
525 if (highlight_location == -1) {
526 if (new_location_distance < 20*20) {
527 highlight_location = x;
529 } else {
530 highlight_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[highlight_location].worldmap_x,
531 my-worldmap_ypos-locations[highlight_location].worldmap_y);
532 if (highlight_location_distance > 20*20) {
533 highlight_location = -1;
535 if (highlight_location_distance > new_location_distance && new_location_distance < 20*20) {
536 highlight_location = x;
540 if (prepared_game != NULL) {
541 if (!location_info_visible) {
542 for (x=0; x<location_count(); x++) {
543 /* draw rectangle for location at "x"*/
544 if (highlight_location != -1 && (unsigned int)highlight_location == x) {
545 rectangle(worldmap_xpos + locations[x].worldmap_x-3, worldmap_ypos + locations[x].worldmap_y-3, 7, 7, 255*((i/10)%2), 0, 255*((i/10)%2));
548 rectangle(worldmap_xpos + locations[x].worldmap_x-2, worldmap_ypos + locations[x].worldmap_y-2, 5, 5, 255, 255*(prepared_game->current_location != -1 && x==(unsigned int)(prepared_game->current_location)), 0);
550 } else {
551 rectangle_alpha(location_info_xpos-5, location_info_ypos-5, 10+get_image_width(prepared_game->location->photo), get_image_height(prepared_game->location->photo)+100, 30, 30, 30, 200);
552 show_sprite(prepared_game->location->photo, (i/1000)%(prepared_game->location->photo_frames), prepared_game->location->photo_frames, location_info_xpos, location_info_ypos, 255);
553 font_draw_string(FONT_SMALL, prepared_game->location->name, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200);
554 font_draw_string(FONT_SMALL, prepared_game->location->area, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200+font_get_height(FONT_SMALL));
555 font_draw_string(FONT_SMALL, prepared_game->location->city, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200+2*font_get_height(FONT_SMALL));
556 font_draw_string(FONT_SMALL, prepared_game->location->court_type_name, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200+3*font_get_height(FONT_SMALL));
558 if (prepared_game->location != NULL) {
559 draw_button_object(&btn_start, mx, my);
562 draw_button_object(&btn_back, mx, my);
565 SDL_PollEvent( &e);
566 if( e.type == SDL_QUIT) {
567 state = MENU_STATE_SLIDE_TO_QUIT;
568 /*break;*/
571 keys = SDL_GetKeyState( NULL);
572 mb = SDL_GetMouseState( &mx, &my);
574 btn_hovering_old = btn_hovering;
575 if (state == MENU_STATE_MAINMENU) {
576 btn_hovering = M_POS_DECODE(mx, my);
577 if (current_game == NULL) {
578 btn_hovering &= ~MENU_RESUME;
580 } else if (state == MENU_STATE_LOCATION) {
581 if (M_POS_BUTTON(btn_back, mx, my)) {
582 btn_hovering = MENU_QUIT;
583 } else if (M_POS_BUTTON(btn_start, mx, my)) {
584 btn_hovering = MENU_START;
585 } else {
586 btn_hovering = 0;
588 } else if (state == MENU_STATE_OPTIONS) {
589 if (M_POS_BUTTON(btn_back, mx, my)) {
590 btn_hovering = MENU_QUIT;
591 } else if (M_POS_BUTTON(btn_start, mx, my)) {
592 btn_hovering = MENU_START;
593 } else if (M_POS_BUTTON(btn_player1, mx, my)) {
594 btn_hovering = MENU_PLAYER1;
595 } else if (M_POS_BUTTON(btn_player2, mx, my)) {
596 btn_hovering = MENU_PLAYER2;
597 } else {
598 btn_hovering = 0;
600 } else {
601 /* No menu screen - no hovering. */
602 btn_hovering = 0;
604 #ifndef MAEMO /* On Maemo, we cannot really "hover" (touchscreen!) */
605 if (btn_hovering_old != btn_hovering && btn_hovering != 0) {
606 #ifdef HAVE_VOICE_FILES
607 if (btn_hovering == MENU_QUIT) {
608 play_sample(VOICE_QUIT_IT);
609 } else if (btn_hovering == MENU_START) {
610 play_sample(VOICE_NEW_GAME);
611 } else {
612 play_sample(SOUND_MOUSEOVER);
614 #else
615 /*play_sample(SOUND_MOUSEOVER);*/
616 #endif
618 #endif
620 if( keys[SDLK_ESCAPE] || keys['q']) {
621 /* FIXME: do the state thingie! */
622 break;
625 if( keys['f']) {
626 SDL_WM_ToggleFullScreen( screen);
629 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
630 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
631 show_image(GR_CURSOR, mx-2, my-1, 255);
633 #endif
635 /* Draw the "real" mouse coordinates */
636 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
638 /* Store the screen, because we are fading after this screen update */
639 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
641 updatescr();
643 if( mb & SDL_BUTTON(SDL_BUTTON_LEFT)) {
644 if (!mouse_pressed) {
645 play_sample(SOUND_MOUSEOVER);
647 mouse_pressed = true;
648 } else if (mouse_pressed == true) {
649 /* Mouse button released */
650 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
651 #ifdef HAVE_VOICE_FILES
652 if (btn_hovering == MENU_START) {
653 play_sample(VOICE_LETS_GO);
654 } else {
655 play_sample(SOUND_MOUSECLICK);
657 #else
658 /*play_sample(SOUND_MOUSEOVER);*/
659 #endif
661 if (state == MENU_STATE_MAINMENU) {
662 switch (btn_hovering) {
663 case MENU_START:
664 state = MENU_STATE_SLIDE_TO_LOCATION;
665 break;
666 case MENU_RESUME:
667 state = MENU_STATE_SLIDE_TO_RESUME;
668 break;
669 case MENU_QUIT:
670 state = MENU_STATE_SLIDE_TO_QUIT;
671 break;
673 } else if (state == MENU_STATE_LOCATION) {
674 switch (btn_hovering) {
675 case MENU_START:
676 if (prepared_game->location != NULL) {
677 state = MENU_STATE_FADE_TO_OPTIONS;
679 break;
680 case MENU_QUIT:
681 state = MENU_STATE_SLIDE_TO_MAINMENU;
682 break;
683 default:
684 if (!location_info_visible && highlight_location != -1) {
685 prepared_game->current_location = highlight_location;
686 /* Set the day/night status */
687 if (night_start < night_end) {
688 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x > night_start && locations[prepared_game->current_location].worldmap_x < night_end);
689 } else {
690 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x < night_end || locations[prepared_game->current_location].worldmap_x > night_start);
692 prepared_game->location = &(locations[prepared_game->current_location]);
693 location_info_xpos = MAX(0, MIN(WIDTH-320-50, mx-320/2));
694 location_info_ypos = MAX(0, MIN(HEIGHT-200-160, my-200/2));
695 location_info_visible = true;
696 } else {
697 location_info_visible = false;
698 highlight_location = -1;
699 prepared_game->current_location = -1;
700 prepared_game->location = NULL;
702 break;
704 } else if (state == MENU_STATE_OPTIONS) {
705 switch (btn_hovering) {
706 case MENU_START:
707 state = MENU_STATE_SLIDE_TO_GAME;
708 break;
709 case MENU_QUIT:
710 state = MENU_STATE_FADE_TO_LOCATION;
711 break;
712 case MENU_PLAYER1:
713 /* advance the input device index */
714 PLAYER(prepared_game, 1).input_device_index++;
715 if (PLAYER(prepared_game, 1).input_device_index == (signed int)input_device_count) {
716 PLAYER(prepared_game, 1).input_device_index = -1;
719 if (input_devices[PLAYER(prepared_game, 1).input_device_index].exclusive_to_player == 2) {
720 PLAYER(prepared_game, 1).input_device_index++;
723 /* determine the selected input device */
724 if (PLAYER(prepared_game, 1).input_device_index == -1) {
725 PLAYER(prepared_game, 1).type = PLAYER_TYPE_AI;
726 PLAYER(prepared_game, 1).input = NULL;
727 btn_player1.text = "Carl Van Court";
728 } else {
729 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
730 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
731 btn_player1.text = input_device_get_name(PLAYER(prepared_game, 1).input);
733 break;
734 case MENU_PLAYER2:
735 /* advance the input device index */
736 PLAYER(prepared_game, 2).input_device_index++;
738 if (input_devices[PLAYER(prepared_game, 2).input_device_index].exclusive_to_player == 1) {
739 PLAYER(prepared_game, 2).input_device_index++;
742 if (PLAYER(prepared_game, 2).input_device_index == (signed int)input_device_count) {
743 PLAYER(prepared_game, 2).input_device_index = -1;
746 /* determine the selected input device */
747 if (PLAYER(prepared_game, 2).input_device_index == -1) {
748 PLAYER(prepared_game, 2).type = PLAYER_TYPE_AI;
749 PLAYER(prepared_game, 2).input = NULL;
750 btn_player2.text = "Carl Van Court";
751 } else {
752 PLAYER(prepared_game, 2).type = PLAYER_TYPE_HUMAN;
753 PLAYER(prepared_game, 2).input = &(input_devices[PLAYER(prepared_game, 2).input_device_index]);
754 btn_player2.text = input_device_get_name(PLAYER(prepared_game, 2).input);
756 break;
759 mouse_pressed = false;
761 i++;
762 #ifdef ENABLE_FPS_LIMIT
763 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
764 SDL_Delay(10);
766 frames++;
767 #endif
770 if (current_game != NULL) {
771 if (gamestate_save(current_game, GAMESTATE_FILE) != 0) {
772 fprintf(stderr, "Warning: cannot save gamestate to %s\n", GAMESTATE_FILE);
776 /* Play the credits */
777 /*intro = create_credits();
778 intro_playback = animation_state_new(intro);
779 animation_state_run(intro_playback, 1);
780 animation_state_free(intro_playback);
781 animation_free(intro);*/
783 uninit_graphics();
784 uninit_input();
785 uninit_network();
787 SDL_Quit();
788 return 0;
792 void menu_button_init(MenuButton* b)
794 int w, h;
796 if (b->image_id != GR_COUNT) {
798 * If the button is an image, the "w" and "h" attributes of the
799 * MenuButton struct are simply factors with which the real width
800 * and height of the image (=button) should be multiplied and added
801 * to the "x" and "y" position and the "w" and "h" are replaced with
802 * the real size of the image for the collision detection
804 w = b->w;
805 h = b->h;
807 b->w = get_image_width(b->image_id);
808 b->h = get_image_height(b->image_id);
809 b->x += w*b->w;
810 b->y += h*b->h;