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