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