Updated TODO list (add ChangeLog on release)
[tennix.git] / tennix.c
blob0ae7d7987e5b75fcea0ff03a69bf46be32b06280
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 | SDL_DOUBLEBUF;
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 i = 0;
219 while( 1) {
220 SDL_PollEvent( &e);
221 if( e.type == SDL_QUIT) {
222 break;
224 keys = SDL_GetKeyState( NULL);
225 mb = SDL_GetMouseState( &mx, &my);
227 if( keys[SDLK_ESCAPE] || keys['q']) {
228 break;
231 if( keys['f']) {
232 SDL_WM_ToggleFullScreen( screen);
235 clearscr();
236 if( highlight_y) {
237 for( el=0; el<ELEKTRONS; el++) {
238 elektrons[el].new_x = WIDTH/2 - ELEKTRONS_WIDTH;
239 elektrons[el].new_y = highlight_y;
240 if( elektrons[el].new_size >= 5) {
241 elektrons[el].new_size = 1+rand()%4;
246 for( el=0; el<ELEKTRONS; el++) {
247 elektrons[el].current_x += (1.0*(elektrons[el].new_x-elektrons[el].current_x)/ELEKTRON_LAZYNESS);
248 elektrons[el].current_y += (1.0*(elektrons[el].new_y-elektrons[el].current_y)/ELEKTRON_LAZYNESS);
249 if( i%4==0 && elektrons[el].size < elektrons[el].new_size) {
250 elektrons[el].size++;
252 if( i%4==0 && elektrons[el].size > elektrons[el].new_size) {
253 elektrons[el].size--;
255 elektrons[el].phase += 0.02+(rand()%20/100.0);
256 elektrons[el].x = elektrons[el].current_x + ELEKTRONS_WIDTH + elektrons[el].w*cosf(elektrons[el].phase/4);
257 elektrons[el].y = elektrons[el].current_y + elektrons[el].h*sinf(elektrons[el].phase);
260 if( M_POS_START_GAME(mx,my)) {
261 highlight_y = HIGHLIGHT_START_GAME;
262 } else if( M_POS_START_MULTI(mx,my)) {
263 highlight_y = HIGHLIGHT_START_MULTI;
264 /*} else if( M_POS_CREDITS(mx,my)) {
265 highlight_y = HIGHLIGHT_CREDITS;
266 */} else if( M_POS_QUIT(mx,my)) {
267 highlight_y = HIGHLIGHT_QUIT;
268 } else if( highlight_y == 0) {
269 if( i%20 == 0) {
270 for( el=0; el<ELEKTRONS; el++) {
271 elektrons[el].new_size = 5+rand()%10;
274 } else {
275 for( el=0; el<ELEKTRONS; el++) {
276 elektrons[el].new_x = rand()%(WIDTH-ELEKTRONS_WIDTH-elektrons[el].w/2);
277 elektrons[el].new_y = rand()%HEIGHT;
278 elektrons[el].new_size = 5+rand()%10;
280 highlight_y = 0;
283 for( el=0; el<ELEKTRONS; el++) {
284 if( elektrons[el].size > 0) {
285 rectangle( elektrons[el].x, elektrons[el].y, elektrons[el].size, elektrons[el].size, elektrons[el].r, elektrons[el].g, elektrons[el].b);
286 if( elektrons[el].size > 4) {
287 if( highlight_y != 0 || rand()%10 == 0) {
288 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);
290 rectangle( elektrons[el].x+2, elektrons[el].y+2, elektrons[el].size-4, elektrons[el].size-4, 0, 0, 0);
295 show_image( GR_MENU, WIDTH/2-get_image_width( GR_MENU)/2, 0, 255);
296 font_draw_string( GR_DKC2_FONT, copyright_line, (WIDTH-copyright_line_width)/2, HEIGHT-35, i, ((i/150)%ANIMATION_COUNT));
297 for( help=0; help<HELP_LINES; help++) {
298 help_offset = (help_line_height*(HELP_LINES-help)+i/3)%HELP_PHASE;
299 font_draw_string_alpha( GR_SMALLISH_FONT, help_text[help],
300 (WIDTH-help_line_widths[help])/2,
301 HEIGHT/2+45-help_offset,
302 i, ANIMATION_NONE,
303 255.0*fabsf( sinf( help_offset/((float)HELP_PHASE)*M_PI))
306 show_sprite( GR_RACKET, ((mb&SDL_BUTTON( SDL_BUTTON_LEFT))>0)+(((mb&SDL_BUTTON( SDL_BUTTON_RIGHT))>0)*2), 4, mx, my, 255);
307 updatescr();
309 if( mb & SDL_BUTTON( SDL_BUTTON_LEFT)) {
310 if( M_POS_START_GAME(mx,my)) {
311 stop_sample(SOUND_BACKGROUND);
312 play_sample_loop(SOUND_AUDIENCE);
313 start_fade();
314 game( true);
315 SDL_Delay( 150);
316 start_fade();
317 while( SDL_PollEvent( &e));
318 stop_sample(SOUND_AUDIENCE);
319 play_sample_background(SOUND_BACKGROUND);
321 if( M_POS_START_MULTI(mx,my)) {
322 stop_sample(SOUND_BACKGROUND);
323 play_sample_loop(SOUND_AUDIENCE);
324 start_fade();
325 game( false);
326 SDL_Delay( 150);
327 start_fade();
328 while( SDL_PollEvent( &e));
329 stop_sample(SOUND_AUDIENCE);
330 play_sample_background(SOUND_BACKGROUND);
332 if( M_POS_CREDITS(mx,my)) {
333 //introimage( "data/credits.bmp");
335 if( M_POS_QUIT(mx,my)) {
336 stop_sample(SOUND_BACKGROUND);
337 break;
340 i++;
341 SDL_Delay( 4);
344 start_fade();
345 while( is_fading()) {
346 clearscr();
347 updatescr();
348 SDL_Delay( 10);
351 uninit_graphics();
352 uninit_joystick();
354 SDL_Quit();
355 return 0;