JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Friction doesn't make sense any more, dropped that.
[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 <argp.h>
23 #include <math.h>
24 #include <SDL/SDL.h>
25 #include <SDL/SDL_image.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "SFont.h"
32
33 #ifdef DEBUG
34 #include "debug.h"
35 #endif
36
37 #include "args.h"
38 #include "common.h"
39 #include "config.h"
40 #include "file.h"
41 #include "globals.h"
42 #include "rocks.h"
43 #include "score.h"
44 #include "shape.h"
45 #include "sound.h"
46
47 // ************************************* VARS
48 // SDL_Surface global variables
49 SDL_Surface 
50         *surf_screen,   // Screen
51         *surf_b_variations, // "variations" banner
52         *surf_b_on, // "on" banner
53         *surf_b_rockdodger, // "rockdodger" banner
54         *surf_b_game,   // Title element "game"
55         *surf_b_over,   // Title element "over"
56         *surf_ship,             // Spaceship element
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 SFont_Font *g_font;
63
64 // Structure global variables
65 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
66 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
67 struct spacedot sdot[MAXSPACEDOTS];
68
69 // Other global variables
70 char topline[1024];
71 char *initerror = "";
72
73
74
75 struct shape shipshape;
76 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
77 float shipdx = SCREENDXMIN, shipdy = 0.0;       // Change in X position per tick.
78 float screendx = SCREENDXMIN, screendy = 0.0;
79 float xscroll, yscroll;
80 float back_dist;
81
82 // all movement is based on t_frame.
83 float t_frame;  // length of this frame (in ticks = 1/20th second)
84 float s_frame;  // length of this frame (seconds)
85 int ms_frame;   // length of this frame (milliseconds)
86 int ms_end;     // end of this frame (milliseconds)
87
88 float bangx, bangy, bangdx, bangdy;
89
90 int nships,score;
91 int gameover;
92 int jets = 0;
93
94 float fadetimer = 0, faderate;
95
96 int pausedown = 0, paused = 0;
97
98 // bangdot start (bd1) and end (bd2) position:
99 int bd1 = 0, bd2 = 0;
100
101 enum states {
102         TITLE_PAGE,
103         GAMEPLAY,
104         DEAD_PAUSE,
105         GAME_OVER,
106         HIGH_SCORE_ENTRY,
107         HIGH_SCORE_DISPLAY
108 };
109 enum states state = TITLE_PAGE;
110 float state_timeout = 600.0;
111
112 #define NSEQUENCE 2
113 char *sequence[] = {
114         "Press SPACE to start",
115         "http://herkamire.com/jason/vor"
116 };
117
118 int bangdotlife, nbangdots;
119 Uint16 heatcolor[W*3];
120
121 char *data_dir;
122 extern char *optarg;
123 extern int optind, opterr, optopt;
124
125 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
126
127 // ************************************* FUNCS
128
129 float
130 rnd() {
131         return (float)random()/(float)RAND_MAX;
132 }
133
134 void
135 init_engine_dots() {
136         int i;
137         for(i = 0; i<MAXENGINEDOTS; i++) {
138                 edot[i].active = 0;
139         }
140 }
141
142 void
143 init_space_dots() {
144         int i,b;
145         for(i = 0; i<MAXSPACEDOTS; i++) {
146                 sdot[i].x = rnd()*(XSIZE-5);
147                 sdot[i].y = rnd()*(YSIZE-5);
148                 sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
149                 b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
150                 sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
151         }
152 }
153
154 void
155 make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
156
157         // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
158         // TODO - generate and display dots in a circular buffer
159
160         int x,y,endcount;
161         Uint16 *rawpixel,c;
162         double theta,r;
163         int begin_generate;
164
165         begin_generate = SDL_GetTicks();
166
167         SDL_LockSurface(s);
168         rawpixel = (Uint16 *) s->pixels;
169
170         //for(n = 0; n <= power/2; n++) {
171
172         endcount = 0;
173         while (endcount<3) {
174                 for(x = 0; x<s->w; x++) {
175                         for(y = 0; y<s->h; y++) {
176                                 c = rawpixel[s->pitch/2*y + x];
177                                 if(c && c != s->format->colorkey) {
178
179                                         theta = rnd()*M_PI*2;
180
181                                         r = 1-(rnd()*rnd());
182
183                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
184                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
185                                         bdot[bd2].x = x + xbang;
186                                         bdot[bd2].y = y + ybang;
187
188                                         // Replace the last few bang dots with the pixels from the exploding object
189                                         bdot[bd2].c = (endcount>0)?c:0;
190                                         bdot[bd2].life = 100;
191                                         bdot[bd2].decay = rnd()*3 + 1;
192                                         bdot[bd2].active = 1;
193
194                                         bd2++;
195                                         bd2 %= MAXBANGDOTS;
196
197                                         // If the circular buffer is filled, who cares? They've had their chance.
198                                         //if(bd2 == bd1-1) goto exitloop;
199
200                                 }
201                         }
202                 }
203
204                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
205         }
206
207         SDL_UnlockSurface(s);
208
209 }
210
211 void
212 draw_bang_dots(SDL_Surface *s) {
213         int i;
214         int first_i, last_i;
215         Uint16 *rawpixel;
216         rawpixel = (Uint16 *) s->pixels;
217
218         first_i = -1;
219         last_i = 0;
220
221         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
222
223                 i %= MAXBANGDOTS;
224
225                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
226                         // If the dot has drifted outside the perimeter, kill it
227                         bdot[i].active = 0;
228                 }
229
230                 if(bdot[i].active) {
231                         if(first_i < 0)
232                         first_i = i;
233                         //last_i = i + 1;
234                         rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)] = bdot[i].c ? bdot[i].c : heatcolor[(int)(bdot[i].life*3)];
235                         bdot[i].life -= bdot[i].decay;
236                         bdot[i].x += bdot[i].dx*t_frame - xscroll;
237                         bdot[i].y += bdot[i].dy*t_frame - yscroll;
238
239                         if(bdot[i].life<0)
240                         bdot[i].active = 0;
241                 }
242         }
243
244         if(first_i >= 0) {
245                 bd1 = first_i;
246                 bd2 = last_i;
247         }
248         else {
249                 bd1 = 0;
250                 bd2 = 0;
251         }
252
253 }
254
255
256 void
257 draw_space_dots(SDL_Surface *s) {
258         int i;
259         Uint16 *rawpixel;
260         rawpixel = (Uint16 *) s->pixels;
261
262         for(i = 0; i<MAXSPACEDOTS; i++) {
263                 if(sdot[i].y<0) {
264                         sdot[i].y = 0;
265                 }
266                 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
267                 sdot[i].x -= xscroll / (1.3 + sdot[i].z);
268                 sdot[i].y -= yscroll / (1.3 + sdot[i].z);
269                 if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
270                 else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
271                 if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
272                 else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
273         }
274 }
275
276 void
277 draw_engine_dots(SDL_Surface *s) {
278         int i;
279         Uint16 *rawpixel;
280         rawpixel = (Uint16 *) s->pixels;
281
282         for(i = 0; i<MAXENGINEDOTS; i++) {
283                 if(edot[i].active) {
284                         edot[i].x += edot[i].dx*t_frame - xscroll;
285                         edot[i].y += edot[i].dy*t_frame - yscroll;
286                         if((edot[i].life -= t_frame*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
287                                 edot[i].active = 0;
288                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
289                                 edot[i].active = 0;
290                         } else {
291                                 int heatindex;
292                                 heatindex = edot[i].life * 6;
293                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
294                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
295                         }
296                 }
297         }
298 }
299
300 void
301 new_engine_dots(int n, int dir) {
302         int i;
303         float a, r;  // angle, random length
304         float dx, dy;
305         float hx, hy; // half ship width/height.
306         static const int s[4] = { 2, 1, 0, 1 };
307
308         hx = surf_ship->w / 2;
309         hy = surf_ship->h / 2;
310
311         for(i = 0; i<n; i++) {
312                 if(dotptr->active == 0) {
313                         a = rnd()*M_PI + (dir-1)*M_PI_2;
314                         r = sin(rnd()*M_PI);
315                         dx = r * cos(a);
316                         dy = r * -sin(a);  // screen y is "backwards".
317
318                         dotptr->active = 1;
319                         dotptr->x = shipx + s[dir]*hx + (rnd()-0.5)*3;
320                         dotptr->y = shipy + s[(dir+1)&3]*hy + (rnd()-0.5)*3;
321                         if(dir&1) {
322                                 dotptr->dx = shipdx + 2*dx;
323                                 dotptr->dy = shipdy + 20*dy;
324                                 dotptr->life = 60 * fabs(dy);
325                         } else {
326                                 dotptr->dx = shipdx + 20*dx;
327                                 dotptr->dy = shipdy + 2*dy;
328                                 dotptr->life = 60 * fabs(dx);
329                         }
330
331                         dotptr++;
332                         if(dotptr-edot >= MAXENGINEDOTS) {
333                                 dotptr = edot;
334                         }
335                 }
336         }
337 }
338
339 void
340 drawdots(SDL_Surface *s) {
341         int m;
342
343         // Create engine dots out the side we're moving from
344         for(m = 0; m<4; m++) {
345                 if(jets & 1<<m) { // 'jets' is a bit field
346                         new_engine_dots(80,m);
347                 }
348         }
349
350         SDL_LockSurface(s);
351         draw_space_dots(s);
352         draw_engine_dots(s);
353         draw_bang_dots(s);
354         SDL_UnlockSurface(s);
355 }
356
357 int
358 init(void) {
359
360         int i;
361         SDL_Surface *temp;
362         Uint32 flag;
363
364         // Where are our data files?
365         if(!find_files()) exit(1);
366         read_high_score_table();
367
368         if(opt_sound) {
369                 // Initialize SDL with audio and video
370                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
371                         opt_sound = 0;
372                         printf ("Can't open sound, starting without it\n");
373                         atexit(SDL_Quit);
374                 } else {
375                         atexit(SDL_Quit);
376                         atexit(SDL_CloseAudio);
377                         opt_sound = init_sound();
378                 }
379         } else {
380                 // Initialize with video only
381                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
382                 atexit(SDL_Quit);
383         }
384
385         play_tune(0);
386
387         // Attempt to get the required video size
388         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
389         if(opt_fullscreen) flag |= SDL_FULLSCREEN;
390         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
391
392         // Set the title bar text
393         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
394
395         NULLERROR(surf_screen);
396
397         // Set the heat color from the range 0 (cold) to 300 (blue-white)
398         for(i = 0; i<W*3; i++) {
399                 heatcolor[i] = SDL_MapRGB(
400                         surf_screen->format,
401                         (i<W)?(i*M/W):(M),(i<W)?0:(i<2*W)?((i-W)*M/W):M,(i<2*W)?0:((i-W)*M/W) // Got that?
402                 );
403         }
404
405         // Load the banners
406         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
407         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
408
409         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
410         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
411
412         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
413         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
414
415         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
416         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
417
418         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
419         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
420
421         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
422         g_font = SFont_InitFont(surf_font_big);
423
424         // Load the spaceship graphic.
425         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
426         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
427         get_shape(surf_ship, &shipshape);
428
429         // Load the life indicator (small ship) graphic.
430         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
431         NULLERROR(surf_life = SDL_DisplayFormat(temp));
432
433         init_engine_dots();
434         init_space_dots();
435
436         init_rocks();
437
438         // Remove the mouse cursor
439 #ifdef SDL_DISABLE
440         SDL_ShowCursor(SDL_DISABLE);
441 #endif
442
443         return 0;
444 }
445
446 int
447 draw() {
448         int i;
449         SDL_Rect dest;
450         int bang, x;
451         char *text;
452         float fadegame,fadeover;
453
454         bang = 0;
455
456         // Draw a fully black background
457         SDL_FillRect(surf_screen,NULL,0);
458
459         // Draw the background dots
460         drawdots(surf_screen);
461
462         // Draw ship
463         if(!gameover && state == GAMEPLAY ) {
464                 dest.x = shipx;
465                 dest.y = shipy;
466                 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
467         }
468
469         draw_rocks();
470
471         // Draw the life indicators.
472         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
473         for(i = 0; i<nships-1; i++) {
474                 dest.x = (i + 1)*(surf_life->w + 10);
475                 dest.y = 20;
476                 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
477         }
478
479         // Draw the score
480         snprintscore_line(topline, 50, score);
481         SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
482
483         // If it's game over, show the game over graphic in the dead centre
484         switch (state) {
485                 case GAME_OVER:
486                         if(fadetimer<3.0/faderate) {
487                                 fadegame = fadetimer/(3.0/faderate);
488                         } else {
489                                 fadegame = 1.0;
490                         }
491
492                         if(fadetimer<3.0/faderate) {
493                                 fadeover = 0.0;
494                         } else if(fadetimer<6.0/faderate) {
495                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
496                         } else {
497                                 fadeover = 1.0;
498                         }
499
500                         dest.x = (XSIZE-surf_b_game->w)/2;
501                         dest.y = (YSIZE-surf_b_game->h)/2-40;
502                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
503                         SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
504
505                         dest.x = (XSIZE-surf_b_over->w)/2;
506                         dest.y = (YSIZE-surf_b_over->h)/2 + 40;
507                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
508                         SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
509                 break;
510
511                 case TITLE_PAGE:
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_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
516                         SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
517
518                         dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
519                         dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
520                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
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_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
526                         SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
527
528                         text = "Version " VERSION;
529                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
530                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
531
532                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
533                         //text = "Press SPACE to start!";
534                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
535                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
536                 break;
537
538                 case HIGH_SCORE_ENTRY:
539                         play_tune(2);
540                         if(!process_score_input()) {  // done inputting name
541
542                                 // Change state to briefly show high scores page
543                                 state = HIGH_SCORE_DISPLAY;
544                                 state_timeout = 200;
545
546                                 // Write the high score table to the file
547                                 write_high_score_table();
548                 
549                                 // Play the title page tune
550                                 play_tune(0);
551                         }
552                 // FALL THROUGH TO
553                 case HIGH_SCORE_DISPLAY:
554                         // Display de list o high scores mon.
555                         display_scores(surf_screen, 150,50);
556                         break;
557                 case GAMEPLAY:
558                 case DEAD_PAUSE:
559                         ; // no action necessary
560         }
561
562         if(!gameover && state == GAMEPLAY) {
563                 bang = hit_rocks(shipx, shipy, &shipshape);
564         }
565
566         ms_frame = SDL_GetTicks() - ms_end;
567         ms_end += ms_frame;
568         if(ms_frame>200 || ms_frame<0) {
569                 // We won't run at all below 5 frames per second.
570                 // This also happens if we were paused, grr.
571                 s_frame = 0;
572                 ms_frame = 0;
573         } else {
574                 s_frame = opt_gamespeed * ms_frame / 1000;
575                 if(state == GAMEPLAY) score += ms_frame;
576         }
577         t_frame = s_frame * 20;
578
579         // Update the surface
580         SDL_Flip(surf_screen);
581
582
583         return bang;
584 }
585
586 int
587 gameloop() {
588         Uint8 *keystate;
589         float tmp;
590
591
592         for(;;) {
593                 if(!paused) {
594                         // Count down the game loop timer, and change state when it gets to zero or less;
595
596                         if((state_timeout -= t_frame*3) < 0) {
597                                 switch(state) {
598                                         case DEAD_PAUSE:
599                                                 // Create a new ship and start all over again
600                                                 state = GAMEPLAY;
601                                                 play_tune(1);
602                                                 break;
603                                         case GAME_OVER:
604                                                 if(new_high_score(score)) {
605                                                         SDL_Event e;
606                                                         state = HIGH_SCORE_ENTRY;
607                                                         state_timeout = 5.0e6;
608                                                         SDL_EnableUNICODE(1);
609                                                         while(SDL_PollEvent(&e))
610                                                                 ;
611                                                 } else if(!keystate[SDLK_SPACE]) {
612                                                         state = HIGH_SCORE_DISPLAY;
613                                                         state_timeout = 400;
614                                                 }
615                                                 break;
616                                         case HIGH_SCORE_DISPLAY:
617                                                 state = TITLE_PAGE;
618                                                 state_timeout = 500.0;
619                                                 break;
620                                         case HIGH_SCORE_ENTRY:
621                                                 // state = TITLE_PAGE;
622                                                 // play_tune(1);
623                                                 // state_timeout = 100.0;
624                                                 break;
625                                         case TITLE_PAGE:
626                                                 state = HIGH_SCORE_DISPLAY;
627                                                 state_timeout = 200.0;
628                                                 break;
629                                         case GAMEPLAY:
630                                                 ; // no action necessary
631                                 }
632                         } else {
633                                 if(state == DEAD_PAUSE) {
634                                         float blast_radius;
635                                         int fixonly;
636
637                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
638                                                 blast_radius = BLAST_RADIUS * 1.3;
639                                                 fixonly = 1;
640                                         } else {
641                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
642                                                 fixonly = 0;
643                                         }
644                                         blast_rocks(bangx, bangy, blast_radius, fixonly);
645
646                                         if(bangx < 60) bangx = 60;
647                                 }
648                         }
649
650                         new_rocks();
651
652                         // INERTIA
653                         shipx += shipdx*t_frame;
654                         shipy += shipdy*t_frame;
655
656                         // SCROLLING
657                         tmp = shipy - (YSIZE / 2);
658                         tmp += shipdy * 25;
659                         tmp /= -25;
660                         tmp = ((screendy * (t_frame - 12)) + (tmp * t_frame)) / 12;
661                         screendy = -tmp;
662                         tmp = shipx - (XSIZE / 3);
663                         tmp += shipdx * 25;
664                         tmp /= -25;
665                         tmp = ((screendx * (t_frame - 12)) + (tmp * t_frame)) / 12;
666                         screendx = -tmp;
667
668                         // taper off if we would hit the barrier in under 2 seconds.
669                         if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
670                                 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
671                         }
672
673                         xscroll = screendx * t_frame;
674                         yscroll = screendy * t_frame;
675                         back_dist += (screendx - SCREENDXMIN)*t_frame;
676                         if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
677
678                         shipx -= xscroll;
679                         shipy -= yscroll;
680
681                         // move bang center
682                         bangx += bangdx*t_frame - xscroll;
683                         bangy += bangdy*t_frame - yscroll;
684
685                         move_rocks();
686
687
688                         // BOUNCE X
689                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
690                                 // BOUNCE from left and right wall
691                                 shipx -= (shipdx-screendx)*t_frame;
692                                 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
693                         }
694
695                         // BOUNCE Y
696                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
697                                 // BOUNCE from top and bottom wall
698                                 shipy -= (shipdy-screendy)*t_frame;
699                                 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
700                         }
701
702
703                         if(draw() && state == GAMEPLAY) {
704                                 // Died
705                                 play_sound(0); // Play the explosion sound
706                                 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
707                                 make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
708                                 shipdx *= 0.5; shipdy *= 0.5;
709                                 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
710                                 if(--nships <= 0) {
711                                         state = GAME_OVER;
712                                         gameover = 1;
713                                         shipdx = 8; shipdy = 0;
714                                         state_timeout = 200.0;
715                                         fadetimer = 0.0;
716                                         faderate = t_frame;
717                                 }
718                                 else {
719                                         state = DEAD_PAUSE;
720                                         state_timeout = DEAD_PAUSE_LENGTH;
721                                 }
722                         }
723
724                         SDL_PumpEvents();
725                         keystate = SDL_GetKeyState(NULL);
726
727                         // new game
728                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
729
730                                 reset_rocks();
731
732                                 nships = 4;
733                                 score = 0;
734
735                                 state = GAMEPLAY;
736                                 play_tune(1);
737
738                                 gameover = 0;
739                                 shipx = XSIZE/2.2; shipy = YSIZE/2;
740                                 shipdx = screendx; shipdy = screendy;
741                         }
742
743                         jets = 0;
744                 } else {
745                         SDL_PumpEvents();
746                         keystate = SDL_GetKeyState(NULL);
747                 }
748
749                 if(state == GAMEPLAY) {
750                         if(!gameover) {
751
752                                 if(!paused) {
753                                         if(keystate[SDLK_LEFT]  | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
754                                         if(keystate[SDLK_DOWN]  | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
755                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
756                                         if(keystate[SDLK_UP]    | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
757                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
758                                 }
759
760                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
761                                         if(!pausedown) {
762                                                 paused = !paused;
763                                                 pausedown = 1;
764                                         }
765                                 } else {
766                                         pausedown = 0;
767                                 }
768
769                         }
770                         else {
771                                 paused = 0;
772                                 pausedown = 0;
773                         }
774                 } else if(state == GAME_OVER) {
775                         if(keystate[SDLK_SPACE]) {
776                                 state_timeout = -1;
777                         }
778                 }
779
780                 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
781                         return 0;
782                 }
783
784         }
785 }
786
787 int
788 main(int argc, char **argv) {
789         init_opts();
790         argp_parse(&argp, argc, argv, 0, 0, 0);
791
792         if(init()) {
793                 printf ("ta: '%s'\n",initerror);
794                 return 1;
795         }
796
797         reset_rocks();
798         gameloop();
799
800         return 0;
801 }