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,
42 /* IDs from the resource file */
43 #define START_BUTTON 1
44 #define CHECKBOX_FULLSCREEN 2
47 BOOL CALLBACK
ConfigDialogProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
49 static int checkbox_is_checked
;
53 EndDialog(hwndDlg
, IDCANCEL
);
58 EndDialog(hwndDlg
, (checkbox_is_checked
)?(IDYES
):(IDNO
));
61 EndDialog(hwndDlg
, IDCANCEL
);
63 case CHECKBOX_FULLSCREEN
:
64 checkbox_is_checked
^= 1;
76 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
77 LPSTR lpCmdLine
, int nCmdShow
) {
79 int main( int argc
, char** argv
) {
81 int i
, slide
, slide_direction
;
87 int sdl_flags
= SDL_SWSURFACE
;
88 int btn_hovering
= 0, btn_hovering_old
= 0;
90 bool mouse_pressed
= false;
92 GameState
*current_game
= NULL
, *prepared_game
= NULL
;
94 MenuButton btn_back
= {
97 HEIGHT
-(MENU_OPTIONS_BORDER
+MENU_OPTIONS_BUTTON_HEIGHT
),
98 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
101 MenuButton btn_start
= {
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
,
108 MenuButton btn_court_change
= {
112 50, MENU_OPTIONS_BUTTON_HEIGHT
,
115 MenuButton btn_player1
= {
119 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
122 MenuButton btn_player2
= {
125 180+MENU_OPTIONS_BORDER
*2+MENU_OPTIONS_BUTTON_HEIGHT
,
126 MENU_OPTIONS_BUTTON_WIDTH
, MENU_OPTIONS_BUTTON_HEIGHT
,
130 int state
= MENU_STATE_STARTED
;
132 #ifdef ENABLE_FPS_LIMIT
133 Uint32 ft
, frames
; /* frame timer and frames */
137 sdl_flags
|= SDL_FULLSCREEN
;
142 mb_result
= DialogBox(hInstance
, "CONFIG", 0, (DLGPROC
)ConfigDialogProc
);
146 sdl_flags
|= SDL_FULLSCREEN
;
155 fprintf( stderr
, "Tennix %s\n%s\n%s\n\n", VERSION
, COPYRIGHT
, URL
);
157 bool do_help
= false;
160 /* A poor/lazy man's getopt */
161 #define OPTION_SET(longopt,shortopt) \
162 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
163 #define OPTION_VALUE \
164 ((i+1 < argc)?(argv[i+1]):(NULL))
165 #define OPTION_VALUE_PROCESSED \
167 if (OPTION_SET("--fullscreen", "-f")) {
168 sdl_flags
|= SDL_FULLSCREEN
;
170 else if (OPTION_SET("--help", "-h")) {
173 else if (OPTION_SET("--list-joysticks", "-J")) {
174 SDL_Init(SDL_INIT_JOYSTICK
);
178 else if (OPTION_SET("--joystick", "-j")) {
179 SDL_Init(SDL_INIT_JOYSTICK
);
180 if (OPTION_VALUE
== NULL
) {
181 fprintf(stderr
, "Error: You need to specify the name of the joystick as parameter.\n");
185 if (joystick_open(OPTION_VALUE
)==0) {
186 fprintf(stderr
, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE
);
189 OPTION_VALUE_PROCESSED
;
192 fprintf(stderr
, "Ignoring unknown option: %s\n", argv
[i
]);
197 if (do_help
== true) {
198 fprintf(stderr
, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv
[0]);
203 srand( (unsigned)time( NULL
));
205 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_JOYSTICK
) == -1) {
206 fprintf( stderr
, "Can't init SDL: %s\n", SDL_GetError());
210 SDL_VideoInfo
* vi
= (SDL_VideoInfo
*)SDL_GetVideoInfo();
211 if( (screen
= SDL_SetVideoMode( WIDTH
, HEIGHT
, vi
->vfmt
->BitsPerPixel
, sdl_flags
)) == NULL
) {
212 fprintf( stderr
, "Can't set video mode: %s\n", SDL_GetError());
216 SDL_WM_SetCaption( "Tennix " VERSION
, "Tennix");
217 SDL_ShowCursor( SDL_DISABLE
);
218 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY
, 1);
224 #ifdef ENABLE_FPS_LIMIT
230 /* Sliding initialization */
231 ticks
= SDL_GetTicks();
232 slide
= slide_start
= get_image_width(GR_SIDEBAR
);
235 /* State transitions */
237 case MENU_STATE_STARTED
:
238 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
240 case MENU_STATE_SLIDE_TO_MAINMENU
:
242 slide_direction
= -1;
243 state
= MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
;
245 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
:
248 state
= MENU_STATE_MAINMENU
;
251 case MENU_STATE_MAINMENU
:
253 prepared_game
= NULL
;
255 case MENU_STATE_SLIDE_TO_OPTIONS
:
258 state
= MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
;
260 case MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
:
261 if (slide
== slide_start
) {
263 state
= MENU_STATE_OPTIONS
;
266 case MENU_STATE_OPTIONS
:
267 /* Prepare a new game */
268 if (prepared_game
== NULL
) {
269 prepared_game
= gamestate_new();
270 btn_player1
.text
= "Keyboard (W-S-D)";
271 btn_player2
.text
= "Computer (AI)";
274 case MENU_STATE_SLIDE_TO_GAME
:
277 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
279 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
280 if (slide == slide_start) {
281 state = MENU_STATE_GAME;
283 state
= MENU_STATE_GAME
;
285 case MENU_STATE_SLIDE_TO_RESUME
:
288 state
= MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
;
290 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
:
291 if (slide
== slide_start
) {
292 state
= MENU_STATE_RESUME
;
295 case MENU_STATE_GAME
:
296 if (prepared_game
== NULL
) {
297 fprintf(stderr
, "Game not yet prepared!\n");
300 /* Cancel a possibly started game */
302 current_game
= prepared_game
;
303 prepared_game
= NULL
;
304 /* no break - we are continuing with "resume" */
305 case MENU_STATE_RESUME
:
306 if (current_game
== NULL
) {
307 fprintf(stderr
, "Cannot resume game!\n");
311 gameloop(current_game
);
313 while(SDL_PollEvent(&e
));
314 #ifdef ENABLE_FPS_LIMIT
319 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
321 case MENU_STATE_SLIDE_TO_QUIT
:
324 state
= MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
;
326 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
:
327 if (slide
== slide_start
) {
328 state
= MENU_STATE_QUIT
;
331 case MENU_STATE_QUIT
:
335 fprintf(stderr
, "State error: %d\n", state
);
340 if (SDL_GetTicks() > ticks
+ 20) {
341 if (slide
>= 1 && slide
<= slide_start
) {
342 slide
+= slide_direction
+(slide_direction
*slide
/(sqrt(2*slide
)));
343 slide
= MAX(0, MIN(slide_start
, slide
));
344 } else if (slide_direction
!= 0) {
347 ticks
= SDL_GetTicks();
352 if (state
!= MENU_STATE_OPTIONS
) {
353 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -i
, 0);
356 show_image(GR_SIDEBAR
, WIDTH
-get_image_width(GR_SIDEBAR
)+slide
, 0, 255);
357 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20-slide
, 255);
358 if (state
!= MENU_STATE_OPTIONS
) {
360 show_image(GR_BTN_PLAY
, WIDTH
-get_image_width(GR_BTN_PLAY
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_START
)), 150, 255);
361 if (current_game
!= NULL
) {
362 show_image(GR_BTN_RESUME
, WIDTH
-get_image_width(GR_BTN_RESUME
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_RESUME
)), 230, 255);
363 font_draw_string(GR_DKC2_FONT
, "current match:", 10, 10, 0, 0);
364 font_draw_string(GR_DKC2_FONT
, current_game
->sets_score_str
, 10, 40, 0, 0);
366 show_image(GR_BTN_QUIT
, WIDTH
-get_image_width(GR_BTN_QUIT
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_QUIT
)), 350, 255);
369 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20, 255);
370 draw_button_object(btn_back
, mx
, my
);
371 draw_button_object(btn_start
, mx
, my
);
372 if (prepared_game
!= NULL
) {
373 fill_image(prepared_game
->court_type
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, get_image_width(GR_STADIUM
), get_image_height(GR_STADIUM
));
374 show_image(GR_STADIUM
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, 255);
375 draw_button_object(btn_court_change
, mx
, my
);
376 font_draw_string(GR_DKC2_FONT
, "Location", MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
, 0, 0);
377 draw_button_object(btn_player1
, mx
, my
);
378 draw_button_object(btn_player2
, mx
, my
);
379 font_draw_string(GR_DKC2_FONT
, "Player 1", btn_player1
.x
, btn_player1
.y
-MENU_OPTIONS_BORDER
, 0, 0);
380 font_draw_string(GR_DKC2_FONT
, "Player 2", btn_player2
.x
, btn_player2
.y
-MENU_OPTIONS_BORDER
, 0, 0);
385 if( e
.type
== SDL_QUIT
) {
386 state
= MENU_STATE_SLIDE_TO_QUIT
;
390 keys
= SDL_GetKeyState( NULL
);
391 mb
= SDL_GetMouseState( &mx
, &my
);
393 btn_hovering_old
= btn_hovering
;
394 if (state
== MENU_STATE_MAINMENU
) {
395 btn_hovering
= M_POS_DECODE(mx
, my
);
396 if (current_game
== NULL
) {
397 btn_hovering
&= ~MENU_RESUME
;
399 } else if (state
== MENU_STATE_OPTIONS
) {
400 if (M_POS_BUTTON(btn_back
, mx
, my
)) {
401 btn_hovering
= MENU_QUIT
;
402 } else if (M_POS_BUTTON(btn_start
, mx
, my
)) {
403 btn_hovering
= MENU_START
;
404 } else if (M_POS_BUTTON(btn_court_change
, mx
, my
)) {
405 btn_hovering
= MENU_COURT_CHANGE
;
406 } else if (M_POS_BUTTON(btn_player1
, mx
, my
)) {
407 btn_hovering
= MENU_PLAYER1
;
408 } else if (M_POS_BUTTON(btn_player2
, mx
, my
)) {
409 btn_hovering
= MENU_PLAYER2
;
414 /* No menu screen - no hovering. */
417 if (btn_hovering_old
!= btn_hovering
&& btn_hovering
!= 0) {
418 #ifdef HAVE_VOICE_FILES
419 if (btn_hovering
== MENU_QUIT
) {
420 play_sample(VOICE_QUIT_IT
);
421 } else if (btn_hovering
== MENU_START
) {
422 play_sample(VOICE_NEW_GAME
);
424 play_sample(SOUND_MOUSEOVER
);
427 play_sample(SOUND_MOUSEOVER
);
431 if( keys
[SDLK_ESCAPE
] || keys
['q']) {
432 /* FIXME: do the state thingie! */
437 SDL_WM_ToggleFullScreen( screen
);
440 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
441 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
442 show_sprite( GR_RACKET
, ((mb
&SDL_BUTTON( SDL_BUTTON_LEFT
))>0)+(((mb
&SDL_BUTTON( SDL_BUTTON_RIGHT
))>0)*2), 4, mx
, my
, 255);
446 /* Draw the "real" mouse coordinates */
447 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
449 /* Store the screen, because we are fading after this screen update */
450 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
454 if( mb
& SDL_BUTTON(SDL_BUTTON_LEFT
)) {
455 mouse_pressed
= true;
456 } else if (mouse_pressed
== true) {
457 /* Mouse button released */
458 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
459 #ifdef HAVE_VOICE_FILES
460 if (btn_hovering
== MENU_START
) {
461 play_sample(VOICE_LETS_GO
);
463 play_sample(SOUND_MOUSECLICK
);
466 play_sample(SOUND_MOUSECLICK
);
469 if (state
== MENU_STATE_MAINMENU
) {
470 switch (btn_hovering
) {
472 state
= MENU_STATE_SLIDE_TO_OPTIONS
;
475 state
= MENU_STATE_SLIDE_TO_RESUME
;
478 state
= MENU_STATE_SLIDE_TO_QUIT
;
481 } else if (state
== MENU_STATE_OPTIONS
) {
482 switch (btn_hovering
) {
484 state
= MENU_STATE_SLIDE_TO_GAME
;
487 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
489 case MENU_COURT_CHANGE
:
490 prepared_game
->court_type
++;
491 if (prepared_game
->court_type
> GR_CTT_LAST
) {
492 prepared_game
->court_type
= GR_CTT_FIRST
;
496 switch (prepared_game
->player1
.type
) {
497 case PLAYER_TYPE_HUMAN
:
498 prepared_game
->player1
.type
= PLAYER_TYPE_AI
;
499 btn_player1
.text
= "Computer (AI)";
502 prepared_game
->player1
.type
= PLAYER_TYPE_HUMAN
;
503 btn_player1
.text
= "Keyboard (W-S-D)";
508 switch (prepared_game
->player2
.type
) {
509 case PLAYER_TYPE_HUMAN
:
510 prepared_game
->player2
.type
= PLAYER_TYPE_AI
;
511 btn_player2
.text
= "Computer (AI)";
514 prepared_game
->player2
.type
= PLAYER_TYPE_HUMAN
;
515 btn_player2
.text
= "Keyboard (O-L-K)";
521 mouse_pressed
= false;
524 #ifdef ENABLE_FPS_LIMIT
525 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {