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