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