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