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