Remove some development code from makefile
[tennix.git] / tennix.c
blob074cda84baf2d048626e8b4fa8394bb9c9664ce6
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007 Thomas Perl <thp@perli.net>
6 *
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,
20 * MA 02110-1301, USA.
22 **/
24 #include <stdio.h>
25 #include <time.h>
26 #include <libgen.h>
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
32 #include "tennix.h"
33 #include "game.h"
34 #include "graphics.h"
35 #include "sound.h"
36 #include "input.h"
38 SDL_Surface *screen;
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 */
48 #define HELP_LINES 4
49 /* Height (in pixels) of the help text scroller */
50 #define HELP_PHASE 85
52 #ifdef WIN32
54 /* IDs from the resource file */
55 #define START_BUTTON 1
56 #define CHECKBOX_FULLSCREEN 2
57 #define QUIT_BUTTON 3
59 BOOL CALLBACK ConfigDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
61 static int checkbox_is_checked;
63 switch (uMsg) {
64 case WM_CLOSE:
65 EndDialog(hwndDlg, IDCANCEL);
66 break;
67 case WM_COMMAND:
68 switch (wParam) {
69 case START_BUTTON:
70 EndDialog(hwndDlg, (checkbox_is_checked)?(IDYES):(IDNO));
71 break;
72 case QUIT_BUTTON:
73 EndDialog(hwndDlg, IDCANCEL);
74 break;
75 case CHECKBOX_FULLSCREEN:
76 checkbox_is_checked ^= 1;
77 break;
79 break;
80 default:
81 return FALSE;
83 return TRUE;
85 #endif
87 #ifdef WIN32
88 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
89 LPSTR lpCmdLine, int nCmdShow) {
90 #else
91 int main( int argc, char** argv) {
92 #endif
93 int i;
94 int mx, my;
95 Uint8 *keys;
96 Uint8 mb;
97 SDL_Event e;
98 int sdl_flags = SDL_SWSURFACE;
99 int highlight_y = 0;
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];
105 int el;
107 #ifdef WIN32
108 int mb_result;
109 mb_result = DialogBox(hInstance, "CONFIG", 0, (DLGPROC)ConfigDialogProc);
111 switch (mb_result) {
112 case IDYES:
113 sdl_flags |= SDL_FULLSCREEN;
114 break;
115 case IDCANCEL:
116 return 0;
117 break;
118 default:
119 break;
121 #else
122 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
124 bool do_help = false;
125 i = 1;
126 while (i < argc) {
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 \
133 (i++)
134 if (OPTION_SET("--fullscreen", "-f")) {
135 sdl_flags |= SDL_FULLSCREEN;
137 else if (OPTION_SET("--help", "-h")) {
138 do_help = true;
140 else if (OPTION_SET("--list-joysticks", "-J")) {
141 SDL_Init(SDL_INIT_JOYSTICK);
142 joystick_list();
143 return 0;
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");
149 do_help = true;
150 break;
152 if (joystick_open(OPTION_VALUE)==0) {
153 fprintf(stderr, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE);
154 break;
156 OPTION_VALUE_PROCESSED;
158 else {
159 fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]);
161 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]);
166 return 0;
168 #endif
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;
185 elektrons[el].b = 0;
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());
192 exit( 1);
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());
198 exit( 1);
201 SDL_WM_SetCaption( "Tennix! SDL", "Tennix");
202 SDL_ShowCursor( SDL_DISABLE);
203 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
205 init_sound();
206 init_graphics();
207 init_joystick();
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);
218 clear_screen();
219 show_image(GR_MENU, WIDTH/2-get_image_width(GR_MENU)/2, 0, 255);
220 store_screen();
222 i = 0;
223 while( 1) {
224 SDL_PollEvent( &e);
225 if( e.type == SDL_QUIT) {
226 break;
228 keys = SDL_GetKeyState( NULL);
229 mb = SDL_GetMouseState( &mx, &my);
231 if( keys[SDLK_ESCAPE] || keys['q']) {
232 break;
235 if( keys['f']) {
236 SDL_WM_ToggleFullScreen( screen);
239 if( highlight_y) {
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) {
272 if( i%20 == 0) {
273 for( el=0; el<ELEKTRONS; el++) {
274 elektrons[el].new_size = 5+rand()%10;
277 } else {
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;
283 highlight_y = 0;
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,
304 i, ANIMATION_NONE,
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);
309 updatescr();
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);
315 start_fade();
316 game( true);
317 SDL_Delay( 150);
318 start_fade();
319 while( SDL_PollEvent( &e));
320 stop_sample(SOUND_AUDIENCE);
321 play_sample_background(SOUND_BACKGROUND);
322 clear_screen();
323 show_image(GR_MENU, WIDTH/2-get_image_width(GR_MENU)/2, 0, 255);
324 store_screen();
326 if( M_POS_START_MULTI(mx,my)) {
327 stop_sample(SOUND_BACKGROUND);
328 play_sample_loop(SOUND_AUDIENCE);
329 start_fade();
330 game( false);
331 SDL_Delay( 150);
332 start_fade();
333 while( SDL_PollEvent( &e));
334 stop_sample(SOUND_AUDIENCE);
335 play_sample_background(SOUND_BACKGROUND);
336 clear_screen();
337 show_image(GR_MENU, WIDTH/2-get_image_width(GR_MENU)/2, 0, 255);
338 store_screen();
340 if( M_POS_CREDITS(mx,my)) {
341 //introimage( "data/credits.bmp");
343 if( M_POS_QUIT(mx,my)) {
344 stop_sample(SOUND_BACKGROUND);
345 break;
348 i++;
349 SDL_Delay( 4);
352 start_fade();
353 store_screen();
354 while( is_fading()) {
355 clear_screen();
356 updatescr();
357 SDL_Delay( 10);
360 uninit_graphics();
361 uninit_joystick();
363 SDL_Quit();
364 return 0;