Tennix 0.3.3: Dynamic Text Engine
[tennix.git] / tennix.c
blob1d1572caa7b23e5c8e1b9ed1131b2c7a5c8cf7d8
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 int main( int argc, char** argv) {
36 int i = 0;
37 int mx, my;
38 Uint8 *keys;
39 Uint8 mb;
40 SDL_Event e;
41 SDL_VideoInfo* vi;
42 int sdl_flags = SDL_SWSURFACE | SDL_DOUBLEBUF;
43 char datadir[MAXPATHLEN];
44 int highlight_y = 0;
45 char copyright_line[100];
46 int copyright_line_width, url_width;
48 Point elektrons[ELEKTRONS];
49 int el;
51 fprintf( stderr, "Tennix %s\n%s\n%s\n\n", VERSION, COPYRIGHT, URL);
53 sprintf( copyright_line, "Tennix %s -- %s", VERSION, COPYRIGHT);
55 srand( (unsigned)time( NULL));
57 for( el=0; el<ELEKTRONS; el++) {
58 elektrons[el].x = elektrons[el].y = 0;
59 elektrons[el].w = 50+(rand()%120);
60 elektrons[el].h = 10+(rand()%10);
61 elektrons[el].current_x = WIDTH/2;
62 elektrons[el].current_y = HEIGHT/2;
63 elektrons[el].new_x = rand()%WIDTH;
64 elektrons[el].new_y = rand()%HEIGHT;
65 elektrons[el].phase = 4*2*PI*((rand()%1000)/1000.0);
66 elektrons[el].r = 100+rand()%155;
67 elektrons[el].g = 100+rand()%155;
68 elektrons[el].b = 0;
69 elektrons[el].size = 0;
70 elektrons[el].new_size = 5+rand()%10;
73 if( SDL_Init( SDL_INIT_VIDEO) == -1) {
74 fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError());
75 exit( 1);
78 atexit( SDL_Quit);
80 vi = (SDL_VideoInfo*)SDL_GetVideoInfo();
82 if( (screen = SDL_SetVideoMode( WIDTH, HEIGHT, vi->vfmt->BitsPerPixel, sdl_flags)) == NULL) {
83 fprintf( stderr, "Can't set video mode: %s\n", SDL_GetError());
84 exit( 1);
87 SDL_WM_SetCaption( "Tennix! SDL", "Tennix");
88 SDL_ShowCursor( SDL_DISABLE);
89 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, 1);
91 strcpy( datadir, argv[0]);
92 strcpy( (char*)(datadir+strlen( dirname( datadir))), DATADIR);
94 init_sound( datadir);
95 init_graphics( datadir);
97 copyright_line_width = font_get_string_width( GR_SMALLISH_FONT, copyright_line);
98 url_width = font_get_string_width( GR_SMALLISH_FONT, URL);
100 while( 1) {
101 SDL_PollEvent( &e);
102 keys = SDL_GetKeyState( NULL);
103 mb = SDL_GetMouseState( &mx, &my);
105 if( keys[SDLK_ESCAPE] || keys['q']) {
106 break;
109 if( keys['f']) {
110 SDL_WM_ToggleFullScreen( screen);
113 clearscr();
114 for( el=0; el<ELEKTRONS; el++) {
115 if( highlight_y) {
116 elektrons[el].new_x = HIGHLIGHT_X;
117 elektrons[el].new_y = highlight_y;
118 if( elektrons[el].new_size >= 5) {
119 elektrons[el].new_size = 1+rand()%4;
124 for( el=0; el<ELEKTRONS; el++) {
125 elektrons[el].current_x += (int)(1.0*(elektrons[el].new_x-elektrons[el].current_x)/ELEKTRON_LAZYNESS);
126 elektrons[el].current_y += (int)(1.0*(elektrons[el].new_y-elektrons[el].current_y)/ELEKTRON_LAZYNESS);
127 if( abs(elektrons[el].current_x - elektrons[el].new_x) < ELEKTRON_LAZYNESS) {
128 elektrons[el].current_x = elektrons[el].new_x;
130 if( abs(elektrons[el].current_y - elektrons[el].new_y) < ELEKTRON_LAZYNESS) {
131 elektrons[el].current_y = elektrons[el].new_y;
133 if( i%4==0 && elektrons[el].size < elektrons[el].new_size) {
134 elektrons[el].size++;
136 if( i%4==0 && elektrons[el].size > elektrons[el].new_size) {
137 elektrons[el].size--;
139 elektrons[el].phase += 0.02+(rand()%20/100.0);
140 elektrons[el].x = elektrons[el].current_x + ELEKTRONS_WIDTH + elektrons[el].w*cosf(elektrons[el].phase/4);
141 elektrons[el].y = elektrons[el].current_y + elektrons[el].h*sinf(elektrons[el].phase);
144 if( M_POS_START_GAME(mx,my)) {
145 highlight_y = HIGHLIGHT_START_GAME;
146 } else if( M_POS_START_MULTI(mx,my)) {
147 highlight_y = HIGHLIGHT_START_MULTI;
148 /*} else if( M_POS_CREDITS(mx,my)) {
149 highlight_y = HIGHLIGHT_CREDITS;
150 */} else if( M_POS_QUIT(mx,my)) {
151 highlight_y = HIGHLIGHT_QUIT;
152 } else if( highlight_y == 0) {
153 if( i%20 == 0) {
154 for( el=0; el<ELEKTRONS; el++) {
155 elektrons[el].new_size = 5+rand()%10;
158 } else {
159 for( el=0; el<ELEKTRONS; el++) {
160 elektrons[el].new_x = rand()%(WIDTH-ELEKTRONS_WIDTH-elektrons[el].w/2);
161 elektrons[el].new_y = rand()%HEIGHT;
162 elektrons[el].new_size = 5+rand()%10;
164 highlight_y = 0;
167 if( highlight_y) {
168 //show_image( GR_MENU_HIGHLIGHT, HIGHLIGHT_X, highlight_y, 255);
170 for( el=0; el<ELEKTRONS; el++) {
171 if( elektrons[el].size > 0) {
172 rectangle( elektrons[el].x, elektrons[el].y, elektrons[el].size, elektrons[el].size, elektrons[el].r, elektrons[el].g, elektrons[el].b);
173 if( elektrons[el].size > 5) {
174 rectangle( elektrons[el].x+2, elektrons[el].y+2, elektrons[el].size-4, elektrons[el].size-4, 0, 0, 0);
179 show_image( GR_MENU, 180*2, 0, 255);
180 if( i%(12*4) == 0) {
181 sound_racket( 0);
183 show_sprite( GR_ANIMATION, (i++/12%4), 4, 40, 100, 255);
184 font_draw_string( GR_SMALLISH_FONT, copyright_line, (WIDTH-copyright_line_width)/2, HEIGHT-70, i, ((i/150)%ANIMATION_COUNT));
185 font_draw_string( GR_SMALLISH_FONT, URL, (WIDTH-url_width)/2, HEIGHT-30, i, ((i/50)%ANIMATION_COUNT));
186 show_sprite( GR_RACKET, (mb&SDL_BUTTON( SDL_BUTTON_LEFT)) > 0, 4, mx, my, 255);
187 updatescr();
189 if( mb & SDL_BUTTON( SDL_BUTTON_LEFT)) {
190 if( M_POS_START_GAME(mx,my)) {
191 start_fade();
192 game( true);
193 SDL_Delay( 150);
194 start_fade();
195 while( SDL_PollEvent( &e));
196 fade_out( CH_AUDIENCE);
198 if( M_POS_START_MULTI(mx,my)) {
199 start_fade();
200 game( false);
201 SDL_Delay( 150);
202 start_fade();
203 while( SDL_PollEvent( &e));
204 fade_out( CH_AUDIENCE);
206 if( M_POS_CREDITS(mx,my)) {
207 //introimage( "data/credits.bmp");
209 if( M_POS_QUIT(mx,my)) {
210 break;
213 SDL_Delay( 10);
216 uninit_graphics();
218 SDL_Quit();
219 return 0;