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