Tennix 0.4.1
[tennix.git] / tennix.c
blob62a82a797cfa3042cda8a3b75590601f72bc0929
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 #include "tennix.h"
29 #include "game.h"
30 #include "graphics.h"
31 #include "sound.h"
33 SDL_Surface *screen;
35 static const char* help_text[] = {
36 "keyboard controls:",
37 "player 1 moves with <w>, <s> and <d>",
38 "player 2 moves with <o>, <l> and <k>",
39 "switch court in game with <c>",
42 /* Number of lines in help_text */
43 #define HELP_LINES 4
44 /* Height (in pixels) of the help text scroller */
45 #define HELP_PHASE 50
47 int main( int argc, char** argv) {
48 int i = 0;
49 int mx, my;
50 Uint8 *keys;
51 Uint8 mb;
52 SDL_Event e;
53 SDL_VideoInfo* vi;
54 int sdl_flags = SDL_SWSURFACE | SDL_DOUBLEBUF;
55 char datadir[MAXPATHLEN];
56 int highlight_y = 0;
57 char copyright_line[100];
58 int copyright_line_width;
59 int help, help_line_widths[HELP_LINES], help_line_height, help_offset;
61 Point elektrons[ELEKTRONS];
62 int el;
64 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
66 sprintf( copyright_line, "Tennix %s -- %s", VERSION, COPYRIGHT);
68 srand( (unsigned)time( NULL));
70 for( el=0; el<ELEKTRONS; el++) {
71 elektrons[el].x = elektrons[el].y = 0;
72 elektrons[el].w = 50+(rand()%120);
73 elektrons[el].h = 10+(rand()%10);
74 elektrons[el].current_x = WIDTH/2;
75 elektrons[el].current_y = HEIGHT/2;
76 elektrons[el].new_x = rand()%WIDTH;
77 elektrons[el].new_y = rand()%HEIGHT;
78 elektrons[el].phase = 4*2*PI*((rand()%1000)/1000.0);
79 elektrons[el].r = 100+rand()%155;
80 elektrons[el].g = 100+rand()%155;
81 elektrons[el].b = 0;
82 elektrons[el].size = 0;
83 elektrons[el].new_size = 5+rand()%10;
86 if( SDL_Init( SDL_INIT_VIDEO) == -1) {
87 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
88 exit( 1);
91 atexit( SDL_Quit);
93 vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
95 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
96 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
97 exit( 1);
100 SDL_WM_SetCaption( "Tennix! SDL", "Tennix");
101 SDL_ShowCursor( SDL_DISABLE);
102 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
104 init_sound( datadir);
105 init_graphics( datadir);
107 copyright_line_width = font_get_string_width( GR_DKC2_FONT, copyright_line);
108 for( help=0; help<HELP_LINES; help++) {
109 help_line_widths[help] = font_get_string_width( GR_SMALLISH_FONT, help_text[help]);
111 help_line_height = get_image_height( GR_SMALLISH_FONT);
113 while( 1) {
114 SDL_PollEvent( &e);
115 if( e.type == SDL_QUIT) {
116 break;
118 keys = SDL_GetKeyState( NULL);
119 mb = SDL_GetMouseState( &mx, &my);
121 if( keys[SDLK_ESCAPE] || keys['q']) {
122 break;
125 if( keys['f']) {
126 SDL_WM_ToggleFullScreen( screen);
129 clearscr();
130 if( highlight_y) {
131 for( el=0; el<ELEKTRONS; el++) {
132 elektrons[el].new_x = WIDTH/2 - ELEKTRONS_WIDTH;
133 elektrons[el].new_y = highlight_y;
134 if( elektrons[el].new_size >= 5) {
135 elektrons[el].new_size = 1+rand()%4;
140 for( el=0; el<ELEKTRONS; el++) {
141 elektrons[el].current_x += (1.0*(elektrons[el].new_x-elektrons[el].current_x)/ELEKTRON_LAZYNESS);
142 elektrons[el].current_y += (1.0*(elektrons[el].new_y-elektrons[el].current_y)/ELEKTRON_LAZYNESS);
143 if( i%4==0 && elektrons[el].size < elektrons[el].new_size) {
144 elektrons[el].size++;
146 if( i%4==0 && elektrons[el].size > elektrons[el].new_size) {
147 elektrons[el].size--;
149 elektrons[el].phase += 0.02+(rand()%20/100.0);
150 elektrons[el].x = elektrons[el].current_x + ELEKTRONS_WIDTH + elektrons[el].w*cosf(elektrons[el].phase/4);
151 elektrons[el].y = elektrons[el].current_y + elektrons[el].h*sinf(elektrons[el].phase);
154 if( M_POS_START_GAME(mx,my)) {
155 highlight_y = HIGHLIGHT_START_GAME;
156 } else if( M_POS_START_MULTI(mx,my)) {
157 highlight_y = HIGHLIGHT_START_MULTI;
158 /*} else if( M_POS_CREDITS(mx,my)) {
159 highlight_y = HIGHLIGHT_CREDITS;
160 */} else if( M_POS_QUIT(mx,my)) {
161 highlight_y = HIGHLIGHT_QUIT;
162 } else if( highlight_y == 0) {
163 if( i%20 == 0) {
164 for( el=0; el<ELEKTRONS; el++) {
165 elektrons[el].new_size = 5+rand()%10;
168 } else {
169 for( el=0; el<ELEKTRONS; el++) {
170 elektrons[el].new_x = rand()%(WIDTH-ELEKTRONS_WIDTH-elektrons[el].w/2);
171 elektrons[el].new_y = rand()%HEIGHT;
172 elektrons[el].new_size = 5+rand()%10;
174 highlight_y = 0;
177 for( el=0; el<ELEKTRONS; el++) {
178 if( elektrons[el].size > 0) {
179 rectangle( elektrons[el].x, elektrons[el].y, elektrons[el].size, elektrons[el].size, elektrons[el].r, elektrons[el].g, elektrons[el].b);
180 if( elektrons[el].size > 4) {
181 if( highlight_y != 0 || rand()%10 == 0) {
182 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);
184 rectangle( elektrons[el].x+2, elektrons[el].y+2, elektrons[el].size-4, elektrons[el].size-4, 0, 0, 0);
189 show_image( GR_MENU, WIDTH/2-get_image_width( GR_MENU)/2, 0, 255);
190 font_draw_string( GR_DKC2_FONT, copyright_line, (WIDTH-copyright_line_width)/2, HEIGHT-35, i, ((i/150)%ANIMATION_COUNT));
191 for( help=0; help<HELP_LINES; help++) {
192 help_offset = (help_line_height*(HELP_LINES-help)+i/2)%HELP_PHASE;
193 font_draw_string_alpha( GR_SMALLISH_FONT, help_text[help],
194 (WIDTH-help_line_widths[help])/2,
195 HEIGHT/2+30-help_offset,
196 i, ANIMATION_NONE,
197 255.0*fabsf( sinf( help_offset/((float)HELP_PHASE)*M_PI))
200 show_sprite( GR_RACKET, ((mb&SDL_BUTTON( SDL_BUTTON_LEFT))>0)+(((mb&SDL_BUTTON( SDL_BUTTON_RIGHT))>0)*2), 4, mx, my, 255);
201 updatescr();
203 if( mb & SDL_BUTTON( SDL_BUTTON_LEFT)) {
204 if( M_POS_START_GAME(mx,my)) {
205 start_fade();
206 game( true);
207 SDL_Delay( 150);
208 start_fade();
209 while( SDL_PollEvent( &e));
210 fade_out( CH_AUDIENCE);
212 if( M_POS_START_MULTI(mx,my)) {
213 start_fade();
214 game( false);
215 SDL_Delay( 150);
216 start_fade();
217 while( SDL_PollEvent( &e));
218 fade_out( CH_AUDIENCE);
220 if( M_POS_CREDITS(mx,my)) {
221 //introimage( "data/credits.bmp");
223 if( M_POS_QUIT(mx,my)) {
224 break;
227 i++;
228 SDL_Delay( 10);
231 start_fade();
232 while( is_fading()) {
233 clearscr();
234 updatescr();
235 SDL_Delay( 10);
238 uninit_graphics();
240 SDL_Quit();
241 return 0;