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