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