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