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