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