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