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