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