JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
renamed framelen and friends (again)
[vor.git] / main.c
1 /* Variations on RockDodger
2  * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
3  *
4  * Project fork 2004, Jason Woofenden and Joshua Grams.
5  * (a whole bunch of modifications and project rename)
6
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <argp.h>
23 #include <math.h>
24 #include <SDL/SDL.h>
25 #include <SDL/SDL_image.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "SFont.h"
32
33 #ifdef DEBUG
34 #include "debug.h"
35 #endif
36
37 #include "args.h"
38 #include "common.h"
39 #include "config.h"
40 #include "file.h"
41 #include "globals.h"
42 #include "rocks.h"
43 #include "score.h"
44 #include "shape.h"
45 #include "sound.h"
46
47 // ************************************* VARS
48 // SDL_Surface global variables
49 SDL_Surface 
50         *surf_screen,   // Screen
51         *surf_b_variations, // "variations" banner
52         *surf_b_on, // "on" banner
53         *surf_b_rockdodger, // "rockdodger" banner
54         *surf_b_game,   // Title element "game"
55         *surf_b_over,   // Title element "over"
56         *surf_ship,             // Spaceship element
57         *surf_life,     // Indicator of number of ships remaining
58         *surf_rock[NROCKS],     // THE ROCKS
59         *surf_font_big; // The big font
60         
61
62 SFont_Font *g_font;
63
64 // Structure global variables
65 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
66 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
67 struct spacedot sdot[MAXSPACEDOTS];
68
69 // Other global variables
70 char topline[1024];
71 char *initerror = "";
72
73
74
75 struct shape shipshape;
76 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
77 float shipdx = SCREENDXMIN, shipdy = 0.0;       // Change in X position per tick.
78 float screendx = SCREENDXMIN, screendy = 0.0;
79 float xscroll, yscroll;
80 float back_dist;
81
82 // all movement is based on t_frame.
83 float t_frame;  // length of this frame (in ticks = 1/20th second)
84 int ms_frame;   // length of this frame (milliseconds)
85 int ms_end;     // end of this frame (milliseconds)
86
87 float bangx, bangy, bangdx, bangdy;
88
89 int nships,score;
90 int gameover;
91 int maneuver = 0;
92
93 float fadetimer = 0, faderate;
94
95 int pausedown = 0, paused = 0;
96
97 // bangdot start (bd1) and end (bd2) position:
98 int bd1 = 0, bd2 = 0;
99
100 enum states {
101         TITLE_PAGE,
102         GAMEPLAY,
103         DEAD_PAUSE,
104         GAME_OVER,
105         HIGH_SCORE_ENTRY,
106         HIGH_SCORE_DISPLAY
107 };
108 enum states state = TITLE_PAGE;
109 float state_timeout = 600.0;
110
111 #define NSEQUENCE 2
112 char *sequence[] = {
113         "Press SPACE to start",
114         "http://herkamire.com/jason/vor"
115 };
116
117 int bangdotlife, nbangdots;
118 Uint16 heatcolor[W*3];
119
120 char *data_dir;
121 extern char *optarg;
122 extern int optind, opterr, optopt;
123
124 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
125
126 // ************************************* FUNCS
127
128 float
129 rnd() {
130         return (float)random()/(float)RAND_MAX;
131 }
132
133 void
134 init_engine_dots() {
135         int i;
136         for(i = 0; i<MAXENGINEDOTS; i++) {
137                 edot[i].active = 0;
138         }
139 }
140
141 void
142 init_space_dots() {
143         int i,b;
144         for(i = 0; i<MAXSPACEDOTS; i++) {
145                 sdot[i].x = rnd()*(XSIZE-5);
146                 sdot[i].y = rnd()*(YSIZE-5);
147                 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
148                 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
149                 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
150         }
151 }
152
153 void
154 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
155
156         // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
157         // TODO - generate and display dots in a circular buffer
158
159         int x,y,endcount;
160         Uint16 *rawpixel,c;
161         double theta,r;
162         int begin_generate;
163
164         begin_generate = SDL_GetTicks();
165
166         SDL_LockSurface(s);
167         rawpixel = (Uint16 *) s->pixels;
168
169         //for(n = 0; n <= power/2; n++) {
170
171         endcount = 0;
172         while (endcount<3) {
173                 for(x = 0; x<s->w; x++) {
174                         for(y = 0; y<s->h; y++) {
175                                 c = rawpixel[s->pitch/2*y + x];
176                                 if(c && c != s->format->colorkey) {
177
178                                         theta = rnd()*M_PI*2;
179
180                                         r = 1-(rnd()*rnd());
181
182                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
183                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
184                                         bdot[bd2].x = x + xbang;
185                                         bdot[bd2].y = y + ybang;
186
187                                         // Replace the last few bang dots with the pixels from the exploding object
188                                         bdot[bd2].c = (endcount>0)?c:0;
189                                         bdot[bd2].life = 100;
190                                         bdot[bd2].decay = rnd()*3 + 1;
191                                         bdot[bd2].active = 1;
192
193                                         bd2++;
194                                         bd2 %= MAXBANGDOTS;
195
196                                         // If the circular buffer is filled, who cares? They've had their chance.
197                                         //if(bd2 == bd1-1) goto exitloop;
198
199                                 }
200                         }
201                 }
202
203                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
204         }
205
206         SDL_UnlockSurface(s);
207
208 }
209
210 void
211 draw_bang_dots(SDL_Surface *s) {
212         int i;
213         int first_i, last_i;
214         Uint16 *rawpixel;
215         rawpixel = (Uint16 *) s->pixels;
216
217         first_i = -1;
218         last_i = 0;
219
220         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
221
222                 i %= MAXBANGDOTS;
223
224                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
225                         // If the dot has drifted outside the perimeter, kill it
226                         bdot[i].active = 0;
227                 }
228
229                 if(bdot[i].active) {
230                         if(first_i < 0)
231                         first_i = i;
232                         //last_i = i + 1;
233                         rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)] = bdot[i].c ? bdot[i].c : heatcolor[(int)(bdot[i].life*3)];
234                         bdot[i].life -= bdot[i].decay;
235                         bdot[i].x += bdot[i].dx*t_frame - xscroll;
236                         bdot[i].y += bdot[i].dy*t_frame - yscroll;
237
238                         if(bdot[i].life<0)
239                         bdot[i].active = 0;
240                 }
241         }
242
243         if(first_i >= 0) {
244                 bd1 = first_i;
245                 bd2 = last_i;
246         }
247         else {
248                 bd1 = 0;
249                 bd2 = 0;
250         }
251
252 }
253
254
255 void
256 draw_space_dots(SDL_Surface *s) {
257         int i;
258         Uint16 *rawpixel;
259         rawpixel = (Uint16 *) s->pixels;
260
261         for(i = 0; i<MAXSPACEDOTS; i++) {
262                 if(sdot[i].y<0) {
263                         sdot[i].y = 0;
264                 }
265                 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
266                 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
267                 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
268                 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
269                 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
270                 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
271                 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
272         }
273 }
274
275 void
276 draw_engine_dots(SDL_Surface *s) {
277         int i;
278         Uint16 *rawpixel;
279         rawpixel = (Uint16 *) s->pixels;
280
281         for(i = 0; i<MAXENGINEDOTS; i++) {
282                 if(edot[i].active) {
283                         edot[i].x += edot[i].dx*t_frame - xscroll;
284                         edot[i].y += edot[i].dy*t_frame - yscroll;
285                         if((edot[i].life -= t_frame*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
286                                 edot[i].active = 0;
287                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
288                                 edot[i].active = 0;
289                         } else {
290                                 int heatindex;
291                                 heatindex = edot[i].life * 6;
292                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
293                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
294                         }
295                 }
296         }
297 }
298
299 void
300 create_engine_dots(int newdots) {
301         int i;
302         double theta,r,dx,dy;
303
304         if(!opt_tail_engine) return;
305
306         if(state == GAMEPLAY) {
307                 for(i = 0; i<newdots*t_frame; i++) {
308                         if(dotptr->active == 0) {
309                                 theta = rnd()*M_PI*2;
310                                 r = rnd();
311                                 dx = cos(theta)*r;
312                                 dy = sin(theta)*r;
313
314                                 dotptr->active = 1;
315                                 dotptr->x = shipx + surf_ship->w/2-14;
316                                 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
317                                 dotptr->dx = 10*(dx-1.5) + shipdx;
318                                 dotptr->dy = 1*dy + shipdy;
319                                 dotptr->life = 45 + rnd(1)*5;
320
321                                 dotptr++;
322                                 if(dotptr-edot >= MAXENGINEDOTS) {
323                                         dotptr = edot;
324                                 }
325                         }
326                 }
327         }
328 }
329
330 void
331 create_engine_dots2(int newdots, int m) {
332         int i;
333         double theta, theta2, dx, dy, adx, ady;
334
335         // Don't create fresh engine dots when
336         // the game is not being played and a demo is not beng shown
337         if(state != GAMEPLAY) return;
338
339         for(i = 0; i<newdots; i++) {
340                 if(dotptr->active == 0) {
341                         theta = rnd()*M_PI*2;
342                         theta2 = rnd()*M_PI*2;
343
344                         dx = cos(theta) * fabs(cos(theta2));
345                         dy = sin(theta) * fabs(cos(theta2));
346                         adx = fabs(dx);
347                         ady = fabs(dy);
348
349
350                         dotptr->active = 1;
351                         dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
352                         dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
353
354                         switch(m) {
355                                 case 0:
356                                         dotptr->x -= 14;
357                                         dotptr->dx = -20*adx + shipdx;
358                                         dotptr->dy = 2*dy + shipdy;
359                                         dotptr->life = 60 * adx;
360                                 break;
361                                 case 1:
362                                         dotptr->dx = 2*dx + shipdx;
363                                         dotptr->dy = -20*ady + shipdy;
364                                         dotptr->life = 60 * ady;
365                                 break;
366                                 case 2:
367                                         dotptr->x += 14;
368                                         dotptr->dx = 20*adx + shipdx;
369                                         dotptr->dy = 2*dy + shipdy;
370                                         dotptr->life = 60 * adx;
371                                 break;
372                                 case 3:
373                                         dotptr->dx = 2*dx + shipdx;
374                                         dotptr->dy = 20*ady + shipdy;
375                                         dotptr->life = 60 * ady;
376                                 break;
377                         }
378                         dotptr++;
379                         if(dotptr-edot >= MAXENGINEDOTS) {
380                                 dotptr = edot;
381                         }
382                 }
383         }
384 }
385
386 void
387 drawdots(SDL_Surface *s) {
388         int m;
389
390         // Create more engine dots comin' out da back
391         if(!gameover) create_engine_dots(200);
392
393         // Create engine dots out the side we're moving from
394         for(m = 0; m<4; m++) {
395                 if(maneuver & 1<<m) { // 'maneuver' is a bit field
396                         create_engine_dots2(80,m);
397                 }
398         }
399
400         SDL_LockSurface(s);
401         draw_space_dots(s);
402         draw_engine_dots(s);
403         draw_bang_dots(s);
404         SDL_UnlockSurface(s);
405 }
406
407 int
408 init(void) {
409
410         int i;
411         SDL_Surface *temp;
412         Uint32 flag;
413
414         // Where are our data files?
415         if(!find_files()) exit(1);
416         read_high_score_table();
417
418         if(opt_sound) {
419                 // Initialize SDL with audio and video
420                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
421                         opt_sound = 0;
422                         printf ("Can't open sound, starting without it\n");
423                         atexit(SDL_Quit);
424                 } else {
425                         atexit(SDL_Quit);
426                         atexit(SDL_CloseAudio);
427                         opt_sound = init_sound();
428                 }
429         } else {
430                 // Initialize with video only
431                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
432                 atexit(SDL_Quit);
433         }
434
435         play_tune(0);
436
437         // Attempt to get the required video size
438         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
439         if(opt_fullscreen) flag |= SDL_FULLSCREEN;
440         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
441
442         // Set the title bar text
443         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
444
445         NULLERROR(surf_screen);
446
447         // Set the heat color from the range 0 (cold) to 300 (blue-white)
448         for(i = 0; i<W*3; i++) {
449                 heatcolor[i] = SDL_MapRGB(
450                         surf_screen->format,
451                         (i<W)?(i*M/W):(M),(i<W)?0:(i<2*W)?((i-W)*M/W):M,(i<2*W)?0:((i-W)*M/W) // Got that?
452                 );
453         }
454
455         // Load the banners
456         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
457         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
458
459         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
460         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
461
462         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
463         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
464
465         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
466         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
467
468         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
469         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
470
471         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
472         g_font = SFont_InitFont(surf_font_big);
473
474         // Load the spaceship graphic.
475         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
476         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
477         get_shape(surf_ship, &shipshape);
478
479         // Load the life indicator (small ship) graphic.
480         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
481         NULLERROR(surf_life = SDL_DisplayFormat(temp));
482
483         init_engine_dots();
484         init_space_dots();
485
486         init_rocks();
487
488         // Remove the mouse cursor
489 #ifdef SDL_DISABLE
490         SDL_ShowCursor(SDL_DISABLE);
491 #endif
492
493         return 0;
494 }
495
496 int
497 draw() {
498         int i;
499         SDL_Rect dest;
500         int bang, x;
501         char *text;
502         float fadegame,fadeover;
503
504         bang = 0;
505
506         // Draw a fully black background
507         SDL_FillRect(surf_screen,NULL,0);
508
509         // Draw the background dots
510         drawdots(surf_screen);
511
512         // Draw ship
513         if(!gameover && state == GAMEPLAY ) {
514                 dest.x = shipx;
515                 dest.y = shipy;
516                 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
517         }
518
519         draw_rocks();
520
521         // Draw the life indicators.
522         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
523         for(i = 0; i<nships-1; i++) {
524                 dest.x = (i + 1)*(surf_life->w + 10);
525                 dest.y = 20;
526                 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
527         }
528
529         // Draw the score
530         snprintscore_line(topline, 50, score);
531         SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
532
533         // If it's game over, show the game over graphic in the dead centre
534         switch (state) {
535                 case GAME_OVER:
536                         if(fadetimer<3.0/faderate) {
537                                 fadegame = fadetimer/(3.0/faderate);
538                         } else {
539                                 fadegame = 1.0;
540                         }
541
542                         if(fadetimer<3.0/faderate) {
543                                 fadeover = 0.0;
544                         } else if(fadetimer<6.0/faderate) {
545                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
546                         } else {
547                                 fadeover = 1.0;
548                         }
549
550                         dest.x = (XSIZE-surf_b_game->w)/2;
551                         dest.y = (YSIZE-surf_b_game->h)/2-40;
552                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
553                         SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
554
555                         dest.x = (XSIZE-surf_b_over->w)/2;
556                         dest.y = (YSIZE-surf_b_over->h)/2 + 40;
557                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
558                         SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
559                 break;
560
561                 case TITLE_PAGE:
562
563                         dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
564                         dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
565                         SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
566                         SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
567
568                         dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
569                         dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
570                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
571                         SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
572
573                         dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
574                         dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
575                         SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
576                         SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
577
578                         text = "Version " VERSION;
579                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
580                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
581
582                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
583                         //text = "Press SPACE to start!";
584                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
585                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
586                 break;
587
588                 case HIGH_SCORE_ENTRY:
589                         play_tune(2);
590                         if(!process_score_input()) {  // done inputting name
591
592                                 // Change state to briefly show high scores page
593                                 state = HIGH_SCORE_DISPLAY;
594                                 state_timeout = 200;
595
596                                 // Write the high score table to the file
597                                 write_high_score_table();
598                 
599                                 // Play the title page tune
600                                 play_tune(0);
601                         }
602                 // FALL THROUGH TO
603                 case HIGH_SCORE_DISPLAY:
604                         // Display de list o high scores mon.
605                         display_scores(surf_screen, 150,50);
606                         break;
607                 case GAMEPLAY:
608                 case DEAD_PAUSE:
609                         ; // no action necessary
610         }
611
612         if(!gameover && state == GAMEPLAY) {
613                 bang = hit_rocks(shipx, shipy, &shipshape);
614         }
615
616         ms_frame = SDL_GetTicks() - ms_end;
617         ms_end += ms_frame;
618         if(ms_frame>200 || ms_frame<0) {
619                 // We won't run at all below 5 frames per second.
620                 t_frame = 0;
621         } else {
622                 t_frame = opt_gamespeed*ms_frame/50.0;
623                 if(state == GAMEPLAY) {
624                         score += ms_frame;
625                 }
626         }
627
628         // Update the surface
629         SDL_Flip(surf_screen);
630
631
632         return bang;
633 }
634
635 int
636 gameloop() {
637         Uint8 *keystate;
638         float tmp;
639
640
641         for(;;) {
642                 if(!paused) {
643                         // Count down the game loop timer, and change state when it gets to zero or less;
644
645                         if((state_timeout -= t_frame*3) < 0) {
646                                 switch(state) {
647                                         case DEAD_PAUSE:
648                                                 // Create a new ship and start all over again
649                                                 state = GAMEPLAY;
650                                                 play_tune(1);
651                                                 break;
652                                         case GAME_OVER:
653                                                 if(new_high_score(score)) {
654                                                         SDL_Event e;
655                                                         state = HIGH_SCORE_ENTRY;
656                                                         state_timeout = 5.0e6;
657                                                         SDL_EnableUNICODE(1);
658                                                         while(SDL_PollEvent(&e))
659                                                                 ;
660                                                 } else if(!keystate[SDLK_SPACE]) {
661                                                         state = HIGH_SCORE_DISPLAY;
662                                                         state_timeout = 400;
663                                                 }
664                                                 break;
665                                         case HIGH_SCORE_DISPLAY:
666                                                 state = TITLE_PAGE;
667                                                 state_timeout = 500.0;
668                                                 break;
669                                         case HIGH_SCORE_ENTRY:
670                                                 // state = TITLE_PAGE;
671                                                 // play_tune(1);
672                                                 // state_timeout = 100.0;
673                                                 break;
674                                         case TITLE_PAGE:
675                                                 state = HIGH_SCORE_DISPLAY;
676                                                 state_timeout = 200.0;
677                                                 break;
678                                         case GAMEPLAY:
679                                                 ; // no action necessary
680                                 }
681                         } else {
682                                 if(state == DEAD_PAUSE) {
683                                         float blast_radius;
684                                         int fixonly;
685
686                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
687                                                 blast_radius = BLAST_RADIUS * 1.3;
688                                                 fixonly = 1;
689                                         } else {
690                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
691                                                 fixonly = 0;
692                                         }
693                                         blast_rocks(bangx, bangy, blast_radius, fixonly);
694
695                                         if(bangx < 60) bangx = 60;
696                                 }
697                         }
698
699                         new_rocks();
700
701                         // FRICTION?
702                         if(opt_friction) {
703                                 shipdx *= pow((double)0.9,(double)t_frame);
704                                 shipdy *= pow((double)0.9,(double)t_frame);
705                         }
706
707                         // INERTIA
708                         shipx += shipdx*t_frame;
709                         shipy += shipdy*t_frame;
710
711                         // SCROLLING
712                         tmp = shipy - (YSIZE / 2);
713                         tmp += shipdy * 25;
714                         tmp /= -25;
715                         tmp = ((screendy * (t_frame - 12)) + (tmp * t_frame)) / 12;
716                         screendy = -tmp;
717                         tmp = shipx - (XSIZE / 3);
718                         tmp += shipdx * 25;
719                         tmp /= -25;
720                         tmp = ((screendx * (t_frame - 12)) + (tmp * t_frame)) / 12;
721                         screendx = -tmp;
722
723                         // taper off if we would hit the barrier in under 2 seconds.
724                         if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
725                                 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
726                         }
727
728                         xscroll = screendx * t_frame;
729                         yscroll = screendy * t_frame;
730                         back_dist += (screendx - SCREENDXMIN)*t_frame;
731                         if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
732
733                         shipx -= xscroll;
734                         shipy -= yscroll;
735
736                         // move bang center
737                         bangx += bangdx*t_frame - xscroll;
738                         bangy += bangdy*t_frame - yscroll;
739
740                         move_rocks();
741
742
743                         // BOUNCE X
744                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
745                                 // BOUNCE from left and right wall
746                                 shipx -= (shipdx-screendx)*t_frame;
747                                 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
748                         }
749
750                         // BOUNCE Y
751                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
752                                 // BOUNCE from top and bottom wall
753                                 shipy -= (shipdy-screendy)*t_frame;
754                                 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
755                         }
756
757
758                         if(draw() && state == GAMEPLAY) {
759                                 // Died
760                                 play_sound(0); // Play the explosion sound
761                                 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
762                                 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
763                                 shipdx *= 0.5; shipdy *= 0.5;
764                                 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
765                                 if(--nships <= 0) {
766                                         state = GAME_OVER;
767                                         gameover = 1;
768                                         shipdx = 8; shipdy = 0;
769                                         state_timeout = 200.0;
770                                         fadetimer = 0.0;
771                                         faderate = t_frame;
772                                 }
773                                 else {
774                                         state = DEAD_PAUSE;
775                                         state_timeout = DEAD_PAUSE_LENGTH;
776                                 }
777                         }
778
779                         SDL_PumpEvents();
780                         keystate = SDL_GetKeyState(NULL);
781
782                         // new game
783                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
784
785                                 reset_rocks();
786
787                                 nships = 4;
788                                 score = 0;
789
790                                 state = GAMEPLAY;
791                                 play_tune(1);
792
793                                 gameover = 0;
794                                 shipx = XSIZE/2.2; shipy = YSIZE/2;
795                                 shipdx = screendx; shipdy = screendy;
796                         }
797
798                         maneuver = 0;
799                 } else {
800                         SDL_PumpEvents();
801                         keystate = SDL_GetKeyState(NULL);
802                 }
803
804                 if(state == GAMEPLAY) {
805                         if(!gameover) {
806
807                                 if(!paused) {
808                                         if(keystate[SDLK_UP]    | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; maneuver |= 1<<3;}
809                                         if(keystate[SDLK_DOWN]  | keystate[SDLK_t]) { shipdy += 1.5*t_frame; maneuver |= 1<<1;}
810                                         if(keystate[SDLK_LEFT]  | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; maneuver |= 1<<2;}
811                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; maneuver |= 1;}
812                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
813                                 }
814
815                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
816                                         if(!pausedown) {
817                                                 paused = !paused;
818                                                 pausedown = 1;
819                                         }
820                                 } else {
821                                         pausedown = 0;
822                                 }
823
824                         }
825                         else {
826                                 paused = 0;
827                                 pausedown = 0;
828                         }
829                 } else if(state == GAME_OVER) {
830                         if(keystate[SDLK_SPACE]) {
831                                 state_timeout = -1;
832                         }
833                 }
834
835                 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
836                         return 0;
837                 }
838
839         }
840 }
841
842 int
843 main(int argc, char **argv) {
844         init_opts();
845         argp_parse(&argp, argc, argv, 0, 0, 0);
846
847         if(init()) {
848                 printf ("ta: '%s'\n",initerror);
849                 return 1;
850         }
851
852         reset_rocks();
853         gameloop();
854
855         return 0;
856 }