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