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