JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
started factoring out global t_frame
[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 draw_bang_dots(SDL_Surface *s)
184 {
185         int i;
186         int first_i, last_i;
187         uint16_t *pixels, *pixel, c;
188         int row_inc = s->pitch/sizeof(uint16_t);
189         Sprite *hit;
190
191         pixels = (uint16_t *) s->pixels;
192         first_i = -1;
193         last_i = 0;
194
195         for(i=0; i<MAXBANGDOTS; i++) {
196                 if(!bdot[i].active) continue;
197
198                 // decrement life and maybe kill
199                 bdot[i].life -= bdot[i].decay;
200                 if(bdot[i].life<0) { bdot[i].active = 0; continue; }
201
202                 // move and clip
203                 bdot[i].x += (bdot[i].dx - screendx)*t_frame;
204                 bdot[i].y += (bdot[i].dy - screendy)*t_frame;
205                 if(bdot[i].x < 0 || bdot[i].x >= XSIZE || bdot[i].y < 0 || bdot[i].y >= YSIZE) {
206                         bdot[i].active = 0;
207                         continue;
208                 }
209
210                 // check collisions
211                 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
212                         if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
213                                 bdot[i].active = 0;
214                                 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
215                                 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
216                                 continue;
217                         }
218                 }
219
220                 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
221                 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
222                 *pixel = c;
223         }
224 }
225
226
227 void
228 new_engine_dots(float time_span, int dir) {
229         int i;
230         int n = time_span * ENGINE_DOTS_PER_TIC;
231         float a, r;  // angle, random length
232         float dx, dy;
233         float hx, hy; // half ship width/height.
234         static const int s[4] = { 2, 1, 0, 1 };
235         float time;
236         float accelh, accelv, past_ship_dx, past_ship_dy;
237
238         hx = ship.image->w / 2;
239         hy = ship.image->h / 2;
240
241         for(i = 0; i<n; i++) {
242                 if(dotptr->active == 0) {
243                         a = frnd()*M_PI + (dir-1)*M_PI_2;
244                         r = sin(frnd()*M_PI);
245                         dx = r * cos(a);
246                         dy = r * -sin(a);  // screen y is "backwards".
247
248                         dotptr->active = 1;
249
250                         // dot was created at a random time during the time span
251                         time = frnd() * time_span; // this is how long ago
252
253                         // calculate how fast the ship was going when this engine dot was
254                         // created (as if it had a smoothe accelleration). This is used in
255                         // determining the velocity of the dots, but not their starting
256                         // location.
257                         accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
258                         accelh *= THRUSTER_STRENGTH * time;
259                         past_ship_dx = ship.dx - accelh;
260                         accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
261                         accelv *= THRUSTER_STRENGTH * time;
262                         past_ship_dy = ship.dy - accelv;
263
264                         // the starting position (not speed) of the dot is calculated as
265                         // though the ship were traveling at a constant speed for this
266                         // time_span.
267                         dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
268                         dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
269                         if(dir&1) {
270                                 dotptr->dx = past_ship_dx + 2*dx;
271                                 dotptr->dy = past_ship_dy + 20*dy;
272                                 dotptr->life = 60 * fabs(dy);
273                         } else {
274                                 dotptr->dx = past_ship_dx + 20*dx;
275                                 dotptr->dy = past_ship_dy + 2*dy;
276                                 dotptr->life = 60 * fabs(dx);
277                         }
278
279                         // move the dot as though it were created in the past
280                         dotptr->x += (dotptr->dx - screendx) * time;
281                         dotptr->y += (dotptr->dy - screendy) * time;
282
283                         if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
284                         else dotptr = edot;
285                 }
286         }
287 }
288
289 void
290 move_engine_dots() {
291         int i;
292         for(i = 0; i<MAXENGINEDOTS; i++) {
293                 if(!edot[i].active) {
294                         continue;
295                 }
296                 edot[i].x += (edot[i].dx - screendx)*t_frame;
297                 edot[i].y += (edot[i].dy - screendy)*t_frame;
298                 edot[i].life -= t_frame*3;
299                 if(edot[i].life < 0 || edot[i].x<0 || edot[i].x >= XSIZE || edot[i].y < 0 || edot[i].y >= YSIZE) {
300                         edot[i].active = 0;
301                 }
302         }
303 }
304
305 void
306 draw_engine_dots(SDL_Surface *s) {
307         int i;
308         uint16_t c;
309         uint16_t *pixels = (uint16_t *) s->pixels;
310         int row_inc = s->pitch/sizeof(uint16_t);
311         int heatindex;
312         Sprite *hit;
313
314         for(i = 0; i<MAXENGINEDOTS; i++) {
315                 if(!edot[i].active) {
316                         continue;
317                 }
318                 // check collisions
319                 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
320                         if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
321                                 edot[i].active = 0;
322                                 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
323                                 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
324                                 continue;
325                         }
326                 }
327                 heatindex = edot[i].life * 6;
328                 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
329                 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
330         }
331 }
332
333 void
334 drawdots(SDL_Surface *s) {
335         int m;
336
337         move_engine_dots();
338
339         // Create engine dots
340         for(m = 0; m<4; m++) {
341                 if(ship.jets & 1<<m) { // 'jets' is a bit field
342                         new_engine_dots(t_frame,m);
343                 }
344         }
345
346         move_dust(t_frame);
347
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         drawdots(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         ms_frame = SDL_GetTicks() - ms_end;
575         ms_end += ms_frame;
576         t_frame = opt_gamespeed * ms_frame / 50;
577         if(state == GAMEPLAY) score += ms_frame;
578
579         // Update the surface
580         SDL_Flip(surf_screen);
581 }
582
583 static inline void
584 kill_ship(Sprite *ship)
585 {
586         ship->flags = MOVE;
587         bang = true;
588 }
589
590 void
591 do_collision(Sprite *a, Sprite *b)
592 {
593         if(a->type == SHIP) kill_ship(a);
594         else if (b->type == SHIP) kill_ship(b);
595         else bounce(a, b);
596 }
597
598 void
599 init_score_entry(void)
600 {
601         SDL_Event e;
602         state = HIGH_SCORE_ENTRY;
603         state_timeout = 5.0e6;
604         SDL_EnableUNICODE(1);
605         while(SDL_PollEvent(&e))
606                 ;
607         insert_score(score);
608 }
609
610 void
611 gameloop() {
612         SDL_Event e;
613         Uint8 *keystate;
614         float tmp;
615
616
617         for(;;) {
618                 while(SDL_PollEvent(&e)) {
619                         switch(e.type) {
620                                 case SDL_QUIT: return;
621                                 case SDL_KEYUP:
622                                         if(e.key.keysym.sym == SDLK_ESCAPE
623                                            || e.key.keysym.sym == SDLK_q)
624                                                 return;
625                                         break;
626                                 case SDL_KEYDOWN:
627                                         if(state == HIGH_SCORE_ENTRY)
628                                                 if(!process_score_input(&e.key.keysym)) {
629                                                         // Write the high score table to the file
630                                                         write_high_score_table();
631                                                         // continue to display the scores briefly
632                                                         state = HIGH_SCORE_DISPLAY;
633                                                         state_timeout = 200;
634                                                         play_tune(TUNE_TITLE_PAGE);
635                                                 }
636                                         break;
637                         }
638                 }
639                 keystate = SDL_GetKeyState(NULL);
640
641                 if(!paused) {
642                         // Count down the game loop timer, and change state when it gets to zero or less;
643
644                         if((state_timeout -= t_frame*3) < 0) {
645                                 switch(state) {
646                                         case DEAD_PAUSE:
647                                                 // Restore the ship and continue playing
648                                                 ship.flags = DRAW|MOVE|COLLIDE;
649                                                 state = GAMEPLAY;
650                                                 play_tune(TUNE_GAMEPLAY);
651                                                 break;
652                                         case GAME_OVER:
653                                                 if(new_high_score(score)) init_score_entry();
654                                                 else {
655                                                         state = HIGH_SCORE_DISPLAY;
656                                                         state_timeout = 400;
657                                                 }
658                                                 break;
659                                         case HIGH_SCORE_DISPLAY:
660                                                 state = TITLE_PAGE;
661                                                 state_timeout = 600.0;
662                                                 fadetimer = 0.0;
663                                                 break;
664                                         case HIGH_SCORE_ENTRY:
665                                                 break;
666                                         case TITLE_PAGE:
667                                                 state = HIGH_SCORE_DISPLAY;
668                                                 state_timeout = 200.0;
669                                                 break;
670                                         case GAMEPLAY:
671                                                 ; // no action necessary
672                                 }
673                         } else {
674                                 if(state == DEAD_PAUSE) {
675                                         if(bangx < 60) bangx = 60;
676                                 }
677                         }
678
679                         new_rocks(t_frame);
680
681                         // SCROLLING
682                         tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
683                         screendy += tmp * t_frame/12;
684                         tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
685                         screendx += tmp * t_frame/12;
686                         // taper off so we don't hit the barrier abruptly.
687                         // (if we would hit in < 2 seconds, adjust to 2 seconds).
688                         if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0)
689                                 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
690                         back_dist += (screendx - SCREENDXMIN)*t_frame;
691                         if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
692
693                         // move bang center
694                         bangx += (bangdx - screendx)*t_frame;
695                         bangy += (bangdy - screendy)*t_frame;
696
697                         move_sprites(t_frame);
698
699
700                         // BOUNCE off left or right edge of screen
701                         if(ship.x < 0 || ship.x+ship.w > XSIZE) {
702                                 ship.x -= (ship.dx-screendx)*t_frame;
703                                 ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
704                         }
705
706                         // BOUNCE off top or bottom of screen
707                         if(ship.y < 0 || ship.y+ship.h > YSIZE) {
708                                 ship.y -= (ship.dy-screendy)*t_frame;
709                                 ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
710                         }
711
712                         draw();
713
714                         if(state == GAMEPLAY && bang) {
715                                 // Died
716                                 bang = false;
717                                 play_sound(SOUND_BANG); // Play the explosion sound
718                                 bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy;
719                                 new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
720
721                                 if(--ship.lives) {
722                                         state = DEAD_PAUSE;
723                                         state_timeout = DEAD_PAUSE_LENGTH;
724                                         ship.dx = (ship.dx < 0) ? -sqrt(-ship.dx) : sqrt(ship.dx);
725                                         ship.dy = (ship.dy < 0) ? -sqrt(-ship.dy) : sqrt(ship.dy);
726                                         if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN;
727                                 } else {
728                                         state = GAME_OVER;
729                                         ship.dx = SCREENDXMIN; ship.dy = 0;
730                                         state_timeout = 200.0;
731                                         fadetimer = 0.0;
732                                         faderate = t_frame;
733                                 }
734                         }
735
736                         // new game
737                         if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
738                            && (state == HIGH_SCORE_DISPLAY
739                                || state == TITLE_PAGE
740                                || state == GAME_OVER)) {
741                                 if(state == GAME_OVER && new_high_score(score))
742                                         init_score_entry();
743                                 else {
744                                         if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
745                                                 g_easy = 0;
746                                                 initial_rocks = NORMAL_I_ROCKS;
747                                                 final_rocks = NORMAL_F_ROCKS;
748                                                 if(opt_gamespeed == EASY_GAMESPEED)
749                                                         opt_gamespeed = NORMAL_GAMESPEED;
750                                         } else if(keystate[SDLK_1]) {
751                                                 g_easy = 1;
752                                                 initial_rocks = EASY_I_ROCKS;
753                                                 final_rocks = EASY_F_ROCKS;
754                                                 opt_gamespeed = EASY_GAMESPEED;
755                                         }
756                                         reset_sprites();
757                                         reset_rocks();
758                                         screendx = SCREENDXMIN; screendy = 0;
759
760                                         ship.x = XSIZE/2.2; ship.y = YSIZE/2;
761                                         ship.dx = screendx; ship.dy = screendy;
762                                         ship.lives = 4;
763                                         ship.flags = MOVE|DRAW|COLLIDE;
764                                         add_sprite(SPRITE(&ship));
765
766                                         score = 0;
767
768                                         state = GAMEPLAY;
769                                         play_tune(TUNE_GAMEPLAY);
770                                 }
771                         }
772
773                         ship.jets = 0;
774                 }
775
776                 if(state == GAMEPLAY) {
777                         if(!paused) {
778                                 // FIXME why is this at the bottom? Shouldn't it be up before the ship movement?
779                                 if(keystate[SDLK_LEFT]  || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
780                                 if(keystate[SDLK_DOWN]  || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
781                                 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
782                                 if(keystate[SDLK_UP]    || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
783                                 if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
784                         }
785
786                         if(keystate[SDLK_p] | keystate[SDLK_s]) {
787                                 if(!pausedown) {
788                                         paused = !paused;
789                                         pausedown = 1;
790                                         if(!paused) ms_end = SDL_GetTicks();
791                                 }
792                         } else {
793                                 pausedown = 0;
794                         }
795                 }
796
797                 if(state == TITLE_PAGE && keystate[SDLK_h]) {
798                         state = HIGH_SCORE_DISPLAY;
799                         state_timeout = 400;
800                 }
801         }
802 }
803
804 int
805 main(int argc, char **argv) {
806         if(!parse_opts(argc, argv)) return 1;
807
808         if(init()) {
809                 printf ("ta: '%s'\n",initerror);
810                 return 1;
811         }
812
813         gameloop();
814
815         return 0;
816 }