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