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