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