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