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