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
));
298 #ifdef ENABLE_FPS_LIMIT
303 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
305 case MENU_STATE_SLIDE_TO_QUIT
:
308 state
= MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
;
310 case MENU_STATE_SLIDE_TO_QUIT_IN_PROGRESS
:
311 if (slide
== slide_start
) {
312 state
= MENU_STATE_QUIT
;
315 case MENU_STATE_QUIT
:
319 fprintf(stderr
, "State error: %d\n", state
);
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) {
331 ticks
= SDL_GetTicks();
336 if (state
!= MENU_STATE_OPTIONS
) {
337 fill_image_offset(GR_FOG
, 0, 0, WIDTH
, HEIGHT
, -i
, 0);
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
) {
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);
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);
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);
369 if( e
.type
== SDL_QUIT
) {
370 state
= MENU_STATE_SLIDE_TO_QUIT
;
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
;
394 /* No menu screen - no hovering. */
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! */
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);
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();*/
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
) {
434 state
= MENU_STATE_SLIDE_TO_OPTIONS
;
437 state
= MENU_STATE_SLIDE_TO_RESUME
;
440 state
= MENU_STATE_SLIDE_TO_QUIT
;
443 } else if (state
== MENU_STATE_OPTIONS
) {
444 switch (btn_hovering
) {
446 state
= MENU_STATE_SLIDE_TO_GAME
;
449 state
= MENU_STATE_SLIDE_TO_MAINMENU
;
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
;
459 mouse_pressed
= false;
462 #ifdef ENABLE_FPS_LIMIT
463 while (frames
*1000.0/((float)(SDL_GetTicks()-ft
+1))>(float)(DEFAULT_FPS
)) {