Make sure FPS limit in the main menu is always correct
[tennix.git] / tennix.c
blob03ede76cf53156db82b9257511bc7b0042fafb9a
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
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>
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
32 #include "tennix.h"
33 #include "game.h"
34 #include "graphics.h"
35 #include "sound.h"
36 #include "input.h"
38 SDL_Surface *screen;
40 #ifdef WIN32
42 /* IDs from the resource file */
43 #define START_BUTTON 1
44 #define CHECKBOX_FULLSCREEN 2
45 #define QUIT_BUTTON 3
47 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
49 static int checkbox_is_checked;
51 switch (uMsg) {
52 case WM_CLOSE:
53 EndDialog(hwndDlg, IDCANCEL);
54 break;
55 case WM_COMMAND:
56 switch (wParam) {
57 case START_BUTTON:
58 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
59 break;
60 case QUIT_BUTTON:
61 EndDialog(hwndDlg, IDCANCEL);
62 break;
63 case CHECKBOX_FULLSCREEN:
64 checkbox_is_checked ^= 1;
65 break;
67 break;
68 default:
69 return FALSE;
71 return TRUE;
73 #endif
75 #ifdef WIN32
76 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
77 LPSTR lpCmdLine, int nCmdShow) {
78 #else
79 int main( int argc, char** argv) {
80 #endif
81 int i, slide, slide_direction;
82 int ticks;
83 int mx, my;
84 Uint8 *keys;
85 Uint8 mb;
86 SDL_Event e;
87 int sdl_flags = SDL_SWSURFACE;
88 int btn_hovering = 0, btn_hovering_old = 0;
89 int slide_start;
90 bool mouse_pressed = false;
91 bool quit = false;
92 GameState *current_game = NULL, *prepared_game = NULL;
94 MenuButton btn_back = {
95 "Back to main menu",
96 MENU_OPTIONS_BORDER,
97 HEIGHT-(MENU_OPTIONS_BORDER+MENU_OPTIONS_BUTTON_HEIGHT),
98 MENU_OPTIONS_BUTTON_WIDTH, MENU_OPTIONS_BUTTON_HEIGHT,
99 255, 0, 0
101 MenuButton btn_start = {
102 "Start new game",
103 WIDTH-(MENU_OPTIONS_BORDER+MENU_OPTIONS_BUTTON_WIDTH),
104 HEIGHT-(MENU_OPTIONS_BORDER+MENU_OPTIONS_BUTTON_HEIGHT),
105 MENU_OPTIONS_BUTTON_WIDTH, MENU_OPTIONS_BUTTON_HEIGHT,
106 0, 255, 0
108 MenuButton btn_court_change = {
109 "change",
110 240,
112 50, MENU_OPTIONS_BUTTON_HEIGHT,
113 100, 100, 100
116 int state = MENU_STATE_STARTED;
118 #ifdef ENABLE_FPS_LIMIT
119 Uint32 ft, frames; /* frame timer and frames */
120 #endif
122 #ifdef MAEMO
123 sdl_flags |= SDL_FULLSCREEN;
124 #endif
126 #ifdef WIN32
127 int mb_result;
128 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
130 switch (mb_result) {
131 case IDYES:
132 sdl_flags |= SDL_FULLSCREEN;
133 break;
134 case IDCANCEL:
135 return 0;
136 break;
137 default:
138 break;
140 #else
141 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
143 bool do_help = false;
144 i = 1;
145 while (i < argc) {
146 /* A poor/lazy man's getopt */
147 #define OPTION_SET(longopt,shortopt) \
148 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
149 #define OPTION_VALUE \
150 ((i+1 < argc)?(argv[i+1]):(NULL))
151 #define OPTION_VALUE_PROCESSED \
152 (i++)
153 if (OPTION_SET("--fullscreen", "-f")) {
154 sdl_flags |= SDL_FULLSCREEN;
156 else if (OPTION_SET("--help", "-h")) {
157 do_help = true;
159 else if (OPTION_SET("--list-joysticks", "-J")) {
160 SDL_Init(SDL_INIT_JOYSTICK);
161 joystick_list();
162 return 0;
164 else if (OPTION_SET("--joystick", "-j")) {
165 SDL_Init(SDL_INIT_JOYSTICK);
166 if (OPTION_VALUE == NULL) {
167 fprintf(stderr, "Error: You need to specify the name of the joystick as parameter.\n");
168 do_help = true;
169 break;
171 if (joystick_open(OPTION_VALUE)==0) {
172 fprintf(stderr, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE);
173 break;
175 OPTION_VALUE_PROCESSED;
177 else {
178 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
180 i++;
183 if (do_help == true) {
184 fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv[0]);
185 return 0;
187 #endif
189 srand( (unsigned)time( NULL));
191 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
192 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
193 exit( 1);
196 SDL_VideoInfo* vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
197 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
198 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
199 exit( 1);
202 SDL_WM_SetCaption( "Tennix " VERSION, "Tennix");
203 SDL_ShowCursor( SDL_DISABLE);
204 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
206 init_sound();
207 init_graphics();
208 init_joystick();
210 #ifdef ENABLE_FPS_LIMIT
211 frames = 0;
212 ft = SDL_GetTicks();
213 #endif
215 i = 0;
216 /* Sliding initialization */
217 ticks = SDL_GetTicks();
218 slide = slide_start = get_image_width(GR_SIDEBAR);
219 slide_direction = 0;
220 while(!quit) {
221 /* State transitions */
222 switch (state) {
223 case MENU_STATE_STARTED:
224 state = MENU_STATE_SLIDE_TO_MAINMENU;
225 break;
226 case MENU_STATE_SLIDE_TO_MAINMENU:
227 slide = slide_start;
228 slide_direction = -1;
229 state = MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS;
230 break;
231 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS:
232 if (slide == 0) {
233 slide_direction = 0;
234 state = MENU_STATE_MAINMENU;
236 break;
237 case MENU_STATE_MAINMENU:
238 free(prepared_game);
239 prepared_game = NULL;
240 break;
241 case MENU_STATE_SLIDE_TO_OPTIONS:
242 slide = 1;
243 slide_direction = 3;
244 state = MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS;
245 break;
246 case MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS:
247 if (slide == slide_start) {
248 start_fade();
249 state = MENU_STATE_OPTIONS;
251 break;
252 case MENU_STATE_OPTIONS:
253 /* Prepare a new game */
254 if (prepared_game == NULL) {
255 prepared_game = gamestate_new();
257 break;
258 case MENU_STATE_SLIDE_TO_GAME:
259 /*slide = 1;
260 slide_direction = 2;
261 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
262 break;
263 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
264 if (slide == slide_start) {
265 state = MENU_STATE_GAME;
267 state = MENU_STATE_GAME;
268 break;
269 case MENU_STATE_SLIDE_TO_RESUME:
270 slide = 1;
271 slide_direction = 2;
272 state = MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS;
273 break;
274 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS:
275 if (slide == slide_start) {
276 state = MENU_STATE_RESUME;
278 break;
279 case MENU_STATE_GAME:
280 if (prepared_game == NULL) {
281 fprintf(stderr, "Game not yet prepared!\n");
282 exit(EXIT_FAILURE);
284 /* Cancel a possibly started game */
285 free(current_game);
286 current_game = prepared_game;
287 prepared_game = NULL;
288 /* no break - we are continuing with "resume" */
289 case MENU_STATE_RESUME:
290 if (current_game == NULL) {
291 fprintf(stderr, "Cannot resume game!\n");
292 exit(EXIT_FAILURE);
294 start_fade();
295 gameloop(current_game);
296 SDL_Delay(150);
297 while(SDL_PollEvent(&e));
298 #ifdef ENABLE_FPS_LIMIT
299 frames = 0;
300 ft = SDL_GetTicks();
301 #endif
302 start_fade();
303 state = MENU_STATE_SLIDE_TO_MAINMENU;
304 break;
305 case MENU_STATE_SLIDE_TO_QUIT:
306 slide = 1;
307 slide_direction = 3;
308 state = MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS;
309 break;
310 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS:
311 if (slide == slide_start) {
312 state = MENU_STATE_QUIT;
314 break;
315 case MENU_STATE_QUIT:
316 quit = true;
317 break;
318 default:
319 fprintf(stderr, "State error: %d\n", state);
320 exit(EXIT_FAILURE);
323 /* Sliding */
324 if (SDL_GetTicks() > ticks + 20) {
325 if (slide >= 1 && slide <= slide_start) {
326 slide += slide_direction+(slide_direction*slide/(sqrt(2*slide)));
327 slide = MAX(0, MIN(slide_start, slide));
328 } else if (slide_direction != 0) {
329 slide_direction = 0;
331 ticks = SDL_GetTicks();
334 /* Graphics */
335 #ifdef DEBUG
336 if (state != MENU_STATE_OPTIONS) {
337 fill_image_offset(GR_FOG, 0, 0, WIDTH, HEIGHT, -i, 0);
339 #endif
340 show_image(GR_SIDEBAR, WIDTH-get_image_width(GR_SIDEBAR)+slide, 0, 255);
341 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20-slide, 255);
342 if (state != MENU_STATE_OPTIONS) {
343 /* Main Menu */
344 show_image(GR_BTN_PLAY, WIDTH-get_image_width(GR_BTN_PLAY)+slide+(slide/7)+3-(3*(btn_hovering==MENU_START)), 150, 255);
345 if (current_game != NULL) {
346 show_image(GR_BTN_RESUME, WIDTH-get_image_width(GR_BTN_RESUME)+slide+(slide/7)+3-(3*(btn_hovering==MENU_RESUME)), 230, 255);
347 if (current_game->player2.type == PLAYER_TYPE_AI) {
348 font_draw_string(GR_DKC2_FONT, "Game Against The Computer", 10, 10, 0, 0);
349 } else {
350 font_draw_string(GR_DKC2_FONT, "Multiplayer Versus Game", 10, 10, 0, 0);
352 font_draw_string(GR_DKC2_FONT, current_game->sets_score_str, 10, 40, 0, 0);
354 show_image(GR_BTN_QUIT, WIDTH-get_image_width(GR_BTN_QUIT)+slide+(slide/7)+3-(3*(btn_hovering==MENU_QUIT)), 350, 255);
355 } else {
356 /* Options screen */
357 show_image(GR_TENNIXLOGO, WIDTH-get_image_width(GR_SIDEBAR)-10, 20, 255);
358 draw_button_object(btn_back, mx, my);
359 draw_button_object(btn_start, mx, my);
360 if (prepared_game != NULL) {
361 fill_image(prepared_game->court_type, MENU_OPTIONS_BORDER, MENU_OPTIONS_BORDER*2, get_image_width(GR_STADIUM), get_image_height(GR_STADIUM));
362 show_image(GR_STADIUM, MENU_OPTIONS_BORDER, MENU_OPTIONS_BORDER*2, 255);
363 draw_button_object(btn_court_change, mx, my);
364 font_draw_string(GR_DKC2_FONT, "Location", MENU_OPTIONS_BORDER, MENU_OPTIONS_BORDER, 0, 0);
368 SDL_PollEvent( &e);
369 if( e.type == SDL_QUIT) {
370 state = MENU_STATE_SLIDE_TO_QUIT;
371 /*break;*/
374 keys = SDL_GetKeyState( NULL);
375 mb = SDL_GetMouseState( &mx, &my);
377 btn_hovering_old = btn_hovering;
378 if (state == MENU_STATE_MAINMENU) {
379 btn_hovering = M_POS_DECODE(mx, my);
380 if (current_game == NULL) {
381 btn_hovering &= ~MENU_RESUME;
383 } else if (state == MENU_STATE_OPTIONS) {
384 if (M_POS_BUTTON(btn_back, mx, my)) {
385 btn_hovering = MENU_QUIT;
386 } else if (M_POS_BUTTON(btn_start, mx, my)) {
387 btn_hovering = MENU_START;
388 } else if (M_POS_BUTTON(btn_court_change, mx, my)) {
389 btn_hovering = MENU_COURT_CHANGE;
390 } else {
391 btn_hovering = 0;
393 } else {
394 /* No menu screen - no hovering. */
395 btn_hovering = 0;
397 if (btn_hovering_old != btn_hovering && btn_hovering != 0) {
398 play_sample(SOUND_MOUSEOVER);
401 if( keys[SDLK_ESCAPE] || keys['q']) {
402 /* FIXME: do the state thingie! */
403 break;
406 if( keys['f']) {
407 SDL_WM_ToggleFullScreen( screen);
410 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
411 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS) {
412 show_sprite( GR_RACKET, ((mb&SDL_BUTTON( SDL_BUTTON_LEFT))>0)+(((mb&SDL_BUTTON( SDL_BUTTON_RIGHT))>0)*2), 4, mx, my, 255);
414 #endif
416 /* Draw the "real" mouse coordinates */
417 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
419 /* Store the screen, because we are fading after this screen update */
420 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
422 updatescr();
424 if( mb & SDL_BUTTON(SDL_BUTTON_LEFT)) {
425 mouse_pressed = true;
426 } else if (mouse_pressed == true) {
427 /* Mouse button released */
428 if (state == MENU_STATE_MAINMENU || state == MENU_STATE_OPTIONS) {
429 play_sample(SOUND_MOUSECLICK);
431 if (state == MENU_STATE_MAINMENU) {
432 switch (btn_hovering) {
433 case MENU_START:
434 state = MENU_STATE_SLIDE_TO_OPTIONS;
435 break;
436 case MENU_RESUME:
437 state = MENU_STATE_SLIDE_TO_RESUME;
438 break;
439 case MENU_QUIT:
440 state = MENU_STATE_SLIDE_TO_QUIT;
441 break;
443 } else if (state == MENU_STATE_OPTIONS) {
444 switch (btn_hovering) {
445 case MENU_START:
446 state = MENU_STATE_SLIDE_TO_GAME;
447 break;
448 case MENU_QUIT:
449 state = MENU_STATE_SLIDE_TO_MAINMENU;
450 break;
451 case MENU_COURT_CHANGE:
452 prepared_game->court_type++;
453 if (prepared_game->court_type > GR_CTT_LAST) {
454 prepared_game->court_type = GR_CTT_FIRST;
456 break;
459 mouse_pressed = false;
461 i++;
462 #ifdef ENABLE_FPS_LIMIT
463 while (frames*1000.0/((float)(SDL_GetTicks()-ft+1))>(float)(DEFAULT_FPS)) {
464 SDL_Delay(10);
466 frames++;
467 #endif
470 uninit_graphics();
471 uninit_joystick();
473 SDL_Quit();
474 return 0;