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