5 * Copyright (C) 2003, 2007 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,
40 static const char* help_text
[] = {
41 "player 1 moves with <w>, <s> and <d>",
42 "player 2 moves with <o>, <l> and <k>",
43 "switch court in game with <c>, pause with <p>",
44 "" /* joystick help text comes here */,
47 /* Number of lines in help_text */
49 /* Height (in pixels) of the help text scroller */
54 /* IDs from the resource file */
55 #define START_BUTTON 1
56 #define CHECKBOX_FULLSCREEN 2
59 BOOL CALLBACK
ConfigDialogProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
61 static int checkbox_is_checked
;
65 EndDialog(hwndDlg
, IDCANCEL
);
70 EndDialog(hwndDlg
, (checkbox_is_checked
)?(IDYES
):(IDNO
));
73 EndDialog(hwndDlg
, IDCANCEL
);
75 case CHECKBOX_FULLSCREEN
:
76 checkbox_is_checked
^= 1;
88 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
89 LPSTR lpCmdLine
, int nCmdShow
) {
91 int main( int argc
, char** argv
) {
98 int sdl_flags
= SDL_SWSURFACE
;
100 char copyright_line
[100];
101 int copyright_line_width
;
102 int help
, help_line_widths
[HELP_LINES
], help_line_height
, help_offset
;
104 Point elektrons
[ELEKTRONS
];
109 mb_result
= DialogBox(hInstance
, "CONFIG", 0, (DLGPROC
)ConfigDialogProc
);
113 sdl_flags
|= SDL_FULLSCREEN
;
122 fprintf( stderr
, "Tennix %s\n%s\n%s\n\n", VERSION
, COPYRIGHT
, URL
);
124 bool do_help
= false;
127 /* A poor/lazy man's getopt */
128 #define OPTION_SET(longopt,shortopt) \
129 (strcmp(argv[i], longopt)==0 || strcmp(argv[i], shortopt)==0)
130 #define OPTION_VALUE \
131 ((i+1 < argc)?(argv[i+1]):(NULL))
132 #define OPTION_VALUE_PROCESSED \
134 if (OPTION_SET("--fullscreen", "-f")) {
135 sdl_flags
|= SDL_FULLSCREEN
;
137 else if (OPTION_SET("--help", "-h")) {
140 else if (OPTION_SET("--list-joysticks", "-J")) {
141 SDL_Init(SDL_INIT_JOYSTICK
);
145 else if (OPTION_SET("--joystick", "-j")) {
146 SDL_Init(SDL_INIT_JOYSTICK
);
147 if (OPTION_VALUE
== NULL
) {
148 fprintf(stderr
, "Error: You need to specify the name of the joystick as parameter.\n");
152 if (joystick_open(OPTION_VALUE
)==0) {
153 fprintf(stderr
, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE
);
156 OPTION_VALUE_PROCESSED
;
159 fprintf(stderr
, "Ignoring unknown option: %s\n", argv
[i
]);
164 if (do_help
== true) {
165 fprintf(stderr
, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv
[0]);
170 sprintf( copyright_line
, "Tennix %s -- %s", VERSION
, COPYRIGHT
);
172 srand( (unsigned)time( NULL
));
174 for( el
=0; el
<ELEKTRONS
; el
++) {
175 elektrons
[el
].x
= elektrons
[el
].y
= 0;
176 elektrons
[el
].w
= 50+(rand()%120);
177 elektrons
[el
].h
= 10+(rand()%10);
178 elektrons
[el
].current_x
= WIDTH
/2;
179 elektrons
[el
].current_y
= HEIGHT
/2;
180 elektrons
[el
].new_x
= rand()%WIDTH
;
181 elektrons
[el
].new_y
= rand()%HEIGHT
;
182 elektrons
[el
].phase
= 4*2*PI
*((rand()%1000)/1000.0);
183 elektrons
[el
].r
= 100+rand()%155;
184 elektrons
[el
].g
= 100+rand()%155;
186 elektrons
[el
].size
= 0;
187 elektrons
[el
].new_size
= 5+rand()%10;
190 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_JOYSTICK
) == -1) {
191 fprintf( stderr
, "Can't init SDL: %s\n", SDL_GetError());
195 SDL_VideoInfo
* vi
= (SDL_VideoInfo
*)SDL_GetVideoInfo();
196 if( (screen
= SDL_SetVideoMode( WIDTH
, HEIGHT
, vi
->vfmt
->BitsPerPixel
, sdl_flags
)) == NULL
) {
197 fprintf( stderr
, "Can't set video mode: %s\n", SDL_GetError());
201 SDL_WM_SetCaption( "Tennix! SDL", "Tennix");
202 SDL_ShowCursor( SDL_DISABLE
);
203 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY
, 1);
208 help_text
[3] = get_joystick_help();
210 play_sample_background(SOUND_BACKGROUND
);
212 copyright_line_width
= font_get_string_width( GR_DKC2_FONT
, copyright_line
);
213 for( help
=0; help
<HELP_LINES
; help
++) {
214 help_line_widths
[help
] = font_get_string_width( GR_SMALLISH_FONT
, help_text
[help
]);
216 help_line_height
= get_image_height( GR_SMALLISH_FONT
);
219 show_image(GR_MENU
, WIDTH
/2-get_image_width(GR_MENU
)/2, 0, 255);
225 if( e
.type
== SDL_QUIT
) {
228 keys
= SDL_GetKeyState( NULL
);
229 mb
= SDL_GetMouseState( &mx
, &my
);
231 if( keys
[SDLK_ESCAPE
] || keys
['q']) {
236 SDL_WM_ToggleFullScreen( screen
);
240 for( el
=0; el
<ELEKTRONS
; el
++) {
241 elektrons
[el
].new_x
= WIDTH
/2 - ELEKTRONS_WIDTH
;
242 elektrons
[el
].new_y
= highlight_y
;
243 if( elektrons
[el
].new_size
>= 5) {
244 elektrons
[el
].new_size
= 1+rand()%4;
249 for( el
=0; el
<ELEKTRONS
; el
++) {
250 elektrons
[el
].current_x
+= (1.0*(elektrons
[el
].new_x
-elektrons
[el
].current_x
)/ELEKTRON_LAZYNESS
);
251 elektrons
[el
].current_y
+= (1.0*(elektrons
[el
].new_y
-elektrons
[el
].current_y
)/ELEKTRON_LAZYNESS
);
252 if( i
%4==0 && elektrons
[el
].size
< elektrons
[el
].new_size
) {
253 elektrons
[el
].size
++;
255 if( i
%4==0 && elektrons
[el
].size
> elektrons
[el
].new_size
) {
256 elektrons
[el
].size
--;
258 elektrons
[el
].phase
+= 0.02+(rand()%20/100.0);
259 elektrons
[el
].x
= elektrons
[el
].current_x
+ ELEKTRONS_WIDTH
+ elektrons
[el
].w
*cosf(elektrons
[el
].phase
/4);
260 elektrons
[el
].y
= elektrons
[el
].current_y
+ elektrons
[el
].h
*sinf(elektrons
[el
].phase
);
263 if( M_POS_START_GAME(mx
,my
)) {
264 highlight_y
= HIGHLIGHT_START_GAME
;
265 } else if( M_POS_START_MULTI(mx
,my
)) {
266 highlight_y
= HIGHLIGHT_START_MULTI
;
267 /*} else if( M_POS_CREDITS(mx,my)) {
268 highlight_y = HIGHLIGHT_CREDITS;
269 */} else if( M_POS_QUIT(mx
,my
)) {
270 highlight_y
= HIGHLIGHT_QUIT
;
271 } else if( highlight_y
== 0) {
273 for( el
=0; el
<ELEKTRONS
; el
++) {
274 elektrons
[el
].new_size
= 5+rand()%10;
278 for( el
=0; el
<ELEKTRONS
; el
++) {
279 elektrons
[el
].new_x
= rand()%(WIDTH
-ELEKTRONS_WIDTH
-elektrons
[el
].w
/2);
280 elektrons
[el
].new_y
= rand()%HEIGHT
;
281 elektrons
[el
].new_size
= 5+rand()%10;
286 for( el
=0; el
<ELEKTRONS
; el
++) {
287 if( elektrons
[el
].size
> 0) {
288 rectangle( elektrons
[el
].x
, elektrons
[el
].y
, elektrons
[el
].size
, elektrons
[el
].size
, elektrons
[el
].r
, elektrons
[el
].g
, elektrons
[el
].b
);
289 if( elektrons
[el
].size
> 4) {
290 if( highlight_y
!= 0 || rand()%10 == 0) {
291 //draw_line_faded( elektrons[el].x+elektrons[el].size/2, elektrons[el].y+elektrons[el].size/2, mx+15, my+24, elektrons[el].r/3, elektrons[el].g/3, 0, 0 ,0 ,0);
293 rectangle( elektrons
[el
].x
+2, elektrons
[el
].y
+2, elektrons
[el
].size
-4, elektrons
[el
].size
-4, 0, 0, 0);
298 font_draw_string( GR_DKC2_FONT
, copyright_line
, (WIDTH
-copyright_line_width
)/2, HEIGHT
-35, i
, ((i
/150)%ANIMATION_COUNT
));
299 for( help
=0; help
<HELP_LINES
; help
++) {
300 help_offset
= (help_line_height
*(HELP_LINES
-help
)+i
/3)%HELP_PHASE
;
301 font_draw_string_alpha( GR_SMALLISH_FONT
, help_text
[help
],
302 (WIDTH
-help_line_widths
[help
])/2,
303 HEIGHT
/2+45-help_offset
,
305 255.0*fabsf( sinf( help_offset
/((float)HELP_PHASE
)*M_PI
))
308 show_sprite( GR_RACKET
, ((mb
&SDL_BUTTON( SDL_BUTTON_LEFT
))>0)+(((mb
&SDL_BUTTON( SDL_BUTTON_RIGHT
))>0)*2), 4, mx
, my
, 255);
311 if( mb
& SDL_BUTTON( SDL_BUTTON_LEFT
)) {
312 if( M_POS_START_GAME(mx
,my
)) {
313 stop_sample(SOUND_BACKGROUND
);
314 play_sample_loop(SOUND_AUDIENCE
);
319 while( SDL_PollEvent( &e
));
320 stop_sample(SOUND_AUDIENCE
);
321 play_sample_background(SOUND_BACKGROUND
);
323 show_image(GR_MENU
, WIDTH
/2-get_image_width(GR_MENU
)/2, 0, 255);
326 if( M_POS_START_MULTI(mx
,my
)) {
327 stop_sample(SOUND_BACKGROUND
);
328 play_sample_loop(SOUND_AUDIENCE
);
333 while( SDL_PollEvent( &e
));
334 stop_sample(SOUND_AUDIENCE
);
335 play_sample_background(SOUND_BACKGROUND
);
337 show_image(GR_MENU
, WIDTH
/2-get_image_width(GR_MENU
)/2, 0, 255);
340 if( M_POS_CREDITS(mx
,my
)) {
341 //introimage( "data/credits.bmp");
343 if( M_POS_QUIT(mx
,my
)) {
344 stop_sample(SOUND_BACKGROUND
);
354 while( is_fading()) {