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