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
,
116 int state
= MENU_STATE_STARTED
;
118 #ifdef ENABLE_FPS_LIMIT
119 Uint32 ft
, frames
; /* frame timer and frames */
123 sdl_flags
|= SDL_FULLSCREEN
;
128 mb_result
= DialogBox(hInstance
, "CONFIG", 0, (DLGPROC
)ConfigDialogProc
);
132 sdl_flags
|= SDL_FULLSCREEN
;
141 fprintf( stderr
, "Tennix %s\n%s\n%s\n\n", VERSION
, COPYRIGHT
, URL
);
143 bool do_help
= false;
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 \
153 if (OPTION_SET("--fullscreen", "-f")) {
154 sdl_flags
|= SDL_FULLSCREEN
;
156 else if (OPTION_SET("--help", "-h")) {
159 else if (OPTION_SET("--list-joysticks", "-J")) {
160 SDL_Init(SDL_INIT_JOYSTICK
);
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");
171 if (joystick_open(OPTION_VALUE
)==0) {
172 fprintf(stderr
, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE
);
175 OPTION_VALUE_PROCESSED
;
178 fprintf(stderr
, "Ignoring unknown option: %s\n", argv
[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]);
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());
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());
202 SDL_WM_SetCaption( "Tennix " VERSION
, "Tennix");
203 SDL_ShowCursor( SDL_DISABLE
);
204 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY
, 1);
210 #ifdef ENABLE_FPS_LIMIT
216 /* Sliding initialization */
217 ticks
= SDL_GetTicks();
218 slide
= slide_start
= get_image_width(GR_SIDEBAR
);
221 /* State transitions */
223 case MENU_STATE_STARTED
:
224 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
226 case MENU_STATE_SLIDE_TO_MAINMENU
:
228 slide_direction
= -1;
229 state
= MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
;
231 case MENU_STATE_SLIDE_TO_MAINMENU_IN_PROGRESS
:
234 state
= MENU_STATE_MAINMENU
;
237 case MENU_STATE_MAINMENU
:
239 prepared_game
= NULL
;
241 case MENU_STATE_SLIDE_TO_OPTIONS
:
244 state
= MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
;
246 case MENU_STATE_SLIDE_TO_OPTIONS_IN_PROGRESS
:
247 if (slide
== slide_start
) {
249 state
= MENU_STATE_OPTIONS
;
252 case MENU_STATE_OPTIONS
:
253 /* Prepare a new game */
254 if (prepared_game
== NULL
) {
255 prepared_game
= gamestate_new();
258 case MENU_STATE_SLIDE_TO_GAME
:
261 state = MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS;
263 case MENU_STATE_SLIDE_TO_GAME_IN_PROGRESS:
264 if (slide == slide_start) {
265 state = MENU_STATE_GAME;
267 state
= MENU_STATE_GAME
;
269 case MENU_STATE_SLIDE_TO_RESUME
:
272 state
= MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
;
274 case MENU_STATE_SLIDE_TO_RESUME_IN_PROGRESS
:
275 if (slide
== slide_start
) {
276 state
= MENU_STATE_RESUME
;
279 case MENU_STATE_GAME
:
280 if (prepared_game
== NULL
) {
281 fprintf(stderr
, "Game not yet prepared!\n");
284 /* Cancel a possibly started 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");
295 gameloop(current_game
);
297 while(SDL_PollEvent(&e
));
299 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
301 case MENU_STATE_SLIDE_TO_QUIT
:
304 state
= MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
;
306 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
:
307 if (slide
== slide_start
) {
308 state
= MENU_STATE_QUIT
;
311 case MENU_STATE_QUIT
:
315 fprintf(stderr
, "State error: %d\n", state
);
320 if (SDL_GetTicks() > ticks
+ 20) {
321 if (slide
>= 1 && slide
<= slide_start
) {
322 slide
+= slide_direction
+(slide_direction
*slide
/(sqrt(2*slide
)));
323 slide
= MAX(0, MIN(slide_start
, slide
));
324 } else if (slide_direction
!= 0) {
327 ticks
= SDL_GetTicks();
331 show_image(GR_SIDEBAR
, WIDTH
-get_image_width(GR_SIDEBAR
)+slide
, 0, 255);
332 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20-slide
, 255);
333 if (state
!= MENU_STATE_OPTIONS
) {
335 show_image(GR_BTN_PLAY
, WIDTH
-get_image_width(GR_BTN_PLAY
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_START
)), 150, 255);
336 if (current_game
!= NULL
) {
337 show_image(GR_BTN_RESUME
, WIDTH
-get_image_width(GR_BTN_RESUME
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_RESUME
)), 230, 255);
338 if (current_game
->player2
.type
== PLAYER_TYPE_AI
) {
339 font_draw_string(GR_DKC2_FONT
, "Game Against The Computer", 10, 10, 0, 0);
341 font_draw_string(GR_DKC2_FONT
, "Multiplayer Versus Game", 10, 10, 0, 0);
343 font_draw_string(GR_DKC2_FONT
, current_game
->sets_score_str
, 10, 40, 0, 0);
345 show_image(GR_BTN_QUIT
, WIDTH
-get_image_width(GR_BTN_QUIT
)+slide
+(slide
/7)+3-(3*(btn_hovering
==MENU_QUIT
)), 350, 255);
348 show_image(GR_TENNIXLOGO
, WIDTH
-get_image_width(GR_SIDEBAR
)-10, 20, 255);
349 draw_button_object(btn_back
, mx
, my
);
350 draw_button_object(btn_start
, mx
, my
);
351 if (prepared_game
!= NULL
) {
352 fill_image(prepared_game
->court_type
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, get_image_width(GR_STADIUM
), get_image_height(GR_STADIUM
));
353 show_image(GR_STADIUM
, MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
*2, 255);
354 draw_button_object(btn_court_change
, mx
, my
);
355 font_draw_string(GR_DKC2_FONT
, "Location", MENU_OPTIONS_BORDER
, MENU_OPTIONS_BORDER
, 0, 0);
360 if( e
.type
== SDL_QUIT
) {
361 state
= MENU_STATE_SLIDE_TO_QUIT
;
365 keys
= SDL_GetKeyState( NULL
);
366 mb
= SDL_GetMouseState( &mx
, &my
);
368 btn_hovering_old
= btn_hovering
;
369 if (state
== MENU_STATE_MAINMENU
) {
370 btn_hovering
= M_POS_DECODE(mx
, my
);
371 if (current_game
== NULL
) {
372 btn_hovering
&= ~MENU_RESUME
;
374 } else if (state
== MENU_STATE_OPTIONS
) {
375 if (M_POS_BUTTON(btn_back
, mx
, my
)) {
376 btn_hovering
= MENU_QUIT
;
377 } else if (M_POS_BUTTON(btn_start
, mx
, my
)) {
378 btn_hovering
= MENU_START
;
379 } else if (M_POS_BUTTON(btn_court_change
, mx
, my
)) {
380 btn_hovering
= MENU_COURT_CHANGE
;
385 /* No menu screen - no hovering. */
388 if (btn_hovering_old
!= btn_hovering
&& btn_hovering
!= 0) {
389 play_sample(SOUND_MOUSEOVER
);
392 if( keys
[SDLK_ESCAPE
] || keys
['q']) {
393 /* FIXME: do the state thingie! */
398 SDL_WM_ToggleFullScreen( screen
);
401 #ifndef MAEMO /* No mouse cursor on Maemo (we have a touchscreen) */
402 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
403 show_sprite( GR_RACKET
, ((mb
&SDL_BUTTON( SDL_BUTTON_LEFT
))>0)+(((mb
&SDL_BUTTON( SDL_BUTTON_RIGHT
))>0)*2), 4, mx
, my
, 255);
407 /* Draw the "real" mouse coordinates */
408 /*rectangle(mx-1, my-1, 2, 2, 255, 255, 255);*/
410 /* Store the screen, because we are fading after this screen update */
411 /*if (!(mb & SDL_BUTTON(SDL_BUTTON_LEFT)) && btn_hovering != MENU_NONE && mouse_pressed == true) store_screen();*/
415 if( mb
& SDL_BUTTON(SDL_BUTTON_LEFT
)) {
416 mouse_pressed
= true;
417 } else if (mouse_pressed
== true) {
418 /* Mouse button released */
419 if (state
== MENU_STATE_MAINMENU
|| state
== MENU_STATE_OPTIONS
) {
420 play_sample(SOUND_MOUSECLICK
);
422 if (state
== MENU_STATE_MAINMENU
) {
423 switch (btn_hovering
) {
425 state
= MENU_STATE_SLIDE_TO_OPTIONS
;
428 state
= MENU_STATE_SLIDE_TO_RESUME
;
431 state
= MENU_STATE_SLIDE_TO_QUIT
;
434 } else if (state
== MENU_STATE_OPTIONS
) {
435 switch (btn_hovering
) {
437 state
= MENU_STATE_SLIDE_TO_GAME
;
440 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
442 case MENU_COURT_CHANGE
:
443 prepared_game
->court_type
++;
444 if (prepared_game
->court_type
> GR_CTT_LAST
) {
445 prepared_game
->court_type
= GR_CTT_FIRST
;
450 mouse_pressed
= false;
453 #ifdef ENABLE_FPS_LIMIT
454 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {