5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
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,
44 /* IDs from the resource file */
45 #define START_BUTTON 1
46 #define CHECKBOX_FULLSCREEN 2
49 BOOL CALLBACK
ConfigDialogProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
51 static int checkbox_is_checked
;
55 EndDialog(hwndDlg
, IDCANCEL
);
60 EndDialog(hwndDlg
, (checkbox_is_checked
)?(IDYES
):(IDNO
));
63 EndDialog(hwndDlg
, IDCANCEL
);
65 case CHECKBOX_FULLSCREEN
:
66 checkbox_is_checked
^= 1;
78 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
79 LPSTR lpCmdLine
, int nCmdShow
) {
81 int main( int argc
, char** argv
) {
83 int i
, slide
, slide_direction
;
89 int sdl_flags
= SDL_SWSURFACE
;
90 int btn_hovering
= 0, btn_hovering_old
= 0;
92 bool mouse_pressed
= false;
94 bool benchmark
= false;
95 GameState
*current_game
= NULL
, *prepared_game
= NULL
;
97 MenuButton btn_back
= {
100 HEIGHT
-(MENU_OPTIONS_BORDER
+MENU_OPTIONS_BUTTON_HEIGHT
),
101 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
104 MenuButton btn_start
= {
106 WIDTH
-(MENU_OPTIONS_BORDER
+MENU_OPTIONS_BUTTON_WIDTH
),
107 HEIGHT
-(MENU_OPTIONS_BORDER
+MENU_OPTIONS_BUTTON_HEIGHT
),
108 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
111 MenuButton btn_court_change
= {
115 50, MENU_OPTIONS_BUTTON_HEIGHT
,
118 MenuButton btn_player1
= {
122 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
125 MenuButton btn_player2
= {
128 180+MENU_OPTIONS_BORDER
*2+MENU_OPTIONS_BUTTON_HEIGHT
,
129 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
133 int state
= MENU_STATE_STARTED
;
135 #ifdef ENABLE_FPS_LIMIT
136 Uint32 ft
, frames
; /* frame timer and frames */
140 sdl_flags
|= SDL_FULLSCREEN
;
145 mb_result
= DialogBox(hInstance
, "CONFIG", 0, (DLGPROC
)ConfigDialogProc
);
149 sdl_flags
|= SDL_FULLSCREEN
;
158 fprintf( stderr
, "Tennix %s\n%s\n%s\n\n", VERSION
, COPYRIGHT
, URL
);
160 bool do_help
= false;
163 /* A poor/lazy man's getopt */
164 #define OPTION_SET(longopt,shortopt) \
165 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
166 #define OPTION_VALUE \
167 ((i+1 < argc)?(argv[i+1]):(NULL))
168 #define OPTION_VALUE_PROCESSED \
170 if (OPTION_SET("--fullscreen", "-f")) {
171 sdl_flags
|= SDL_FULLSCREEN
;
173 else if (OPTION_SET("--help", "-h")) {
176 else if (OPTION_SET("--list-joysticks", "-J")) {
177 SDL_Init(SDL_INIT_JOYSTICK
);
181 else if (OPTION_SET("--benchmark", "-b")) {
184 else if (OPTION_SET("--joystick", "-j")) {
185 SDL_Init(SDL_INIT_JOYSTICK
);
186 if (OPTION_VALUE
== NULL
) {
187 fprintf(stderr
, "Error: You need to specify the name of the joystick as parameter.\n");
191 if (joystick_open(OPTION_VALUE
)==0) {
192 fprintf(stderr
, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE
);
195 OPTION_VALUE_PROCESSED
;
198 fprintf(stderr
, "Ignoring unknown option: %s\n", argv
[i
]);
203 if (do_help
== true) {
204 fprintf(stderr
, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv
[0]);
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());
220 SDL_VideoInfo
* vi
= (SDL_VideoInfo
*)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());
226 SDL_WM_SetCaption( "Tennix " VERSION
, "Tennix");
227 SDL_ShowCursor( SDL_DISABLE
);
228 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY
, 1);
234 #ifdef ENABLE_FPS_LIMIT
240 GameState
* g
= gamestate_new();
241 g
->player1
.type
= PLAYER_TYPE_AI
;
242 g
->player2
.type
= PLAYER_TYPE_AI
;
243 g
->timelimit
= BENCHMARK_TIMELIMIT
*1000;
250 /* Sliding initialization */
251 ticks
= SDL_GetTicks();
252 slide
= slide_start
= get_image_width(GR_SIDEBAR
);
255 /* State transitions */
257 case MENU_STATE_STARTED
:
258 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
260 case MENU_STATE_SLIDE_TO_MAINMENU
:
262 slide_direction
= -1;
263 state
= MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
;
265 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
:
268 state
= MENU_STATE_MAINMENU
;
271 case MENU_STATE_MAINMENU
:
273 prepared_game
= NULL
;
275 case MENU_STATE_SLIDE_TO_OPTIONS
:
278 state
= MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
;
280 case MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
:
281 if (slide
== slide_start
) {
283 state
= MENU_STATE_OPTIONS
;
286 case MENU_STATE_OPTIONS
:
287 /* Prepare a new game */
288 if (prepared_game
== NULL
) {
289 prepared_game
= gamestate_new();
290 btn_player1
.text
= "Keyboard (W-S-D)";
291 btn_player2
.text
= "Computer (AI)";
294 case MENU_STATE_SLIDE_TO_GAME
:
297 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
299 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
300 if (slide == slide_start) {
301 state = MENU_STATE_GAME;
303 state
= MENU_STATE_GAME
;
305 case MENU_STATE_SLIDE_TO_RESUME
:
308 state
= MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
;
310 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
:
311 if (slide
== slide_start
) {
312 state
= MENU_STATE_RESUME
;
315 case MENU_STATE_GAME
:
316 if (prepared_game
== NULL
) {
317 fprintf(stderr
, "Game not yet prepared!\n");
320 /* Cancel a possibly started game */
322 current_game
= prepared_game
;
323 prepared_game
= NULL
;
324 /* no break - we are continuing with "resume" */
325 case MENU_STATE_RESUME
:
326 if (current_game
== NULL
) {
327 fprintf(stderr
, "Cannot resume game!\n");
331 gameloop(current_game
);
333 while(SDL_PollEvent(&e
));
334 #ifdef ENABLE_FPS_LIMIT
339 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
341 case MENU_STATE_SLIDE_TO_QUIT
:
344 state
= MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
;
346 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
:
347 if (slide
== slide_start
) {
348 state
= MENU_STATE_QUIT
;
351 case MENU_STATE_QUIT
:
355 fprintf(stderr
, "State error: %d\n", state
);
360 if (SDL_GetTicks() > ticks
+ 20) {
361 if (slide
>= 1 && slide
<= slide_start
) {
362 slide
+= slide_direction
+(slide_direction
*slide
/(sqrt(2*slide
)));
363 slide
= MAX(0, MIN(slide_start
, slide
));
364 } else if (slide_direction
!= 0) {
367 ticks
= SDL_GetTicks();
372 if (state
!= MENU_STATE_OPTIONS
) {
373 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -i
, 0);
376 show_image(GR_SIDEBAR
, WIDTH
-get_image_width(GR_SIDEBAR
)+slide
, 0, 255);
377 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20-slide
, 255);
378 if (state
!= MENU_STATE_OPTIONS
) {
380 show_image(GR_BTN_PLAY
, WIDTH
-get_image_width(GR_BTN_PLAY
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_START
)), 150, 255);
381 if (current_game
!= NULL
) {
382 show_image(GR_BTN_RESUME
, WIDTH
-get_image_width(GR_BTN_RESUME
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_RESUME
)), 230, 255);
383 font_draw_string(GR_DKC2_FONT
, "current match:", 10, 10, 0, 0);
384 font_draw_string(GR_DKC2_FONT
, current_game
->sets_score_str
, 10, 40, 0, 0);
386 show_image(GR_BTN_QUIT
, WIDTH
-get_image_width(GR_BTN_QUIT
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_QUIT
)), 350, 255);
389 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20, 255);
390 draw_button_object(btn_back
, mx
, my
);
391 draw_button_object(btn_start
, mx
, my
);
392 if (prepared_game
!= NULL
) {
393 fill_image(prepared_game
->court_type
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, get_image_width(GR_STADIUM
), get_image_height(GR_STADIUM
));
394 show_image(GR_STADIUM
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, 255);
395 draw_button_object(btn_court_change
, mx
, my
);
396 font_draw_string(GR_DKC2_FONT
, "Location", MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
, 0, 0);
397 draw_button_object(btn_player1
, mx
, my
);
398 draw_button_object(btn_player2
, mx
, my
);
399 font_draw_string(GR_DKC2_FONT
, "Player 1", btn_player1
.x
, btn_player1
.y
-MENU_OPTIONS_BORDER
, 0, 0);
400 font_draw_string(GR_DKC2_FONT
, "Player 2", btn_player2
.x
, btn_player2
.y
-MENU_OPTIONS_BORDER
, 0, 0);
405 if( e
.type
== SDL_QUIT
) {
406 state
= MENU_STATE_SLIDE_TO_QUIT
;
410 keys
= SDL_GetKeyState( NULL
);
411 mb
= SDL_GetMouseState( &mx
, &my
);
413 btn_hovering_old
= btn_hovering
;
414 if (state
== MENU_STATE_MAINMENU
) {
415 btn_hovering
= M_POS_DECODE(mx
, my
);
416 if (current_game
== NULL
) {
417 btn_hovering
&= ~MENU_RESUME
;
419 } else if (state
== MENU_STATE_OPTIONS
) {
420 if (M_POS_BUTTON(btn_back
, mx
, my
)) {
421 btn_hovering
= MENU_QUIT
;
422 } else if (M_POS_BUTTON(btn_start
, mx
, my
)) {
423 btn_hovering
= MENU_START
;
424 } else if (M_POS_BUTTON(btn_court_change
, mx
, my
)) {
425 btn_hovering
= MENU_COURT_CHANGE
;
426 } else if (M_POS_BUTTON(btn_player1
, mx
, my
)) {
427 btn_hovering
= MENU_PLAYER1
;
428 } else if (M_POS_BUTTON(btn_player2
, mx
, my
)) {
429 btn_hovering
= MENU_PLAYER2
;
434 /* No menu screen - no hovering. */
437 #ifndef MAEMO /* On Maemo, we cannot really "hover" (touchscreen!) */
438 if (btn_hovering_old
!= btn_hovering
&& btn_hovering
!= 0) {
439 #ifdef HAVE_VOICE_FILES
440 if (btn_hovering
== MENU_QUIT
) {
441 play_sample(VOICE_QUIT_IT
);
442 } else if (btn_hovering
== MENU_START
) {
443 play_sample(VOICE_NEW_GAME
);
445 play_sample(SOUND_MOUSEOVER
);
448 play_sample(SOUND_MOUSEOVER
);
453 if( keys
[SDLK_ESCAPE
] || keys
['q']) {
454 /* FIXME: do the state thingie! */
459 SDL_WM_ToggleFullScreen( screen
);
462 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
463 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
464 show_sprite( GR_RACKET
, ((mb
&SDL_BUTTON( SDL_BUTTON_LEFT
))>0)+(((mb
&SDL_BUTTON( SDL_BUTTON_RIGHT
))>0)*2), 4, mx
, my
, 255);
468 /* Draw the "real" mouse coordinates */
469 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
471 /* Store the screen, because we are fading after this screen update */
472 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
476 if( mb
& SDL_BUTTON(SDL_BUTTON_LEFT
)) {
477 mouse_pressed
= true;
478 } else if (mouse_pressed
== true) {
479 /* Mouse button released */
480 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
481 #ifdef HAVE_VOICE_FILES
482 if (btn_hovering
== MENU_START
) {
483 play_sample(VOICE_LETS_GO
);
485 play_sample(SOUND_MOUSECLICK
);
488 play_sample(SOUND_MOUSECLICK
);
491 if (state
== MENU_STATE_MAINMENU
) {
492 switch (btn_hovering
) {
494 state
= MENU_STATE_SLIDE_TO_OPTIONS
;
497 state
= MENU_STATE_SLIDE_TO_RESUME
;
500 state
= MENU_STATE_SLIDE_TO_QUIT
;
503 } else if (state
== MENU_STATE_OPTIONS
) {
504 switch (btn_hovering
) {
506 state
= MENU_STATE_SLIDE_TO_GAME
;
509 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
511 case MENU_COURT_CHANGE
:
512 prepared_game
->court_type
++;
513 if (prepared_game
->court_type
> GR_CTT_LAST
) {
514 prepared_game
->court_type
= GR_CTT_FIRST
;
518 switch (prepared_game
->player1
.type
) {
519 case PLAYER_TYPE_HUMAN
:
520 prepared_game
->player1
.type
= PLAYER_TYPE_AI
;
521 btn_player1
.text
= "Computer (AI)";
524 prepared_game
->player1
.type
= PLAYER_TYPE_HUMAN
;
525 btn_player1
.text
= "Keyboard (W-S-D)";
530 switch (prepared_game
->player2
.type
) {
531 case PLAYER_TYPE_HUMAN
:
532 prepared_game
->player2
.type
= PLAYER_TYPE_AI
;
533 btn_player2
.text
= "Computer (AI)";
536 prepared_game
->player2
.type
= PLAYER_TYPE_HUMAN
;
537 btn_player2
.text
= "Keyboard (O-L-K)";
543 mouse_pressed
= false;
546 #ifdef ENABLE_FPS_LIMIT
547 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {