JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
w00t! Finally rearranged the scroll equations so they make sense to me.
[vor.git] / main.c
1 /* Variations on RockDodger
2  * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
3  *
4  * Project fork 2004, Jason Woofenden and Joshua Grams.
5  * (a whole bunch of modifications and project rename)
6
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <argp.h>
23 #include <math.h>
24 #include <SDL/SDL.h>
25 #include <SDL/SDL_image.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "SFont.h"
32
33 #ifdef DEBUG
34 #include "debug.h"
35 #endif
36
37 #include "args.h"
38 #include "common.h"
39 #include "config.h"
40 #include "dust.h"
41 #include "file.h"
42 #include "globals.h"
43 #include "mt.h"
44 #include "rocks.h"
45 #include "score.h"
46 #include "shape.h"
47 #include "sound.h"
48
49 // ************************************* VARS
50 // SDL_Surface global variables
51 SDL_Surface 
52         *surf_screen,   // Screen
53         *surf_b_variations, // "variations" banner
54         *surf_b_on, // "on" banner
55         *surf_b_rockdodger, // "rockdodger" banner
56         *surf_b_game,   // Title element "game"
57         *surf_b_over,   // Title element "over"
58         *surf_ship,             // Spaceship element
59         *surf_life,     // Indicator of number of ships remaining
60         *surf_rock[NROCKS],     // THE ROCKS
61         *surf_font_big; // The big font
62         
63
64 SFont_Font *g_font;
65
66 // Structure global variables
67 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
68 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
69
70 // Other global variables
71 char topline[1024];
72 char *initerror = "";
73
74
75
76 struct shape shipshape;
77 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
78 float shipdx = SCREENDXMIN, shipdy = 0.0;       // Change in X position per tick.
79 float screendx = SCREENDXMIN, screendy = 0.0;
80 float xscroll, yscroll;
81 float back_dist;
82
83 // all movement is based on t_frame.
84 float t_frame;  // length of this frame (in ticks = 1/20th second)
85 float s_frame;  // length of this frame (seconds)
86 int ms_frame;   // length of this frame (milliseconds)
87 int ms_end;     // end of this frame (milliseconds)
88
89 float bangx, bangy, bangdx, bangdy;
90
91 int nships,score;
92 int gameover;
93 int jets = 0;
94
95 float fadetimer = 0, faderate;
96
97 int pausedown = 0, paused = 0;
98
99 // bangdot start (bd1) and end (bd2) position:
100 int bd1 = 0, bd2 = 0;
101
102 enum states {
103         TITLE_PAGE,
104         GAMEPLAY,
105         DEAD_PAUSE,
106         GAME_OVER,
107         HIGH_SCORE_ENTRY,
108         HIGH_SCORE_DISPLAY
109 };
110 enum states state = TITLE_PAGE;
111 float state_timeout = 600.0;
112
113 #define NSEQUENCE 2
114 char *sequence[] = {
115         "Press SPACE to start",
116         "http://herkamire.com/jason/vor"
117 };
118
119 int bangdotlife, nbangdots;
120 Uint16 heatcolor[W*3];
121
122 char *data_dir;
123 extern char *optarg;
124 extern int optind, opterr, optopt;
125
126 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
127
128 // ************************************* FUNCS
129
130 void
131 init_engine_dots() {
132         int i;
133         for(i = 0; i<MAXENGINEDOTS; i++) {
134                 edot[i].active = 0;
135         }
136 }
137
138 void
139 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power)
140 {
141         int x,y,endcount;
142         Uint16 *rawpixel,c;
143         double theta,r;
144         int begin_generate;
145
146         begin_generate = SDL_GetTicks();
147
148         SDL_LockSurface(s);
149         rawpixel = (Uint16 *) s->pixels;
150
151         //for(n = 0; n <= power/2; n++) {
152
153         endcount = 0;
154         while (endcount<3) {
155                 for(x = 0; x<s->w; x++) {
156                         for(y = 0; y<s->h; y++) {
157                                 c = rawpixel[s->pitch/2*y + x];
158                                 if(c && c != s->format->colorkey) {
159
160                                         theta = frnd()*M_PI*2;
161
162                                         r = 1-(frnd()*frnd());
163
164                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
165                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
166                                         bdot[bd2].x = x + xbang;
167                                         bdot[bd2].y = y + ybang;
168
169                                         // Replace the last few bang dots with the pixels from the exploding object
170                                         bdot[bd2].c = (endcount>0)?c:0;
171                                         bdot[bd2].life = 100;
172                                         bdot[bd2].decay = frnd()*3 + 1;
173                                         bdot[bd2].active = 1;
174
175                                         bd2++;
176                                         bd2 %= MAXBANGDOTS;
177
178                                         // If the circular buffer is filled, who cares? They've had their chance.
179                                         //if(bd2 == bd1-1) goto exitloop;
180
181                                 }
182                         }
183                 }
184
185                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
186         }
187
188         SDL_UnlockSurface(s);
189
190 }
191
192 void
193 draw_bang_dots(SDL_Surface *s) {
194         int i;
195         int first_i, last_i;
196         Uint16 *rawpixel;
197         rawpixel = (Uint16 *) s->pixels;
198
199         first_i = -1;
200         last_i = 0;
201
202         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
203
204                 i %= MAXBANGDOTS;
205
206                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
207                         // If the dot has drifted outside the perimeter, kill it
208                         bdot[i].active = 0;
209                 }
210
211                 if(bdot[i].active) {
212                         if(first_i < 0)
213                         first_i = i;
214                         //last_i = i + 1;
215                         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)];
216                         bdot[i].life -= bdot[i].decay;
217                         bdot[i].x += bdot[i].dx*t_frame - xscroll;
218                         bdot[i].y += bdot[i].dy*t_frame - yscroll;
219
220                         if(bdot[i].life<0)
221                         bdot[i].active = 0;
222                 }
223         }
224
225         if(first_i >= 0) {
226                 bd1 = first_i;
227                 bd2 = last_i;
228         }
229         else {
230                 bd1 = 0;
231                 bd2 = 0;
232         }
233
234 }
235
236
237 void
238 draw_engine_dots(SDL_Surface *s) {
239         int i;
240         Uint16 *rawpixel;
241         rawpixel = (Uint16 *) s->pixels;
242
243         for(i = 0; i<MAXENGINEDOTS; i++) {
244                 if(edot[i].active) {
245                         edot[i].x += edot[i].dx*t_frame - xscroll;
246                         edot[i].y += edot[i].dy*t_frame - yscroll;
247                         if((edot[i].life -= t_frame*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
248                                 edot[i].active = 0;
249                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
250                                 edot[i].active = 0;
251                         } else {
252                                 int heatindex;
253                                 heatindex = edot[i].life * 6;
254                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
255                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
256                         }
257                 }
258         }
259 }
260
261 void
262 new_engine_dots(int n, int dir) {
263         int i;
264         float a, r;  // angle, random length
265         float dx, dy;
266         float hx, hy; // half ship width/height.
267         static const int s[4] = { 2, 1, 0, 1 };
268
269         hx = surf_ship->w / 2;
270         hy = surf_ship->h / 2;
271
272         for(i = 0; i<n; i++) {
273                 if(dotptr->active == 0) {
274                         a = frnd()*M_PI + (dir-1)*M_PI_2;
275                         r = sin(frnd()*M_PI);
276                         dx = r * cos(a);
277                         dy = r * -sin(a);  // screen y is "backwards".
278
279                         dotptr->active = 1;
280                         dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3;
281                         dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
282                         if(dir&1) {
283                                 dotptr->dx = shipdx + 2*dx;
284                                 dotptr->dy = shipdy + 20*dy;
285                                 dotptr->life = 60 * fabs(dy);
286                         } else {
287                                 dotptr->dx = shipdx + 20*dx;
288                                 dotptr->dy = shipdy + 2*dy;
289                                 dotptr->life = 60 * fabs(dx);
290                         }
291
292                         dotptr++;
293                         if(dotptr-edot >= MAXENGINEDOTS) {
294                                 dotptr = edot;
295                         }
296                 }
297         }
298 }
299
300 void
301 drawdots(SDL_Surface *s) {
302         int m;
303
304         // Create engine dots out the side we're moving from
305         for(m = 0; m<4; m++) {
306                 if(jets & 1<<m) { // 'jets' is a bit field
307                         new_engine_dots(80,m);
308                 }
309         }
310
311         move_dust();
312
313         SDL_LockSurface(s);
314         draw_dust(s);
315         draw_engine_dots(s);
316         draw_bang_dots(s);
317         SDL_UnlockSurface(s);
318 }
319
320 int
321 init(void) {
322
323         int i;
324         SDL_Surface *temp;
325         Uint32 flag;
326
327         // Where are our data files?
328         if(!find_files()) exit(1);
329         read_high_score_table();
330
331         if(opt_sound) {
332                 // Initialize SDL with audio and video
333                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
334                         opt_sound = 0;
335                         printf ("Can't open sound, starting without it\n");
336                         atexit(SDL_Quit);
337                 } else {
338                         atexit(SDL_Quit);
339                         atexit(SDL_CloseAudio);
340                         opt_sound = init_sound();
341                 }
342         } else {
343                 // Initialize with video only
344                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
345                 atexit(SDL_Quit);
346         }
347
348         play_tune(TUNE_TITLE_PAGE);
349
350         // Attempt to get the required video size
351         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
352         if(opt_fullscreen) flag |= SDL_FULLSCREEN;
353         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
354
355         // Set the title bar text
356         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
357
358         NULLERROR(surf_screen);
359
360         // Set the heat color from the range 0 (cold) to 300 (blue-white)
361         for(i = 0; i<W*3; i++) {
362                 heatcolor[i] = SDL_MapRGB(
363                         surf_screen->format,
364                         (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?
365                 );
366         }
367
368         // Load the banners
369         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
370         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
371
372         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
373         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
374
375         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
376         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
377
378         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
379         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
380
381         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
382         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
383
384         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
385         g_font = SFont_InitFont(surf_font_big);
386
387         // Load the spaceship graphic.
388         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
389         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
390         get_shape(surf_ship, &shipshape);
391
392         // Load the life indicator (small ship) graphic.
393         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
394         NULLERROR(surf_life = SDL_DisplayFormat(temp));
395
396         init_engine_dots();
397         init_dust();
398
399         init_rocks();
400
401         // Remove the mouse cursor
402 #ifdef SDL_DISABLE
403         SDL_ShowCursor(SDL_DISABLE);
404 #endif
405
406         return 0;
407 }
408
409 int
410 draw() {
411         int i;
412         SDL_Rect dest;
413         int bang, x;
414         char *text;
415         float fadegame,fadeover;
416
417         bang = 0;
418
419         // Draw a fully black background
420         SDL_FillRect(surf_screen,NULL,0);
421
422         // Draw the background dots
423         drawdots(surf_screen);
424
425         // Draw ship
426         if(!gameover && state == GAMEPLAY ) {
427                 dest.x = shipx;
428                 dest.y = shipy;
429                 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
430         }
431
432         draw_rocks();
433
434         // Draw the life indicators.
435         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
436         for(i = 0; i<nships-1; i++) {
437                 dest.x = (i + 1)*(surf_life->w + 10);
438                 dest.y = 20;
439                 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
440         }
441
442         // Draw the score
443         snprintscore_line(topline, 50, score);
444         SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
445
446         // If it's game over, show the game over graphic in the dead centre
447         switch (state) {
448                 case GAME_OVER:
449                         if(fadetimer<3.0/faderate) {
450                                 fadegame = fadetimer/(3.0/faderate);
451                         } else {
452                                 fadegame = 1.0;
453                         }
454
455                         if(fadetimer<3.0/faderate) {
456                                 fadeover = 0.0;
457                         } else if(fadetimer<6.0/faderate) {
458                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
459                         } else {
460                                 fadeover = 1.0;
461                         }
462
463                         dest.x = (XSIZE-surf_b_game->w)/2;
464                         dest.y = (YSIZE-surf_b_game->h)/2-40;
465                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
466                         SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
467
468                         dest.x = (XSIZE-surf_b_over->w)/2;
469                         dest.y = (YSIZE-surf_b_over->h)/2 + 40;
470                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
471                         SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
472                 break;
473
474                 case TITLE_PAGE:
475
476                         dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
477                         dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
478                         SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
479                         SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
480
481                         dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
482                         dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
483                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
484                         SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
485
486                         dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
487                         dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
488                         SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
489                         SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
490
491                         text = "Version " VERSION;
492                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
493                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
494
495                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
496                         //text = "Press SPACE to start!";
497                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
498                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
499                 break;
500
501                 case HIGH_SCORE_ENTRY:
502                         play_tune(TUNE_HIGH_SCORE_ENTRY);
503                         if(!process_score_input()) {  // done inputting name
504
505                                 // Change state to briefly show high scores page
506                                 state = HIGH_SCORE_DISPLAY;
507                                 state_timeout = 200;
508
509                                 // Write the high score table to the file
510                                 write_high_score_table();
511                 
512                                 play_tune(TUNE_TITLE_PAGE);
513                         }
514                 // FALL THROUGH TO
515                 case HIGH_SCORE_DISPLAY:
516                         // Display de list o high scores mon.
517                         display_scores(surf_screen, 150,50);
518                         break;
519                 case GAMEPLAY:
520                 case DEAD_PAUSE:
521                         ; // no action necessary
522         }
523
524         if(!gameover && state == GAMEPLAY) {
525                 bang = hit_rocks(shipx, shipy, &shipshape);
526         }
527
528         ms_frame = SDL_GetTicks() - ms_end;
529         ms_end += ms_frame;
530         if(ms_frame>200 || ms_frame<0) {
531                 // We won't run at all below 5 frames per second.
532                 // This also happens if we were paused, grr.
533                 s_frame = 0;
534                 ms_frame = 0;
535         } else {
536                 s_frame = opt_gamespeed * ms_frame / 1000;
537                 if(state == GAMEPLAY) score += ms_frame;
538         }
539         t_frame = s_frame * 20;
540
541         // Update the surface
542         SDL_Flip(surf_screen);
543
544
545         return bang;
546 }
547
548 int
549 gameloop() {
550         Uint8 *keystate;
551         float tmp;
552
553
554         for(;;) {
555                 if(!paused) {
556                         // Count down the game loop timer, and change state when it gets to zero or less;
557
558                         if((state_timeout -= t_frame*3) < 0) {
559                                 switch(state) {
560                                         case DEAD_PAUSE:
561                                                 // Create a new ship and start all over again
562                                                 state = GAMEPLAY;
563                                                 play_tune(TUNE_GAMEPLAY);
564                                                 break;
565                                         case GAME_OVER:
566                                                 if(new_high_score(score)) {
567                                                         SDL_Event e;
568                                                         state = HIGH_SCORE_ENTRY;
569                                                         state_timeout = 5.0e6;
570                                                         SDL_EnableUNICODE(1);
571                                                         while(SDL_PollEvent(&e))
572                                                                 ;
573                                                 } else if(!keystate[SDLK_SPACE]) {
574                                                         state = HIGH_SCORE_DISPLAY;
575                                                         state_timeout = 400;
576                                                 }
577                                                 break;
578                                         case HIGH_SCORE_DISPLAY:
579                                                 state = TITLE_PAGE;
580                                                 state_timeout = 500.0;
581                                                 break;
582                                         case HIGH_SCORE_ENTRY:
583                                                 break;
584                                         case TITLE_PAGE:
585                                                 state = HIGH_SCORE_DISPLAY;
586                                                 state_timeout = 200.0;
587                                                 break;
588                                         case GAMEPLAY:
589                                                 ; // no action necessary
590                                 }
591                         } else {
592                                 if(state == DEAD_PAUSE) {
593                                         float blast_radius;
594                                         int fixonly;
595
596                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
597                                                 blast_radius = BLAST_RADIUS * 1.3;
598                                                 fixonly = 1;
599                                         } else {
600                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
601                                                 fixonly = 0;
602                                         }
603                                         blast_rocks(bangx, bangy, blast_radius, fixonly);
604
605                                         if(bangx < 60) bangx = 60;
606                                 }
607                         }
608
609                         new_rocks();
610
611                         // INERTIA
612                         shipx += shipdx*t_frame;
613                         shipy += shipdy*t_frame;
614
615                         // SCROLLING
616                         tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy);
617                         screendy += tmp * t_frame/12;
618                         tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx);
619                         screendx += tmp * t_frame/12;
620
621                         // taper off if we would hit the barrier in under 2 seconds.
622                         if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
623                                 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
624                         }
625
626                         xscroll = screendx * t_frame;
627                         yscroll = screendy * t_frame;
628                         back_dist += (screendx - SCREENDXMIN)*t_frame;
629                         if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
630
631                         shipx -= xscroll;
632                         shipy -= yscroll;
633
634                         // move bang center
635                         bangx += bangdx*t_frame - xscroll;
636                         bangy += bangdy*t_frame - yscroll;
637
638                         move_rocks();
639
640
641                         // BOUNCE X
642                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
643                                 // BOUNCE from left and right wall
644                                 shipx -= (shipdx-screendx)*t_frame;
645                                 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
646                         }
647
648                         // BOUNCE Y
649                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
650                                 // BOUNCE from top and bottom wall
651                                 shipy -= (shipdy-screendy)*t_frame;
652                                 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
653                         }
654
655
656                         if(draw() && state == GAMEPLAY) {
657                                 // Died
658                                 play_sound(SOUND_BANG); // Play the explosion sound
659                                 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
660                                 new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
661                                 shipdx *= 0.5; shipdy *= 0.5;
662                                 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
663                                 if(--nships <= 0) {
664                                         state = GAME_OVER;
665                                         gameover = 1;
666                                         shipdx = 8; shipdy = 0;
667                                         state_timeout = 200.0;
668                                         fadetimer = 0.0;
669                                         faderate = t_frame;
670                                 }
671                                 else {
672                                         state = DEAD_PAUSE;
673                                         state_timeout = DEAD_PAUSE_LENGTH;
674                                 }
675                         }
676
677                         SDL_PumpEvents();
678                         keystate = SDL_GetKeyState(NULL);
679
680                         // new game
681                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
682
683                                 reset_rocks();
684
685                                 nships = 4;
686                                 score = 0;
687
688                                 state = GAMEPLAY;
689                                 play_tune(TUNE_GAMEPLAY);
690
691                                 gameover = 0;
692                                 shipx = XSIZE/2.2; shipy = YSIZE/2;
693                                 shipdx = screendx; shipdy = screendy;
694                         }
695
696                         jets = 0;
697                 } else {
698                         SDL_PumpEvents();
699                         keystate = SDL_GetKeyState(NULL);
700                 }
701
702                 if(state == GAMEPLAY) {
703                         if(!gameover) {
704
705                                 if(!paused) {
706                                         if(keystate[SDLK_LEFT]  | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
707                                         if(keystate[SDLK_DOWN]  | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
708                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
709                                         if(keystate[SDLK_UP]    | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
710                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
711                                 }
712
713                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
714                                         if(!pausedown) {
715                                                 paused = !paused;
716                                                 pausedown = 1;
717                                         }
718                                 } else {
719                                         pausedown = 0;
720                                 }
721
722                         }
723                         else {
724                                 paused = 0;
725                                 pausedown = 0;
726                         }
727                 } else if(state == GAME_OVER) {
728                         if(keystate[SDLK_SPACE]) {
729                                 state_timeout = -1;
730                         }
731                 }
732
733                 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
734                         return 0;
735                 }
736
737         }
738 }
739
740 int
741 main(int argc, char **argv) {
742         init_opts();
743         argp_parse(&argp, argc, argv, 0, 0, 0);
744
745         if(init()) {
746                 printf ("ta: '%s'\n",initerror);
747                 return 1;
748         }
749
750         gameloop();
751
752         return 0;
753 }