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