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