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