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