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