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