5b6d44b873796e18808b118c3b91e9c4e9c5b720
[tennix.git] / tennix.c
blob5b6d44b873796e18808b118c3b91e9c4e9c5b720
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 "network.h"
40 #include "util.h"
41 #include "animation.h"
43 #include "locations.h"
45 SDL_Surface *screen;
47 #ifdef WIN32
49 /* IDs from the resource file */
50 #define START_BUTTON 1
51 #define CHECKBOX_FULLSCREEN 2
52 #define QUIT_BUTTON 3
54 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
56 static int checkbox_is_checked;
58 switch (uMsg) {
59 case WM_CLOSE:
60 EndDialog(hwndDlg, IDCANCEL);
61 break;
62 case WM_COMMAND:
63 switch (wParam) {
64 case START_BUTTON:
65 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
66 break;
67 case QUIT_BUTTON:
68 EndDialog(hwndDlg, IDCANCEL);
69 break;
70 case CHECKBOX_FULLSCREEN:
71 checkbox_is_checked ^= 1;
72 break;
74 break;
75 default:
76 return FALSE;
78 return TRUE;
80 #endif
82 #ifdef WIN32
83 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
84 LPSTR lpCmdLine, int nCmdShow) {
85 #else
86 int main( int argc, char** argv) {
87 #endif
88 int i, slide, slide_direction;
89 unsigned int x;
90 Uint32 ticks;
91 int mx, my;
92 unsigned int worldmap_xpos, worldmap_ypos;
93 Uint8 *keys;
94 Uint8 mb;
95 SDL_Event e;
96 int sdl_flags = SDL_SWSURFACE;
97 int btn_hovering = 0, btn_hovering_old = 0;
98 int slide_start;
99 bool mouse_pressed = false;
100 bool quit = false;
101 bool benchmark = false;
102 unsigned int night_start, night_end;
103 GameState *current_game = NULL, *prepared_game = NULL;
104 InputDevice* input_devices;
105 unsigned int input_device_count, input_device_id;
106 Animation *intro;
107 AnimationState *intro_playback;
108 float wiggle;
109 TennixNet* connection = NULL;
110 char* net_host = NULL;
111 int net_port_local;
112 int net_port_remote;
113 bool net_master = false;
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("--network", "-n")) {
204 net_host = OPTION_VALUE;
205 assert(OPTION_VALUE != NULL);
206 OPTION_VALUE_PROCESSED;
207 net_port_local = atoi(OPTION_VALUE);
208 assert(OPTION_VALUE != NULL);
209 OPTION_VALUE_PROCESSED;
210 net_port_remote = atoi(OPTION_VALUE);
211 assert(OPTION_VALUE != NULL);
212 OPTION_VALUE_PROCESSED;
214 else if (OPTION_SET("--master", "-m")) {
215 net_master = true;
217 else {
218 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
220 i++;
223 if (do_help == true) {
224 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h]\n", argv[0]);
225 return 0;
227 #endif
229 if (benchmark) {
230 srand(100);
231 } else {
232 srand((unsigned)time(NULL));
235 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
236 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
237 exit( 1);
240 vi = SDL_GetVideoInfo();
241 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
242 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
243 exit( 1);
246 SDL_WM_SetCaption( "Tennix 2009 World Tennis Championship Tour", "Tennix 2009");
247 SDL_ShowCursor( SDL_DISABLE);
248 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
250 init_graphics();
251 init_sound();
252 init_input();
253 init_network();
255 if (net_host != NULL) {
256 connection = network_connect(net_host, net_port_local, net_port_remote);
257 network_set_master(connection, net_master);
260 menu_button_init(&btn_back);
261 menu_button_init(&btn_start);
262 menu_button_init(&btn_player1);
263 menu_button_init(&btn_player2);
265 input_devices = find_input_devices(&input_device_count);
267 current_game = gamestate_load(GAMESTATE_FILE);
268 if (current_game != NULL) {
269 /* restore pointer to location */
270 current_game->location = &(locations[current_game->current_location]);
271 /* */
272 for (i=1; i<=MAXPLAYERS; i++) {
273 if (PLAYER(current_game, i).type == PLAYER_TYPE_HUMAN) {
274 input_device_id = PLAYER(current_game, i).input_device_index;
275 if (input_device_id < input_device_count) {
276 /* ok, we still have that device around */
277 PLAYER(current_game, i).input = &(input_devices[input_device_id]);
278 } else {
279 /* the device vanished - set to AI (FIXME: select new device) */
280 PLAYER(current_game, i).type = PLAYER_TYPE_AI;
281 PLAYER(current_game, i).input = NULL;
287 #ifdef ENABLE_FPS_LIMIT
288 frames = 0;
289 ft = SDL_GetTicks();
290 #endif
292 if (benchmark) {
293 GameState* g = gamestate_new();
294 PLAYER(g, 1).type = PLAYER_TYPE_AI;
295 PLAYER(g, 2).type = PLAYER_TYPE_AI;
296 g->location = &(locations[0]);
297 gameloop(g, connection);
298 free(g);
299 exit(0);
302 /*intro = create_intro();
303 intro_playback = animation_state_new(intro);
304 animation_state_run(intro_playback, 1);
305 animation_state_free(intro_playback);
306 animation_free(intro);
307 start_fade();*/
309 worldmap_xpos = (WIDTH-get_image_width(GR_WORLDMAP))/2;
310 worldmap_ypos = (HEIGHT-get_image_height(GR_WORLDMAP))/2;
312 i = 0;
313 /* Sliding initialization */
314 ticks = SDL_GetTicks();
315 slide = slide_start = get_image_width(GR_SIDEBAR);
316 slide_direction = 0;
317 while(!quit) {
318 /* State transitions */
319 switch (state) {
320 case MENU_STATE_STARTED:
321 state = MENU_STATE_SLIDE_TO_MAINMENU;
322 break;
323 case MENU_STATE_SLIDE_TO_MAINMENU:
324 clear_screen();
325 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
326 store_screen();
327 slide = slide_start;
328 slide_direction = -1;
329 state = MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS;
330 break;
331 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS:
332 if (slide == 0) {
333 slide_direction = 0;
334 state = MENU_STATE_MAINMENU;
336 break;
337 case MENU_STATE_MAINMENU:
338 free(prepared_game);
339 prepared_game = NULL;
340 break;
341 case MENU_STATE_SLIDE_TO_LOCATION:
342 slide = 1;
343 slide_direction = 3;
344 state = MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS;
345 break;
346 case MENU_STATE_SLIDE_TO_LOCATION_IN_PROGRESS:
347 if (slide == slide_start) {
348 state = MENU_STATE_FADE_TO_LOCATION;
350 break;
351 case MENU_STATE_FADE_TO_LOCATION:
352 start_fade();
353 state = MENU_STATE_LOCATION;
354 clear_screen();
356 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
357 /* Draw and store the worldmap with day/night times */
358 show_image(GR_WORLDMAP, WIDTH/2-get_image_width(GR_WORLDMAP)/2, HEIGHT/2-get_image_height(GR_WORLDMAP)/2, 255);
359 day_night(get_image_width(GR_WORLDMAP), &night_start, &night_end);
360 if (night_start > night_end) {
361 rectangle_alpha(worldmap_xpos, worldmap_ypos, night_end, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
362 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, get_image_width(GR_WORLDMAP)-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
363 } else {
364 rectangle_alpha(worldmap_xpos+night_start, worldmap_ypos, night_end-night_start, get_image_height(GR_WORLDMAP), 0, 0, 0, 150);
367 /* add misc items to screen */
368 font_draw_string(FONT_XLARGE, "Pick a location", (WIDTH-font_get_string_width(FONT_XLARGE, "Pick a location"))/2, 20);
370 store_screen();
371 break;
372 case MENU_STATE_FADE_TO_OPTIONS:
373 start_fade();
374 clear_screen();
375 rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80);
376 rectangle(CONTROLLER_SETUP_BORDER, HEIGHT/2-CONTROLLER_SETUP_SIZE/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
377 rectangle(WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE, (HEIGHT-CONTROLLER_SETUP_SIZE)/2, CONTROLLER_SETUP_SIZE, CONTROLLER_SETUP_SIZE, 150, 150, 150);
378 font_draw_string(FONT_XLARGE, "Controller setup", (WIDTH-font_get_string_width(FONT_XLARGE, "Controller setup"))/2, 20);
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 1", 130 - font_get_string_width(FONT_MEDIUM, "Player 1")/2, (HEIGHT-CONTROLLER_SETUP_SIZE)/2 - 10 - font_get_height(FONT_MEDIUM));
381 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));
382 store_screen();
383 start_fade();
384 state = MENU_STATE_OPTIONS;
385 break;
386 case MENU_STATE_LOCATION:
387 case MENU_STATE_OPTIONS:
388 /* Prepare a new game */
389 if (prepared_game == NULL) {
390 prepared_game = gamestate_new();
391 prepared_game->location = NULL;
392 /* FIXME - this should not be written here, but taken from the input devices */
393 btn_player1.text = "Keyboard (arrows)";
394 btn_player2.text = "Carl Van Court";
395 #ifdef MAEMO
396 PLAYER(prepared_game, 1).input_device_index = 0;
397 #else
398 PLAYER(prepared_game, 1).input_device_index = 2;
399 #endif
400 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
401 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
402 location_info_visible = false;
403 prepared_game->current_location = -1;
404 highlight_location = -1;
406 break;
407 case MENU_STATE_SLIDE_TO_GAME:
408 /*slide = 1;
409 slide_direction = 2;
410 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
411 break;
412 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
413 if (slide == slide_start) {
414 state = MENU_STATE_GAME;
416 state = MENU_STATE_GAME;
417 break;
418 case MENU_STATE_SLIDE_TO_RESUME:
419 slide = 1;
420 slide_direction = 2;
421 state = MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS;
422 break;
423 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS:
424 if (slide == slide_start) {
425 state = MENU_STATE_RESUME;
427 break;
428 case MENU_STATE_GAME:
429 if (prepared_game == NULL) {
430 fprintf(stderr, "Game not yet prepared!\n");
431 exit(EXIT_FAILURE);
434 /* Cancel a possibly started game */
435 free(current_game);
436 current_game = prepared_game;
437 prepared_game = NULL;
438 /* no break - we are continuing with "resume" */
439 case MENU_STATE_RESUME:
440 if (current_game == NULL) {
441 fprintf(stderr, "Cannot resume game!\n");
442 exit(EXIT_FAILURE);
444 start_fade();
445 gameloop(current_game, connection);
446 SDL_Delay(150);
447 while(SDL_PollEvent(&e));
448 #ifdef ENABLE_FPS_LIMIT
449 frames = 0;
450 ft = SDL_GetTicks();
451 #endif
452 start_fade();
453 state = MENU_STATE_SLIDE_TO_MAINMENU;
454 break;
455 case MENU_STATE_SLIDE_TO_QUIT:
456 slide = 1;
457 slide_direction = 3;
458 state = MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS;
459 break;
460 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS:
461 if (slide == slide_start) {
462 state = MENU_STATE_QUIT;
464 break;
465 case MENU_STATE_QUIT:
466 quit = true;
467 break;
468 default:
469 fprintf(stderr, "State error: %d\n", state);
470 exit(EXIT_FAILURE);
473 /* Sliding */
474 if (SDL_GetTicks() > ticks + 20) {
475 if (slide >= 1 && slide <= slide_start) {
476 slide += slide_direction+(slide_direction*slide/(sqrt(2*slide)));
477 slide = MAX(0, MIN(slide_start, slide));
478 } else if (slide_direction != 0) {
479 slide_direction = 0;
481 ticks = SDL_GetTicks();
484 /* Graphics */
485 #ifdef DEBUG
486 if (state != MENU_STATE_OPTIONS) {
487 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -i, 0);
489 #endif
490 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
491 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20-slide, 255);
492 if (state != MENU_STATE_OPTIONS && state != MENU_STATE_LOCATION) {
493 /* Main Menu */
494 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+(slide/7)+3-(3*(btn_hovering==MENU_START)), 150, 255);
495 if (current_game != NULL) {
496 show_image(GR_BTN_RESUME, WIDTH-get_image_width(GR_BTN_RESUME)+slide+(slide/7)+3-(3*(btn_hovering==MENU_RESUME)), 230, 255);
497 font_draw_string(FONT_SMALL, "match paused", 10, 10);
498 } else {
499 font_draw_string(FONT_MEDIUM, "Tennix 2009 World Tennis Championship Tour", 10, 10);
501 font_draw_string_color(FONT_MEDIUM, URL, 10-1, HEIGHT-10-1-font_get_height(FONT_MEDIUM), 130, 130, 130);
502 font_draw_string_color(FONT_MEDIUM, URL, 10, HEIGHT-10-font_get_height(FONT_MEDIUM), 30, 30, 30);
503 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+(slide/7)+3-(3*(btn_hovering==MENU_QUIT)), 350, 255);
504 } else if (state == MENU_STATE_OPTIONS) {
505 /* Options screen */
506 draw_button_object(&btn_back, mx, my);
507 draw_button_object(&btn_start, mx, my);
508 draw_button_object(&btn_player1, mx, my);
509 draw_button_object(&btn_player2, mx, my);
510 wiggle = 15*sinf((float)ticks/300.);
511 if (PLAYER(prepared_game, 1).input_device_index > -1) {
512 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);
513 } else {
514 show_image_rotozoom(GR_INPUT_AI, CONTROLLER_SETUP_BORDER + CONTROLLER_SETUP_SIZE/2, HEIGHT/2, wiggle, 1.0);
516 if (PLAYER(prepared_game, 2).input_device_index > -1) {
517 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);
518 } else {
519 show_image_rotozoom(GR_INPUT_AI, WIDTH-CONTROLLER_SETUP_BORDER-CONTROLLER_SETUP_SIZE/2, HEIGHT/2, -wiggle, 1.0);
521 } else if (state == MENU_STATE_LOCATION) {
522 /* Location selection screen */
523 for (x=0; x<location_count(); x++) {
524 new_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[x].worldmap_x,
525 my-worldmap_ypos-locations[x].worldmap_y);
526 if (highlight_location == -1) {
527 if (new_location_distance < 20*20) {
528 highlight_location = x;
530 } else {
531 highlight_location_distance = SQUARE_DISTANCE(mx-worldmap_xpos-locations[highlight_location].worldmap_x,
532 my-worldmap_ypos-locations[highlight_location].worldmap_y);
533 if (highlight_location_distance > 20*20) {
534 highlight_location = -1;
536 if (highlight_location_distance > new_location_distance && new_location_distance < 20*20) {
537 highlight_location = x;
541 if (prepared_game != NULL) {
542 if (!location_info_visible) {
543 for (x=0; x<location_count(); x++) {
544 /* draw rectangle for location at "x"*/
545 if (highlight_location != -1 && (unsigned int)highlight_location == x) {
546 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));
549 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);
551 } else {
552 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);
553 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);
554 font_draw_string(FONT_SMALL, prepared_game->location->name, location_info_xpos+MENU_OPTIONS_BORDER, location_info_ypos+MENU_OPTIONS_BORDER+200);
555 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));
556 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));
557 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));
559 if (prepared_game->location != NULL) {
560 draw_button_object(&btn_start, mx, my);
563 draw_button_object(&btn_back, mx, my);
566 SDL_PollEvent( &e);
567 if( e.type == SDL_QUIT) {
568 state = MENU_STATE_SLIDE_TO_QUIT;
569 /*break;*/
572 keys = SDL_GetKeyState( NULL);
573 mb = SDL_GetMouseState( &mx, &my);
575 btn_hovering_old = btn_hovering;
576 if (state == MENU_STATE_MAINMENU) {
577 btn_hovering = M_POS_DECODE(mx, my);
578 if (current_game == NULL) {
579 btn_hovering &= ~MENU_RESUME;
581 } else if (state == MENU_STATE_LOCATION) {
582 if (M_POS_BUTTON(btn_back, mx, my)) {
583 btn_hovering = MENU_QUIT;
584 } else if (M_POS_BUTTON(btn_start, mx, my)) {
585 btn_hovering = MENU_START;
586 } else {
587 btn_hovering = 0;
589 } else if (state == MENU_STATE_OPTIONS) {
590 if (M_POS_BUTTON(btn_back, mx, my)) {
591 btn_hovering = MENU_QUIT;
592 } else if (M_POS_BUTTON(btn_start, mx, my)) {
593 btn_hovering = MENU_START;
594 } else if (M_POS_BUTTON(btn_player1, mx, my)) {
595 btn_hovering = MENU_PLAYER1;
596 } else if (M_POS_BUTTON(btn_player2, mx, my)) {
597 btn_hovering = MENU_PLAYER2;
598 } else {
599 btn_hovering = 0;
601 } else {
602 /* No menu screen - no hovering. */
603 btn_hovering = 0;
605 #ifndef MAEMO /* On Maemo, we cannot really "hover" (touchscreen!) */
606 if (btn_hovering_old != btn_hovering && btn_hovering != 0) {
607 #ifdef HAVE_VOICE_FILES
608 if (btn_hovering == MENU_QUIT) {
609 play_sample(VOICE_QUIT_IT);
610 } else if (btn_hovering == MENU_START) {
611 play_sample(VOICE_NEW_GAME);
612 } else {
613 play_sample(SOUND_MOUSEOVER);
615 #else
616 /*play_sample(SOUND_MOUSEOVER);*/
617 #endif
619 #endif
621 if( keys[SDLK_ESCAPE] || keys['q']) {
622 /* FIXME: do the state thingie! */
623 break;
626 if( keys['f']) {
627 SDL_WM_ToggleFullScreen( screen);
630 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
631 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
632 show_image(GR_CURSOR, mx-2, my-1, 255);
634 #endif
636 /* Draw the "real" mouse coordinates */
637 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
639 /* Store the screen, because we are fading after this screen update */
640 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
642 updatescr();
644 if( mb & SDL_BUTTON(SDL_BUTTON_LEFT)) {
645 if (!mouse_pressed) {
646 play_sample(SOUND_MOUSEOVER);
648 mouse_pressed = true;
649 } else if (mouse_pressed == true) {
650 /* Mouse button released */
651 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS || state == MENU_STATE_LOCATION) {
652 #ifdef HAVE_VOICE_FILES
653 if (btn_hovering == MENU_START) {
654 play_sample(VOICE_LETS_GO);
655 } else {
656 play_sample(SOUND_MOUSECLICK);
658 #else
659 /*play_sample(SOUND_MOUSEOVER);*/
660 #endif
662 if (state == MENU_STATE_MAINMENU) {
663 switch (btn_hovering) {
664 case MENU_START:
665 state = MENU_STATE_SLIDE_TO_LOCATION;
666 break;
667 case MENU_RESUME:
668 state = MENU_STATE_SLIDE_TO_RESUME;
669 break;
670 case MENU_QUIT:
671 state = MENU_STATE_SLIDE_TO_QUIT;
672 break;
674 } else if (state == MENU_STATE_LOCATION) {
675 switch (btn_hovering) {
676 case MENU_START:
677 if (prepared_game->location != NULL) {
678 state = MENU_STATE_FADE_TO_OPTIONS;
680 break;
681 case MENU_QUIT:
682 state = MENU_STATE_SLIDE_TO_MAINMENU;
683 break;
684 default:
685 if (!location_info_visible && highlight_location != -1) {
686 prepared_game->current_location = highlight_location;
687 /* Set the day/night status */
688 if (night_start < night_end) {
689 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x > night_start && locations[prepared_game->current_location].worldmap_x < night_end);
690 } else {
691 locations[prepared_game->current_location].night = (locations[prepared_game->current_location].worldmap_x < night_end || locations[prepared_game->current_location].worldmap_x > night_start);
693 prepared_game->location = &(locations[prepared_game->current_location]);
694 location_info_xpos = MAX(0, MIN(WIDTH-320-50, mx-320/2));
695 location_info_ypos = MAX(0, MIN(HEIGHT-200-160, my-200/2));
696 location_info_visible = true;
697 } else {
698 location_info_visible = false;
699 highlight_location = -1;
700 prepared_game->current_location = -1;
701 prepared_game->location = NULL;
703 break;
705 } else if (state == MENU_STATE_OPTIONS) {
706 switch (btn_hovering) {
707 case MENU_START:
708 state = MENU_STATE_SLIDE_TO_GAME;
709 break;
710 case MENU_QUIT:
711 state = MENU_STATE_FADE_TO_LOCATION;
712 break;
713 case MENU_PLAYER1:
714 /* advance the input device index */
715 PLAYER(prepared_game, 1).input_device_index++;
716 if (PLAYER(prepared_game, 1).input_device_index == (signed int)input_device_count) {
717 PLAYER(prepared_game, 1).input_device_index = -1;
720 if (input_devices[PLAYER(prepared_game, 1).input_device_index].exclusive_to_player == 2) {
721 PLAYER(prepared_game, 1).input_device_index++;
724 /* determine the selected input device */
725 if (PLAYER(prepared_game, 1).input_device_index == -1) {
726 PLAYER(prepared_game, 1).type = PLAYER_TYPE_AI;
727 PLAYER(prepared_game, 1).input = NULL;
728 btn_player1.text = "Carl Van Court";
729 } else {
730 PLAYER(prepared_game, 1).type = PLAYER_TYPE_HUMAN;
731 PLAYER(prepared_game, 1).input = &(input_devices[PLAYER(prepared_game, 1).input_device_index]);
732 btn_player1.text = input_device_get_name(PLAYER(prepared_game, 1).input);
734 break;
735 case MENU_PLAYER2:
736 /* advance the input device index */
737 PLAYER(prepared_game, 2).input_device_index++;
739 if (input_devices[PLAYER(prepared_game, 2).input_device_index].exclusive_to_player == 1) {
740 PLAYER(prepared_game, 2).input_device_index++;
743 if (PLAYER(prepared_game, 2).input_device_index == (signed int)input_device_count) {
744 PLAYER(prepared_game, 2).input_device_index = -1;
747 /* determine the selected input device */
748 if (PLAYER(prepared_game, 2).input_device_index == -1) {
749 PLAYER(prepared_game, 2).type = PLAYER_TYPE_AI;
750 PLAYER(prepared_game, 2).input = NULL;
751 btn_player2.text = "Carl Van Court";
752 } else {
753 PLAYER(prepared_game, 2).type = PLAYER_TYPE_HUMAN;
754 PLAYER(prepared_game, 2).input = &(input_devices[PLAYER(prepared_game, 2).input_device_index]);
755 btn_player2.text = input_device_get_name(PLAYER(prepared_game, 2).input);
757 break;
760 mouse_pressed = false;
762 i++;
763 #ifdef ENABLE_FPS_LIMIT
764 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
765 SDL_Delay(10);
767 frames++;
768 #endif
771 if (current_game != NULL) {
772 if (gamestate_save(current_game, GAMESTATE_FILE) != 0) {
773 fprintf(stderr, "Warning: cannot save gamestate to %s\n", GAMESTATE_FILE);
777 /* Play the credits */
778 /*intro = create_credits();
779 intro_playback = animation_state_new(intro);
780 animation_state_run(intro_playback, 1);
781 animation_state_free(intro_playback);
782 animation_free(intro);*/
784 uninit_graphics();
785 uninit_input();
786 uninit_network();
788 SDL_Quit();
789 return 0;
793 void menu_button_init(MenuButton* b)
795 int w, h;
797 if (b->image_id != GR_COUNT) {
799 * If the button is an image, the "w" and "h" attributes of the
800 * MenuButton struct are simply factors with which the real width
801 * and height of the image (=button) should be multiplied and added
802 * to the "x" and "y" position and the "w" and "h" are replaced with
803 * the real size of the image for the collision detection
805 w = b->w;
806 h = b->h;
808 b->w = get_image_width(b->image_id);
809 b->h = get_image_height(b->image_id);
810 b->x += w*b->w;
811 b->y += h*b->h;