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