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