Move weather settings to Location from GameState
[tennix.git] / tennix.c
blob10cf5a5266943b810af129ebf99ba1f1a54e54e7
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 "game.h"
36 #include "graphics.h"
37 #include "sound.h"
38 #include "input.h"
39 #include "util.h"
40 #include "animation.h"
42 #include "locations.h"
44 SDL_Surface *screen;
46 #ifdef WIN32
48 /* IDs from the resource file */
49 #define START_BUTTON 1
50 #define CHECKBOX_FULLSCREEN 2
51 #define QUIT_BUTTON 3
53 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
55 static int checkbox_is_checked;
57 switch (uMsg) {
58 case WM_CLOSE:
59 EndDialog(hwndDlg, IDCANCEL);
60 break;
61 case WM_COMMAND:
62 switch (wParam) {
63 case START_BUTTON:
64 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
65 break;
66 case QUIT_BUTTON:
67 EndDialog(hwndDlg, IDCANCEL);
68 break;
69 case CHECKBOX_FULLSCREEN:
70 checkbox_is_checked ^= 1;
71 break;
73 break;
74 default:
75 return FALSE;
77 return TRUE;
79 #endif
81 #ifdef WIN32
82 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
83 LPSTR lpCmdLine, int nCmdShow) {
84 #else
85 int main( int argc, char** argv) {
86 #endif
87 int i, slide, slide_direction;
88 unsigned int x;
89 Uint32 ticks;
90 int mx, my;
91 unsigned int worldmap_xpos, worldmap_ypos;
92 Uint8 *keys;
93 Uint8 mb;
94 SDL_Event e;
95 int sdl_flags = SDL_SWSURFACE;
96 int btn_hovering = 0, btn_hovering_old = 0;
97 int slide_start;
98 bool mouse_pressed = false;
99 bool quit = false;
100 bool benchmark = false;
101 unsigned int night_start, night_end;
102 GameState *current_game = NULL, *prepared_game = NULL;
103 InputDevice* input_devices;
104 unsigned int input_device_count, input_device_id;
105 Animation *intro;
106 AnimationState *intro_playback;
107 float wiggle;
109 MenuButton btn_back = {
110 NULL, /* not needed for image-based button */
111 MENU_OPTIONS_BORDER,
112 HEIGHT-MENU_OPTIONS_BORDER,
113 0, -1, /* width and height will be set by menu_button_init */
114 255, 0, 0,
115 GR_BACK
117 MenuButton btn_start = {
118 NULL, /* not needed for image-based button */
119 WIDTH-MENU_OPTIONS_BORDER,
120 HEIGHT-MENU_OPTIONS_BORDER,
121 -1, -1, /* width and height will be set by menu_button_init */
122 0, 255, 0,
123 GR_FORWARD
125 MenuButton btn_player1 = {
126 NULL,
127 CONTROLLER_SETUP_BORDER,
128 HEIGHT/2 + CONTROLLER_SETUP_SIZE/2 + 10,
129 CONTROLLER_SETUP_SIZE, MENU_OPTIONS_BUTTON_HEIGHT,
130 50, 50, 255,
131 GR_COUNT
133 MenuButton btn_player2 = {
134 NULL,
135 WIDTH-CONTROLLER_SETUP_SIZE-CONTROLLER_SETUP_BORDER,
136 HEIGHT/2 + CONTROLLER_SETUP_SIZE/2 + 10,
137 CONTROLLER_SETUP_SIZE, MENU_OPTIONS_BUTTON_HEIGHT,
138 255, 50, 50,
139 GR_COUNT
142 int highlight_location = -1;
143 float highlight_location_distance = 0.0;
144 float new_location_distance = 0.0;
145 bool location_info_visible = false;
146 unsigned int location_info_xpos = 0, location_info_ypos = 0;
148 const SDL_VideoInfo* vi = NULL;
150 bool do_help = false;
152 int state = MENU_STATE_STARTED;
154 #ifdef ENABLE_FPS_LIMIT
155 Uint32 ft, frames; /* frame timer and frames */
156 #endif
158 #if defined(MAEMO) || defined(MACOSX)
159 sdl_flags |= SDL_FULLSCREEN;
160 #endif
162 #ifdef WIN32
163 int mb_result;
164 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
166 switch (mb_result) {
167 case IDYES:
168 sdl_flags |= SDL_FULLSCREEN;
169 break;
170 case IDCANCEL:
171 return 0;
172 break;
173 default:
174 break;
176 #else
177 fprintf(stderr, "Tennix 2009 World Tennis Championship Tour (v" VERSION ")\n" COPYRIGHT "\n" URL "\n\n");
179 i = 1;
180 while (i < argc) {
181 /* A poor/lazy man's getopt */
182 #define OPTION_SET(longopt,shortopt) \
183 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
184 #define OPTION_VALUE \
185 ((i+1 < argc)?(argv[i+1]):(NULL))
186 #define OPTION_VALUE_PROCESSED \
187 (i++)
188 if (OPTION_SET("--fullscreen", "-f")) {
189 sdl_flags |= SDL_FULLSCREEN;
191 else if (OPTION_SET("--help", "-h")) {
192 do_help = true;
194 else if (OPTION_SET("--benchmark", "-b")) {
195 benchmark = true;
197 else {
198 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
200 i++;
203 if (do_help == true) {
204 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h]\n", argv[0]);
205 return 0;
207 #endif
209 if (benchmark) {
210 srand(100);
211 } else {
212 srand((unsigned)time(NULL));
215 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
216 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
217 exit( 1);
220 vi = SDL_GetVideoInfo();
221 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
222 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
223 exit( 1);
226 SDL_WM_SetCaption( "Tennix 2009 World Tennis Championship Tour", "Tennix 2009");
227 SDL_ShowCursor( SDL_DISABLE);
228 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
230 init_graphics();
231 init_sound();
232 init_input();
234 menu_button_init(&btn_back);
235 menu_button_init(&btn_start);
236 menu_button_init(&btn_player1);
237 menu_button_init(&btn_player2);
239 input_devices = find_input_devices(&input_device_count);
241 current_game = gamestate_load(GAMESTATE_FILE);
242 if (current_game != NULL) {
243 /* restore pointer to location */
244 current_game->location = &(locations[current_game->current_location]);
245 /* */
246 for (i=1; i<=MAXPLAYERS; i++) {
247 if (PLAYER(current_game, i).type == PLAYER_TYPE_HUMAN) {
248 input_device_id = PLAYER(current_game, i).input_device_index;
249 if (input_device_id < input_device_count) {
250 /* ok, we still have that device around */
251 PLAYER(current_game, i).input = &(input_devices[input_device_id]);
252 } else {
253 /* the device vanished - set to AI (FIXME: select new device) */
254 PLAYER(current_game, i).type = PLAYER_TYPE_AI;
255 PLAYER(current_game, i).input = NULL;
261 #ifdef ENABLE_FPS_LIMIT
262 frames = 0;
263 ft = SDL_GetTicks();
264 #endif
266 if (benchmark) {
267 GameState* g = gamestate_new();
268 PLAYER(g, 1).type = PLAYER_TYPE_AI;
269 PLAYER(g, 2).type = PLAYER_TYPE_AI;
270 g->location = &(locations[0]);
271 gameloop(g);
272 free(g);
273 exit(0);
276 /*intro = create_intro();
277 intro_playback = animation_state_new(intro);
278 animation_state_run(intro_playback, 1);
279 animation_state_free(intro_playback);
280 animation_free(intro);
281 start_fade();*/
283 worldmap_xpos = (WIDTH-get_image_width(GR_WORLDMAP))/2;
284 worldmap_ypos = (HEIGHT-get_image_height(GR_WORLDMAP))/2;
286 i = 0;
287 /* Sliding initialization */
288 ticks = SDL_GetTicks();
289 slide = slide_start = get_image_width(GR_SIDEBAR);
290 slide_direction = 0;
291 while(!quit) {
292 /* State transitions */
293 switch (state) {
294 case MENU_STATE_STARTED:
295 state = MENU_STATE_SLIDE_TO_MAINMENU;
296 break;
297 case MENU_STATE_SLIDE_TO_MAINMENU:
298 clear_screen();
299 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
300 store_screen();
301 slide = slide_start;
302 slide_direction = -1;
303 state = MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS;
304 break;
305 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS:
306 if (slide == 0) {
307 slide_direction = 0;
308 state = MENU_STATE_MAINMENU;
310 break;
311 case MENU_STATE_MAINMENU:
312 free(prepared_game);
313 prepared_game = NULL;
314 break;
315 case MENU_STATE_SLIDE_TO_LOCATION:
316 slide = 1;
317 slide_direction = 3;
318 state = MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS;
319 break;
320 case MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS:
321 if (slide == slide_start) {
322 state = MENU_STATE_FADE_TO_LOCATION;
324 break;
325 case MENU_STATE_FADE_TO_LOCATION:
326 start_fade();
327 state = MENU_STATE_LOCATION;
328 clear_screen();
330 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
331 /* Draw and store the worldmap with day/night times */
332 show_image(GR_WORLDMAP, WIDTH/2-get_image_width(GR_WORLDMAP)/2, HEIGHT/2-get_image_height(GR_WORLDMAP)/2, 255);
333 day_night(get_image_width(GR_WORLDMAP), &night_start, &night_end);
334 if (night_start > night_end) {
335 rectangle_alpha(worldmap_xpos, worldmap_ypos, night_end, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
336 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, get_image_width(GR_WORLDMAP)-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
337 } else {
338 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, night_end-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
341 /* add misc items to screen */
342 font_draw_string(FONT_XLARGE, "Pick a location", (WIDTH-font_get_string_width(FONT_XLARGE, "Pick a location"))/2, 20);
344 store_screen();
345 break;
346 case MENU_STATE_FADE_TO_OPTIONS:
347 start_fade();
348 clear_screen();
349 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
350 rectangle(CONTROLLER_SETUP_BORDER, HEIGHT/2-CONTROLLER_SETUP_SIZE/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
351 rectangle(WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE, (HEIGHT-CONTROLLER_SETUP_SIZE)/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
352 font_draw_string(FONT_XLARGE, "Controller setup", (WIDTH-font_get_string_width(FONT_XLARGE, "Controller setup"))/2, 20);
353 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));
354 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));
355 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));
356 store_screen();
357 start_fade();
358 state = MENU_STATE_OPTIONS;
359 break;
360 case MENU_STATE_LOCATION:
361 case MENU_STATE_OPTIONS:
362 /* Prepare a new game */
363 if (prepared_game == NULL) {
364 prepared_game = gamestate_new();
365 prepared_game->location = NULL;
366 /* FIXME - this should not be written here, but taken from the input devices */
367 btn_player1.text = "Keyboard (arrows)";
368 btn_player2.text = "Carl Van Court";
369 #ifdef MAEMO
370 PLAYER(prepared_game, 1).input_device_index = 0;
371 #else
372 PLAYER(prepared_game, 1).input_device_index = 2;
373 #endif
374 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
375 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
376 location_info_visible = false;
377 prepared_game->current_location = -1;
378 highlight_location = -1;
380 break;
381 case MENU_STATE_SLIDE_TO_GAME:
382 /*slide = 1;
383 slide_direction = 2;
384 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
385 break;
386 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
387 if (slide == slide_start) {
388 state = MENU_STATE_GAME;
390 state = MENU_STATE_GAME;
391 break;
392 case MENU_STATE_SLIDE_TO_RESUME:
393 slide = 1;
394 slide_direction = 2;
395 state = MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS;
396 break;
397 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS:
398 if (slide == slide_start) {
399 state = MENU_STATE_RESUME;
401 break;
402 case MENU_STATE_GAME:
403 if (prepared_game == NULL) {
404 fprintf(stderr, "Game not yet prepared!\n");
405 exit(EXIT_FAILURE);
408 /* Cancel a possibly started game */
409 free(current_game);
410 current_game = prepared_game;
411 prepared_game = NULL;
412 /* no break - we are continuing with "resume" */
413 case MENU_STATE_RESUME:
414 if (current_game == NULL) {
415 fprintf(stderr, "Cannot resume game!\n");
416 exit(EXIT_FAILURE);
418 start_fade();
419 gameloop(current_game);
420 SDL_Delay(150);
421 while(SDL_PollEvent(&e));
422 #ifdef ENABLE_FPS_LIMIT
423 frames = 0;
424 ft = SDL_GetTicks();
425 #endif
426 start_fade();
427 state = MENU_STATE_SLIDE_TO_MAINMENU;
428 break;
429 case MENU_STATE_SLIDE_TO_QUIT:
430 slide = 1;
431 slide_direction = 3;
432 state = MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS;
433 break;
434 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS:
435 if (slide == slide_start) {
436 state = MENU_STATE_QUIT;
438 break;
439 case MENU_STATE_QUIT:
440 quit = true;
441 break;
442 default:
443 fprintf(stderr, "State error: %d\n", state);
444 exit(EXIT_FAILURE);
447 /* Sliding */
448 if (SDL_GetTicks() > ticks + 20) {
449 if (slide >= 1 && slide <= slide_start) {
450 slide += slide_direction+(slide_direction*slide/(sqrt(2*slide)));
451 slide = MAX(0, MIN(slide_start, slide));
452 } else if (slide_direction != 0) {
453 slide_direction = 0;
455 ticks = SDL_GetTicks();
458 /* Graphics */
459 #ifdef DEBUG
460 if (state != MENU_STATE_OPTIONS) {
461 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -i, 0);
463 #endif
464 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
465 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20-slide, 255);
466 if (state != MENU_STATE_OPTIONS && state != MENU_STATE_LOCATION) {
467 /* Main Menu */
468 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+(slide/7)+3-(3*(btn_hovering==MENU_START)), 150, 255);
469 if (current_game != NULL) {
470 show_image(GR_BTN_RESUME, WIDTH-get_image_width(GR_BTN_RESUME)+slide+(slide/7)+3-(3*(btn_hovering==MENU_RESUME)), 230, 255);
471 font_draw_string(FONT_SMALL, "match paused", 10, 10);
472 } else {
473 font_draw_string(FONT_MEDIUM, "Tennix 2009 World Tennis Championship Tour", 10, 10);
475 font_draw_string_color(FONT_MEDIUM, URL, 10-1, HEIGHT-10-1-font_get_height(FONT_MEDIUM), 130, 130, 130);
476 font_draw_string_color(FONT_MEDIUM, URL, 10, HEIGHT-10-font_get_height(FONT_MEDIUM), 30, 30, 30);
477 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+(slide/7)+3-(3*(btn_hovering==MENU_QUIT)), 350, 255);
478 } else if (state == MENU_STATE_OPTIONS) {
479 /* Options screen */
480 draw_button_object(&btn_back, mx, my);
481 draw_button_object(&btn_start, mx, my);
482 draw_button_object(&btn_player1, mx, my);
483 draw_button_object(&btn_player2, mx, my);
484 wiggle = 15*sinf((float)ticks/300.);
485 if (PLAYER(prepared_game, 1).input_device_index > -1) {
486 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);
487 } else {
488 show_image_rotozoom(GR_INPUT_AI, CONTROLLER_SETUP_BORDER + CONTROLLER_SETUP_SIZE/2, HEIGHT/2, wiggle, 1.0);
490 if (PLAYER(prepared_game, 2).input_device_index > -1) {
491 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);
492 } else {
493 show_image_rotozoom(GR_INPUT_AI, WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE/2, HEIGHT/2, -wiggle, 1.0);
495 } else if (state == MENU_STATE_LOCATION) {
496 /* Location selection screen */
497 for (x=0; x<location_count(); x++) {
498 new_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[x].worldmap_x,
499 my-worldmap_ypos-locations[x].worldmap_y);
500 if (highlight_location == -1) {
501 if (new_location_distance < 20*20) {
502 highlight_location = x;
504 } else {
505 highlight_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[highlight_location].worldmap_x,
506 my-worldmap_ypos-locations[highlight_location].worldmap_y);
507 if (highlight_location_distance > 20*20) {
508 highlight_location = -1;
510 if (highlight_location_distance > new_location_distance && new_location_distance < 20*20) {
511 highlight_location = x;
515 if (prepared_game != NULL) {
516 if (!location_info_visible) {
517 for (x=0; x<location_count(); x++) {
518 /* draw rectangle for location at "x"*/
519 if (highlight_location != -1 && (unsigned int)highlight_location == x) {
520 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));
523 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);
525 } else {
526 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);
527 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);
528 font_draw_string(FONT_SMALL, prepared_game->location->name, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200);
529 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));
530 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));
531 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));
533 if (prepared_game->location != NULL) {
534 draw_button_object(&btn_start, mx, my);
537 draw_button_object(&btn_back, mx, my);
540 SDL_PollEvent( &e);
541 if( e.type == SDL_QUIT) {
542 state = MENU_STATE_SLIDE_TO_QUIT;
543 /*break;*/
546 keys = SDL_GetKeyState( NULL);
547 mb = SDL_GetMouseState( &mx, &my);
549 btn_hovering_old = btn_hovering;
550 if (state == MENU_STATE_MAINMENU) {
551 btn_hovering = M_POS_DECODE(mx, my);
552 if (current_game == NULL) {
553 btn_hovering &= ~MENU_RESUME;
555 } else if (state == MENU_STATE_LOCATION) {
556 if (M_POS_BUTTON(btn_back, mx, my)) {
557 btn_hovering = MENU_QUIT;
558 } else if (M_POS_BUTTON(btn_start, mx, my)) {
559 btn_hovering = MENU_START;
560 } else {
561 btn_hovering = 0;
563 } else if (state == MENU_STATE_OPTIONS) {
564 if (M_POS_BUTTON(btn_back, mx, my)) {
565 btn_hovering = MENU_QUIT;
566 } else if (M_POS_BUTTON(btn_start, mx, my)) {
567 btn_hovering = MENU_START;
568 } else if (M_POS_BUTTON(btn_player1, mx, my)) {
569 btn_hovering = MENU_PLAYER1;
570 } else if (M_POS_BUTTON(btn_player2, mx, my)) {
571 btn_hovering = MENU_PLAYER2;
572 } else {
573 btn_hovering = 0;
575 } else {
576 /* No menu screen - no hovering. */
577 btn_hovering = 0;
579 #ifndef MAEMO /* On Maemo, we cannot really "hover" (touchscreen!) */
580 if (btn_hovering_old != btn_hovering && btn_hovering != 0) {
581 #ifdef HAVE_VOICE_FILES
582 if (btn_hovering == MENU_QUIT) {
583 play_sample(VOICE_QUIT_IT);
584 } else if (btn_hovering == MENU_START) {
585 play_sample(VOICE_NEW_GAME);
586 } else {
587 play_sample(SOUND_MOUSEOVER);
589 #else
590 /*play_sample(SOUND_MOUSEOVER);*/
591 #endif
593 #endif
595 if( keys[SDLK_ESCAPE] || keys['q']) {
596 /* FIXME: do the state thingie! */
597 break;
600 if( keys['f']) {
601 SDL_WM_ToggleFullScreen( screen);
604 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
605 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
606 show_image(GR_CURSOR, mx-2, my-1, 255);
608 #endif
610 /* Draw the "real" mouse coordinates */
611 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
613 /* Store the screen, because we are fading after this screen update */
614 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
616 updatescr();
618 if( mb & SDL_BUTTON(SDL_BUTTON_LEFT)) {
619 if (!mouse_pressed) {
620 play_sample(SOUND_MOUSEOVER);
622 mouse_pressed = true;
623 } else if (mouse_pressed == true) {
624 /* Mouse button released */
625 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
626 #ifdef HAVE_VOICE_FILES
627 if (btn_hovering == MENU_START) {
628 play_sample(VOICE_LETS_GO);
629 } else {
630 play_sample(SOUND_MOUSECLICK);
632 #else
633 /*play_sample(SOUND_MOUSEOVER);*/
634 #endif
636 if (state == MENU_STATE_MAINMENU) {
637 switch (btn_hovering) {
638 case MENU_START:
639 state = MENU_STATE_SLIDE_TO_LOCATION;
640 break;
641 case MENU_RESUME:
642 state = MENU_STATE_SLIDE_TO_RESUME;
643 break;
644 case MENU_QUIT:
645 state = MENU_STATE_SLIDE_TO_QUIT;
646 break;
648 } else if (state == MENU_STATE_LOCATION) {
649 switch (btn_hovering) {
650 case MENU_START:
651 if (prepared_game->location != NULL) {
652 state = MENU_STATE_FADE_TO_OPTIONS;
654 break;
655 case MENU_QUIT:
656 state = MENU_STATE_SLIDE_TO_MAINMENU;
657 break;
658 default:
659 if (!location_info_visible && highlight_location != -1) {
660 prepared_game->current_location = highlight_location;
661 /* Set the day/night status */
662 if (night_start < night_end) {
663 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x > night_start && locations[prepared_game->current_location].worldmap_x < night_end);
664 } else {
665 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x < night_end || locations[prepared_game->current_location].worldmap_x > night_start);
667 prepared_game->location = &(locations[prepared_game->current_location]);
668 location_info_xpos = MAX(0, MIN(WIDTH-320-50, mx-320/2));
669 location_info_ypos = MAX(0, MIN(HEIGHT-200-160, my-200/2));
670 location_info_visible = true;
671 } else {
672 location_info_visible = false;
673 highlight_location = -1;
674 prepared_game->current_location = -1;
675 prepared_game->location = NULL;
677 break;
679 } else if (state == MENU_STATE_OPTIONS) {
680 switch (btn_hovering) {
681 case MENU_START:
682 state = MENU_STATE_SLIDE_TO_GAME;
683 break;
684 case MENU_QUIT:
685 state = MENU_STATE_FADE_TO_LOCATION;
686 break;
687 case MENU_PLAYER1:
688 /* advance the input device index */
689 PLAYER(prepared_game, 1).input_device_index++;
690 if (PLAYER(prepared_game, 1).input_device_index == (signed int)input_device_count) {
691 PLAYER(prepared_game, 1).input_device_index = -1;
694 if (input_devices[PLAYER(prepared_game, 1).input_device_index].exclusive_to_player == 2) {
695 PLAYER(prepared_game, 1).input_device_index++;
698 /* determine the selected input device */
699 if (PLAYER(prepared_game, 1).input_device_index == -1) {
700 PLAYER(prepared_game, 1).type = PLAYER_TYPE_AI;
701 PLAYER(prepared_game, 1).input = NULL;
702 btn_player1.text = "Carl Van Court";
703 } else {
704 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
705 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
706 btn_player1.text = input_device_get_name(PLAYER(prepared_game, 1).input);
708 break;
709 case MENU_PLAYER2:
710 /* advance the input device index */
711 PLAYER(prepared_game, 2).input_device_index++;
713 if (input_devices[PLAYER(prepared_game, 2).input_device_index].exclusive_to_player == 1) {
714 PLAYER(prepared_game, 2).input_device_index++;
717 if (PLAYER(prepared_game, 2).input_device_index == (signed int)input_device_count) {
718 PLAYER(prepared_game, 2).input_device_index = -1;
721 /* determine the selected input device */
722 if (PLAYER(prepared_game, 2).input_device_index == -1) {
723 PLAYER(prepared_game, 2).type = PLAYER_TYPE_AI;
724 PLAYER(prepared_game, 2).input = NULL;
725 btn_player2.text = "Carl Van Court";
726 } else {
727 PLAYER(prepared_game, 2).type = PLAYER_TYPE_HUMAN;
728 PLAYER(prepared_game, 2).input = &(input_devices[PLAYER(prepared_game, 2).input_device_index]);
729 btn_player2.text = input_device_get_name(PLAYER(prepared_game, 2).input);
731 break;
734 mouse_pressed = false;
736 i++;
737 #ifdef ENABLE_FPS_LIMIT
738 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
739 SDL_Delay(10);
741 frames++;
742 #endif
745 if (current_game != NULL) {
746 if (gamestate_save(current_game, GAMESTATE_FILE) != 0) {
747 fprintf(stderr, "Warning: cannot save gamestate to %s\n", GAMESTATE_FILE);
751 /* Play the credits */
752 /*intro = create_credits();
753 intro_playback = animation_state_new(intro);
754 animation_state_run(intro_playback, 1);
755 animation_state_free(intro_playback);
756 animation_free(intro);*/
758 uninit_graphics();
759 uninit_input();
761 SDL_Quit();
762 return 0;
766 void menu_button_init(MenuButton* b)
768 int w, h;
770 if (b->image_id != GR_COUNT) {
772 * If the button is an image, the "w" and "h" attributes of the
773 * MenuButton struct are simply factors with which the real width
774 * and height of the image (=button) should be multiplied and added
775 * to the "x" and "y" position and the "w" and "h" are replaced with
776 * the real size of the image for the collision detection
778 w = b->w;
779 h = b->h;
781 b->w = get_image_width(b->image_id);
782 b->h = get_image_height(b->image_id);
783 b->x += w*b->w;
784 b->y += h*b->h;