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