JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
data/indicators/life.png: now built by povray from ship graphic.
[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 "score.h"
29 #include "shape.h"
30 #include "sound.h"
31
32 #include <math.h>
33 #include <SDL/SDL.h>
34 #include <SDL/SDL_image.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "SFont.h"
42
43 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
44 #define NULLERROR(a) CONDERROR((a) == NULL)
45
46 // ************************************* STRUCTS
47 struct rock_struct {
48         // Array of black pixel coordinates. This is scanned 
49         // every frame to see if it's still black, and as
50         // soon as it isn't we BLOW UP
51         float x,y,dx,dy;
52         int active;
53         int dead;  // has been blown out of the way
54                    // to make room for a new ship appearing.
55         SDL_Surface *image;
56         struct shape *shape;
57         int type_number;
58 }; 
59 struct black_point_struct {
60         int x,y;
61 };
62 struct bangdots {
63         // Bang dots have the same colour as shield dots.
64         // Bang dots get darker as they age.
65         // Some are coloured the same as the ex-ship.
66         float x,y,dx,dy;
67         Uint16 c; // when zero, use heatcolor[bangdotlife]
68         float life;     // When reduced to 0, set active = 0
69         int active;
70         float decay;// Amount by which to reduce life each time dot is drawn
71 };
72 struct enginedots {
73         // Engine dots stream out the back of the ship, getting darker as they go.
74         int active;
75         float x,y,dx,dy;
76         // The life of an engine dot 
77         // is a number starting at between 0 and 50 and counting backward.
78         float life;     // When reduced to 0, set active = 0
79 };
80 struct spacedot {
81         // Space dots are harmless background items
82         // All are active. When one falls off the edge, another is created at the start.
83         float x,y,dx;
84         Uint16 color;
85 };
86
87 // ************************************* VARS
88 // SDL_Surface global variables
89 SDL_Surface 
90         *surf_screen,   // Screen
91         *surf_b_variations, // "variations" banner
92         *surf_b_on, // "on" banner
93         *surf_b_rockdodger, // "rockdodger" banner
94         *surf_b_game,   // Title element "game"
95         *surf_b_over,   // Title element "over"
96         *surf_ship,             // Spaceship element
97         *surf_life,     // Indicator of number of ships remaining
98         *surf_rock[NROCKS],     // THE ROCKS
99         *surf_font_big; // The big font
100
101 struct shape rock_shapes[NROCKS];
102
103 SFont_Font *g_font;
104
105 // Structure global variables
106 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
107 struct rock_struct rock[MAXROCKS], *rockptr = rock;
108 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
109 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
110 struct spacedot sdot[MAXSPACEDOTS];
111
112 // Other global variables
113 char topline[1024];
114 char *initerror = "";
115
116 float shipx,shipy = 240.0;      // X position, 0..XSIZE
117 float shipdx,shipdy;    // Change in X position per tick.
118 float rockrate,rockspeed;
119 float movementrate;  // this controls the speed of everything that moves.
120 float yscroll;
121 float scrollvel;
122
123 int nships,score,initticks,ticks_since_last, last_ticks;
124 int gameover;
125 int countdown = 0;
126 int maneuver = 0;
127 int sound_flag = 1, music_flag = 0;
128 int tail_plume = 0; // display big engine at the back?
129 int friction = 0;       // should there be friction?
130 float fadetimer = 0, faderate;
131
132 int pausedown = 0, paused = 0;
133
134 // bangdot start (bd1) and end (bd2) position:
135 int bd1 = 0, bd2 = 0;
136
137 enum states {
138         TITLE_PAGE,
139         GAMEPLAY,
140         DEAD_PAUSE,
141         GAME_OVER,
142         HIGH_SCORE_ENTRY,
143         HIGH_SCORE_DISPLAY
144 };
145 enum states state = TITLE_PAGE;
146 float state_timeout = 600.0;
147
148 #define NSEQUENCE 2
149 char *sequence[] = {
150         "Press SPACE to start",
151         "http://qualdan.com/vor/"
152 };
153
154 int bangdotlife, nbangdots;
155 Uint16 heatcolor[W*3];
156
157 char *data_dir;
158 extern char *optarg;
159 extern int optind, opterr, optopt;
160
161 float dist_sq(float x1, float y1, float x2, float y2)
162 {
163         return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
164 }
165
166 // ************************************* FUNCS
167
168 float
169 rnd() {
170         return (float)random()/(float)RAND_MAX;
171 }
172
173 void
174 init_engine_dots() {
175         int i;
176         for(i = 0; i<MAXENGINEDOTS; i++) {
177                 edot[i].active = 0;
178         }
179 }
180
181 void
182 init_space_dots() {
183         int i,intensity;
184         for(i = 0; i<MAXSPACEDOTS; i++) {
185                 float r;
186
187                 sdot[i].x = rnd()*(XSIZE-5);
188                 sdot[i].y = rnd()*(YSIZE-5);
189
190                 r = rnd()*rnd();
191
192                 sdot[i].dx = -r*4;
193                 // -1/((1-r) + .3);
194                 intensity = (int)(r*180 + 70);
195                 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
196
197         }
198 }
199
200 void
201 makebangdots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
202
203         // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
204         // TODO - generate and display dots in a circular buffer
205
206         int x,y,endcount;
207         Uint16 *rawpixel,c;
208         double theta,r;
209         int begin_generate;
210
211         begin_generate = SDL_GetTicks();
212
213         SDL_LockSurface(s);
214         rawpixel = (Uint16 *) s->pixels;
215
216         //for(n = 0; n <= power/2; n++) {
217
218         endcount = 0;
219         while (endcount<3) {
220                 for(x = 0; x<s->w; x++) {
221                         for(y = 0; y<s->h; y++) {
222                                 c = rawpixel[s->pitch/2*y + x];
223                                 if(c && c != s->format->colorkey) {
224
225                                         theta = rnd()*M_PI*2;
226
227                                         r = 1-(rnd()*rnd());
228
229                                         bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + dx;
230                                         bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + dy;
231                                         bdot[bd2].x = x + xbang;
232                                         bdot[bd2].y = y + ybang;
233
234                                         // Replace the last few bang dots with the pixels from the exploding object
235                                         bdot[bd2].c = (endcount>0)?c:0;
236                                         bdot[bd2].life = 100;
237                                         bdot[bd2].decay = rnd()*3 + 1;
238                                         bdot[bd2].active = 1;
239
240                                         bd2++;
241                                         bd2 %= MAXBANGDOTS;
242
243                                         // If the circular buffer is filled, who cares? They've had their chance.
244                                         //if(bd2 == bd1-1) goto exitloop;
245
246                                 }
247                         }
248                 }
249
250                 if(SDL_GetTicks() - begin_generate > 7) endcount++;
251         }
252
253         SDL_UnlockSurface(s);
254
255 }
256
257 void
258 draw_bang_dots(SDL_Surface *s) {
259         int i;
260         int first_i, last_i;
261         Uint16 *rawpixel;
262         rawpixel = (Uint16 *) s->pixels;
263
264         first_i = -1;
265
266         for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
267
268                 i %= MAXBANGDOTS;
269
270                 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
271                         // If the dot has drifted outside the perimeter, kill it
272                         bdot[i].active = 0;
273                 }
274
275                 if(bdot[i].active) {
276                         if(first_i < 0)
277                         first_i = i;
278                         //last_i = i + 1;
279                         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)];
280                         bdot[i].life -= bdot[i].decay;
281                         bdot[i].x += bdot[i].dx*movementrate;
282                         bdot[i].y += bdot[i].dy*movementrate + yscroll;
283
284                         if(bdot[i].life<0)
285                         bdot[i].active = 0;
286                 }
287         }
288
289         if(first_i >= 0) {
290                 bd1 = first_i;
291                 bd2 = last_i;
292         }
293         else {
294                 bd1 = 0;
295                 bd2 = 0;
296         }
297
298 }
299
300
301 void
302 draw_space_dots(SDL_Surface *s) {
303         int i;
304         Uint16 *rawpixel;
305         rawpixel = (Uint16 *) s->pixels;
306
307         for(i = 0; i<MAXSPACEDOTS; i++) {
308                 if(sdot[i].y<0) {
309                         sdot[i].y = 0;
310                 }
311                 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
312                 sdot[i].x += sdot[i].dx*movementrate;
313                 sdot[i].y += yscroll;
314                 if(sdot[i].y > YSIZE) {
315                         sdot[i].y -= YSIZE;
316                 } else if(sdot[i].y < 0) {
317                         sdot[i].y += YSIZE;
318                 }
319                 if(sdot[i].x<0) {
320                         sdot[i].x = XSIZE;
321                 }
322         }
323 }
324
325 void
326 draw_engine_dots(SDL_Surface *s) {
327         int i;
328         Uint16 *rawpixel;
329         rawpixel = (Uint16 *) s->pixels;
330
331         for(i = 0; i<MAXENGINEDOTS; i++) {
332                 if(edot[i].active) {
333                         edot[i].x += edot[i].dx*movementrate;
334                         edot[i].y += edot[i].dy*movementrate + yscroll;
335                         if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
336                                 edot[i].active = 0;
337                         } else if(edot[i].x<0 || edot[i].x>XSIZE) {
338                                 edot[i].active = 0;
339                         } else {
340                                 int heatindex;
341                                 heatindex = edot[i].life * 6;
342                                 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
343                                 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
344                         }
345                 }
346         }
347 }
348
349 void
350 create_engine_dots(int newdots) {
351         int i;
352         double theta,r,dx,dy;
353
354         if(!tail_plume) return;
355
356         if(state == GAMEPLAY) {
357                 for(i = 0; i<newdots*movementrate; i++) {
358                         if(dotptr->active == 0) {
359                                 theta = rnd()*M_PI*2;
360                                 r = rnd();
361                                 dx = cos(theta)*r;
362                                 dy = sin(theta)*r;
363
364                                 dotptr->active = 1;
365                                 dotptr->x = shipx + surf_ship->w/2-14;
366                                 dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*5-1;
367                                 dotptr->dx = 10*(dx-1.5) + shipdx;
368                                 dotptr->dy = 1*dy + shipdy;
369                                 dotptr->life = 45 + rnd(1)*5;
370
371                                 dotptr++;
372                                 if(dotptr-edot >= MAXENGINEDOTS) {
373                                         dotptr = edot;
374                                 }
375                         }
376                 }
377         }
378 }
379
380 void
381 create_engine_dots2(int newdots, int m) {
382         int i;
383         double theta, theta2, dx, dy, adx, ady;
384
385         // Don't create fresh engine dots when
386         // the game is not being played and a demo is not beng shown
387         if(state != GAMEPLAY) return;
388
389         for(i = 0; i<newdots; i++) {
390                 if(dotptr->active == 0) {
391                         theta = rnd()*M_PI*2;
392                         theta2 = rnd()*M_PI*2;
393
394                         dx = cos(theta) * fabs(cos(theta2));
395                         dy = sin(theta) * fabs(cos(theta2));
396                         adx = fabs(dx);
397                         ady = fabs(dy);
398
399
400                         dotptr->active = 1;
401                         dotptr->x = shipx + surf_ship->w/2 + (rnd()-0.5)*3;
402                         dotptr->y = shipy + surf_ship->h/2 + (rnd()-0.5)*3;
403
404                         switch(m) {
405                                 case 0:
406                                         dotptr->x -= 14;
407                                         dotptr->dx = -20*adx + shipdx;
408                                         dotptr->dy = 2*dy + shipdy;
409                                         dotptr->life = 60 * adx;
410                                 break;
411                                 case 1:
412                                         dotptr->dx = 2*dx + shipdx;
413                                         dotptr->dy = -20*ady + shipdy;
414                                         dotptr->life = 60 * ady;
415                                 break;
416                                 case 2:
417                                         dotptr->x += 14;
418                                         dotptr->dx = 20*adx + shipdx;
419                                         dotptr->dy = 2*dy + shipdy;
420                                         dotptr->life = 60 * adx;
421                                 break;
422                                 case 3:
423                                         dotptr->dx = 2*dx + shipdx;
424                                         dotptr->dy = 20*ady + shipdy;
425                                         dotptr->life = 60 * ady;
426                                 break;
427                         }
428                         dotptr++;
429                         if(dotptr-edot >= MAXENGINEDOTS) {
430                                 dotptr = edot;
431                         }
432                 }
433         }
434 }
435
436 void
437 drawdots(SDL_Surface *s) {
438         int m;
439
440         SDL_LockSurface(s);
441         // Draw the background stars aka space dots
442         draw_space_dots(s);
443
444         // Draw the score when playing the game or whn the game is freshly over
445         if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
446                 SDL_UnlockSurface(s);
447
448                 snprintscore_line(topline, 50, score);
449                 SFont_Write(s,g_font,XSIZE-250,0,topline);
450
451                 SDL_LockSurface(s);
452         }
453
454         // Draw all the engine dots
455         draw_engine_dots(s);
456
457         // Create more engine dots comin out da back
458         if(!gameover) create_engine_dots(200);
459
460         // Create engine dots out the side we're moving from
461         for(m = 0; m<4; m++) {
462                 if(maneuver & 1<<m) { // 'maneuver' is a bit field
463                         create_engine_dots2(80,m);
464                 }
465         }
466
467         // Draw all outstanding bang dots
468         //if(bangdotlife-- > 0) 
469         draw_bang_dots(s);
470
471         SDL_UnlockSurface(s);
472 }
473
474                 char a[MAX_PATH_LEN];
475 int
476 init(int fullscreen) {
477
478         int i,j;
479         SDL_Surface *temp;
480         Uint16 *raw_pixels;
481         Uint32 flag;
482
483         // Where are our data files?
484         if(!find_files()) exit(1);
485         read_high_score_table();
486
487         if(sound_flag) {
488                 // Initialize SDL with audio and video
489                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
490                         sound_flag = 0;
491                         printf ("Can't open sound, starting without it\n");
492                         atexit(SDL_Quit);
493                 } else {
494                         atexit(SDL_Quit);
495                         atexit(SDL_CloseAudio);
496                         sound_flag = init_sound();
497                 }
498         } else {
499                 // Initialize with video only
500                 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
501                 atexit(SDL_Quit);
502         }
503
504         play_tune(0);
505
506         // Attempt to get the required video size
507         flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
508         if(fullscreen) flag |= SDL_FULLSCREEN;
509         surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
510
511         // Set the title bar text
512         SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
513
514         NULLERROR(surf_screen);
515
516         // Set the heat color from the range 0 (cold) to 300 (blue-white)
517         for(i = 0; i<W*3; i++) {
518                 heatcolor[i] = SDL_MapRGB(
519                         surf_screen->format,
520                         (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?
521                 );
522         }
523
524         // Load the banners
525         NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
526         NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
527
528         NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
529         NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
530
531         NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
532         NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
533
534         NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
535         NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
536
537         NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
538         NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
539
540         surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
541         g_font = SFont_InitFont(surf_font_big);
542
543         // Load the spaceship graphic.
544         NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
545         NULLERROR(surf_ship = SDL_DisplayFormat(temp));
546
547         // Load the life indicator (small ship) graphic.
548         NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
549         NULLERROR(surf_life = SDL_DisplayFormat(temp));
550
551         // Create the array of black points;
552         SDL_LockSurface(surf_ship);
553         raw_pixels = (Uint16 *) surf_ship->pixels;
554         for(i = 0; i<surf_ship->w; i++) {
555                 for(j = 0; j<surf_ship->h; j++) {
556                         if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
557                                 blackptr->x = i;
558                                 blackptr->y = j;
559                                 blackptr++;
560                         }
561                 }
562         }
563
564         SDL_UnlockSurface(surf_ship);
565
566         init_engine_dots();
567         init_space_dots();
568
569         // Load all our lovely rocks
570         for(i = 0; i<NROCKS; i++) {
571
572                 snprintf(a,MAX_PATH_LEN,add_path("sprites/rock%02d.png"),i);
573                 NULLERROR(temp = IMG_Load(a));
574                 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
575                 get_shape(surf_rock[i], &rock_shapes[i]);
576         }
577
578         // Remove the mouse cursor
579 #ifdef SDL_DISABLE
580         SDL_ShowCursor(SDL_DISABLE);
581 #endif
582
583         return 0;
584 }
585
586 int
587 draw() {
588         int i;
589         SDL_Rect src,dest;
590         struct black_point_struct *p;
591         Uint16 *raw_pixels;
592         int bang, offset, x;
593         char *text;
594         float fadegame,fadeover;
595
596         bang = 0;
597
598         src.x = 0;
599         src.y = 0;
600         dest.x = 0;
601         dest.y = 0;
602
603         // Draw a fully black background
604         SDL_FillRect(surf_screen,NULL,0);
605
606         // Draw the background dots
607         drawdots(surf_screen);
608
609         // Draw ship
610         if(!gameover && state == GAMEPLAY ) {
611                 src.w = surf_ship->w;
612                 src.h = surf_ship->h;
613                 dest.w = src.w;
614                 dest.h = src.h;
615                 dest.x = (int)shipx;
616                 dest.y = (int)shipy;
617                 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
618         }
619
620         // Draw all the rocks, in all states
621         for(i = 0; i<MAXROCKS; i++) {
622                 if(rock[i].active) {
623
624                         src.w = rock[i].image->w;
625                         src.h = rock[i].image->h;
626                         dest.w = src.w;
627                         dest.h = src.h;
628                         dest.x = (int) rock[i].x;
629                         dest.y = (int) rock[i].y;
630
631                         // Draw the rock
632                         SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
633
634                 }
635         }
636
637         // If it's game over, show the game over graphic in the dead centre
638         switch (state) {
639                 case GAME_OVER:
640                         if(fadetimer<3.0/faderate) {
641                                 fadegame = fadetimer/(3.0/faderate);
642                         } else {
643                                 fadegame = 1.0;
644                         }
645
646                         if(fadetimer<3.0/faderate) {
647                                 fadeover = 0.0;
648                         } else if(fadetimer<6.0/faderate) {
649                                 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
650                         } else {
651                                 fadeover = 1.0;
652                         }
653
654                         src.w = surf_b_game->w;
655                         src.h = surf_b_game->h;
656                         dest.w = src.w;
657                         dest.h = src.h;
658                         dest.x = (XSIZE-src.w)/2;
659                         dest.y = (YSIZE-src.h)/2-40;
660                         SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
661                         SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
662
663                         src.w = surf_b_over->w;
664                         src.h = surf_b_over->h;
665                         dest.w = src.w;
666                         dest.h = src.h;
667                         dest.x = (XSIZE-src.w)/2;
668                         dest.y = (YSIZE-src.h)/2 + 40;
669                         SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
670                         SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
671                 break;
672
673                 case TITLE_PAGE:
674
675                         src.w = surf_b_variations->w;
676                         src.h = surf_b_variations->h;
677                         dest.w = src.w;
678                         dest.h = src.h;
679                         dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
680                         dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
681                         SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
682                         SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
683
684                         src.w = surf_b_on->w;
685                         src.h = surf_b_on->h;
686                         dest.w = src.w;
687                         dest.h = src.h;
688                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
689                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
690                         SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
691                         SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
692
693                         src.w = surf_b_rockdodger->w;
694                         src.h = surf_b_rockdodger->h;
695                         dest.w = src.w;
696                         dest.h = src.h;
697                         dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
698                         dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
699                         SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
700                         SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
701
702                         text = "Version " VERSION;
703                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
704                         SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
705
706                         text = sequence[(int)(fadetimer/40)%NSEQUENCE];
707                         //text = "Press SPACE to start!";
708                         x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
709                         SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
710                 break;
711
712                 case HIGH_SCORE_ENTRY:
713                         play_tune(2);
714                         if(!process_score_input()) {  // done inputting name
715
716                                 // Change state to briefly show high scores page
717                                 state = HIGH_SCORE_DISPLAY;
718                                 state_timeout = 200;
719
720                                 // Write the high score table to the file
721                                 write_high_score_table();
722                 
723                                 // Play the title page tune
724                                 play_tune(0);
725                         }
726                 // FALL THROUGH TO
727                 case HIGH_SCORE_DISPLAY:
728                         // Display de list o high scores mon.
729                         display_scores(surf_screen, 150,50);
730                         break;
731                 case GAMEPLAY:
732                 case DEAD_PAUSE:
733                         ; // no action necessary
734         }
735
736         if(!gameover && state == GAMEPLAY) {
737                 SDL_LockSurface(surf_screen);
738                 raw_pixels = (Uint16 *) surf_screen->pixels;
739                 // Check that the black points on the ship are
740                 // still black, and not covered up by rocks.
741                 for(p = black_point; p<blackptr; p++) { 
742                         offset = surf_screen->pitch/2 * (p->y + (int)shipy) + p->x + (int)shipx;
743                         if(raw_pixels[offset]) {
744                                 // Set the bang flag
745                                 bang = 1;
746                         }
747                 }
748                 SDL_UnlockSurface(surf_screen);
749         }
750
751         // Draw all the little ships
752         if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
753         for(i = 0; i<nships-1; i++) {
754                 src.w = surf_life->w;
755                 src.h = surf_life->h;
756                 dest.w = src.w;
757                 dest.h = src.h;
758                 dest.x = (i + 1)*(src.w + 10);
759                 dest.y = 20;
760                 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
761         }
762
763
764         // Update the score
765         /*
766         n = SDL_GetTicks()-initticks;
767         if(score)
768         ticks_since_last = n-score;
769         score = n;
770         */
771
772         ticks_since_last = SDL_GetTicks()-last_ticks;
773         last_ticks = SDL_GetTicks();
774         if(ticks_since_last>200 || ticks_since_last<0) {
775                 movementrate = 0;
776         }
777         else {
778                 movementrate = ticks_since_last/50.0;
779                 if(state == GAMEPLAY) {
780                         score += ticks_since_last;
781                 }
782         }
783
784         // Update the surface
785         SDL_Flip(surf_screen);
786
787
788         return bang;
789 }
790
791 int
792 gameloop() {
793         int i = 0;
794         Uint8 *keystate;
795
796
797         for(;;) {
798                 if(!paused) {
799                         // Count down the game loop timer, and change state when it gets to zero or less;
800
801                         if((state_timeout -= movementrate*3) < 0) {
802                                 switch(state) {
803                                         case DEAD_PAUSE:
804                                                 // Create a new ship and start all over again
805                                                 state = GAMEPLAY;
806                                                 play_tune(1);
807                                                 shipx -= 50;
808                                                 break;
809                                         case GAME_OVER:
810                                                 state = HIGH_SCORE_ENTRY;
811                                                 state_timeout = 5.0e6;
812                                                 if(new_high_score(score)) {
813                                                         SDL_Event e;
814                                                         SDL_EnableUNICODE(1);
815                                                         while(SDL_PollEvent(&e))
816                                                                 ;
817                                                 } else {
818                                                         state = HIGH_SCORE_DISPLAY;
819                                                         state_timeout = 400;
820                                                 }
821                                                 break;
822                                         case HIGH_SCORE_DISPLAY:
823                                                 state = TITLE_PAGE;
824                                                 state_timeout = 500.0;
825                                                 break;
826                                         case HIGH_SCORE_ENTRY:
827                                                 // state = TITLE_PAGE;
828                                                 // play_tune(1);
829                                                 // state_timeout = 100.0;
830                                                 break;
831                                         case TITLE_PAGE:
832                                                 state = HIGH_SCORE_DISPLAY;
833                                                 state_timeout = 200.0;
834                                                 break;
835                                         case GAMEPLAY:
836                                                 ; // no action necessary
837                                 }
838                         } else {
839                                 if(state == DEAD_PAUSE) {
840                                         float blast_radius;
841                                         int fixonly;
842
843                                         if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
844                                                 blast_radius = BLAST_RADIUS * 1.3;
845                                                 fixonly = 1;
846                                         } else {
847                                                 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
848                                                 fixonly = 0;
849                                         }
850
851                                         if(shipx < 60) shipx = 60;
852                                         for(i = 0; i<MAXROCKS; i++ ) {
853                                                 float dx, dy, n;
854                                                 if(rock[i].x <= 0) continue;
855
856                                                 // This makes it so your explosion from dying magically doesn't leave
857                                                 // any rocks that aren't moving much on the x axis. After the first
858                                                 // 20 tics, only rocks that are barely moving will be pushed.
859                                                 if(fixonly && (!rock[i].dead || rock[i].dx < -4 || rock[i].dx > 3)) {
860                                                         continue;
861                                                 }
862
863                                                 dx = rock[i].x - shipx;
864                                                 dy = rock[i].y - shipy;
865
866                                                 n = sqrt(dx*dx + dy*dy);
867                                                 if(n < blast_radius) {
868                                                         n *= 20;
869                                                         rock[i].dx += rockrate*(dx+30)/n;
870                                                         rock[i].dy += rockrate*dy/n;
871                                                         rock[i].dead = 1;
872                                                 }
873                                         }
874                                 }
875                         }
876
877                         if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
878                                 // Create a rock
879                                 rockptr++;
880                                 if(rockptr-rock >= MAXROCKS) {
881                                         rockptr = rock;
882                                 }
883                                 if(!rockptr->active) {
884                                         rockptr->x = (float)XSIZE;
885                                         rockptr->dx = -(rockspeed)*(1 + rnd());
886                                         rockptr->dy = rnd()-0.5;
887                                         rockptr->type_number = random() % NROCKS;
888                                         rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
889                                         rockptr->active = 1;
890                                         rockptr->y = rnd()*(YSIZE + rockptr->image->h);
891                                 }
892                                 if(movementrate>0.1) {
893                                         countdown = (int)(ROCKRATE/movementrate);
894                                 } else {
895                                         countdown = 0;
896                                 }
897                         }
898
899                         // FRICTION?
900                         if(friction) {
901                                 shipdx *= pow((double)0.9,(double)movementrate);
902                                 shipdy *= pow((double)0.9,(double)movementrate);
903                                 // if(abs(shipdx)<0.00001) shipdx = 0;
904                                 // if(abs(shipdy)<0.00001) shipdy = 0;
905                         }
906
907                         // INERTIA
908                         shipx += shipdx*movementrate;
909                         shipy += shipdy*movementrate;
910
911                         // SCROLLING
912                         yscroll = shipy - (YSIZE / 2);
913                         yscroll += shipdy * 25;
914                         yscroll /= -25;
915                         yscroll = ((scrollvel * (12 - movementrate)) + (yscroll * movementrate)) / 12;
916                         scrollvel = yscroll;
917                         yscroll = yscroll*movementrate;
918                         shipy += yscroll;
919                         
920                         // Move all the rocks
921                         for(i = 0; i < MAXROCKS; i++) {
922                                 if(rock[i].active) {
923                                         rock[i].x += rock[i].dx*movementrate;
924                                         rock[i].y += rock[i].dy*movementrate + yscroll;
925                                         if(rock[i].y > YSIZE || rock[i].y < -rock[i].image->h) {
926                                                 if(rock[i].dead) {
927                                                         rock[i].dead = 0;
928                                                         rock[i].active = 0;
929                                                 } else {
930                                                         // wrap
931                                                         rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y;
932                                                         rock[i].y += (rock[i].dy*movementrate + yscroll) * 1.01;
933                                                 }
934                                         }
935                                         if(rock[i].x < -rock[i].image->w || rock[i].x > XSIZE) {
936                                                 rock[i].active = 0;
937                                                 rock[i].dead = 0;
938                                         }
939                                 }
940                         }
941
942
943                         // BOUNCE X
944                         if(shipx<0 || shipx>XSIZE-surf_ship->w) {
945                                 // BOUNCE from left and right wall
946                                 shipx -= shipdx*movementrate;
947                                 shipdx *= -0.99;
948                         }
949
950                         // BOUNCE Y
951                         if(shipy<0 || shipy>YSIZE-surf_ship->h) {
952                                 // BOUNCE from top and bottom wall
953                                 shipy -= shipdy;
954                                 shipdy *= -0.99;
955                         }
956
957
958                         if(draw() && state == GAMEPLAY) {
959                                 // Play the explosion sound
960                                 play_sound(0);
961                                 makebangdots(shipx,shipy,shipdx,shipdy,surf_ship,30);
962                                 if(--nships <= 0) {
963                                         gameover = 1;
964                                         state = GAME_OVER;
965                                         state_timeout = 200.0;
966                                         fadetimer = 0.0;
967                                         faderate = movementrate;
968                                 }
969                                 else {
970                                         state = DEAD_PAUSE;
971                                         state_timeout = DEAD_PAUSE_LENGTH;
972                                         shipdx = 0;
973                                         shipdy = 0;
974                                 }
975                         }
976
977                         SDL_PumpEvents();
978                         keystate = SDL_GetKeyState(NULL);
979
980                         if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
981                                 return 0;
982                         }
983
984                         if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
985
986                                 for(i = 0; i<MAXROCKS; i++ ) {
987                                         rock[i].active = 0;
988                                         rock[i].dead = 0;
989                                 }
990
991                                 rockrate = 54.0;
992                                 rockspeed = 5.0;
993
994                                 nships = 4;
995                                 score = 0;
996
997                                 state = GAMEPLAY;
998                                 play_tune(1);
999
1000                                 gameover = 0;
1001                                 shipx = 0;
1002                                 shipy = YSIZE/2;
1003                                 shipdx = -1;
1004                                 shipdy = 0;
1005                         }
1006
1007                         maneuver = 0;
1008                 } else {
1009                         SDL_PumpEvents();
1010                         keystate = SDL_GetKeyState(NULL);
1011                 }
1012
1013                 if(state == GAMEPLAY) {
1014                         if(!gameover) {
1015
1016                                 if(!paused) {
1017                                         if(keystate[SDLK_UP] | keystate[SDLK_c])                { shipdy -= 1.5*movementrate; maneuver |= 1<<3;}
1018                                         if(keystate[SDLK_DOWN] | keystate[SDLK_t])              { shipdy += 1.5*movementrate; maneuver |= 1<<1;}
1019                                         if(keystate[SDLK_LEFT] | keystate[SDLK_h])              { shipdx -= 1.5*movementrate; maneuver |= 1<<2;}
1020                                         if(keystate[SDLK_RIGHT] | keystate[SDLK_n])             { shipdx += 1.5*movementrate; maneuver |= 1;}
1021                                         if(keystate[SDLK_3])            { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1022                                 }
1023
1024                                 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1025                                         if(!pausedown) {
1026                                                 paused = !paused;
1027                                                 pausedown = 1;
1028                                         }
1029                                 } else {
1030                                         pausedown = 0;
1031                                 }
1032
1033                         }
1034                         else {
1035                                 paused = 0;
1036                                 pausedown = 0;
1037                         }
1038                 } else if(state == GAME_OVER) {
1039                         if(keystate[SDLK_SPACE]) {
1040                                 state_timeout = -1;
1041                         }
1042                 }
1043         }
1044 }
1045
1046 int
1047 main(int argc, char **argv) {
1048         int i, x, fullscreen;
1049
1050         fullscreen = 0;
1051         tail_plume = 0;
1052         friction = 0;
1053         sound_flag = 1;
1054         music_flag = 0;
1055
1056         while ((x = getopt(argc,argv,"efhmps")) >= 0) {
1057                 switch(x) {
1058                         case 'e': // engine
1059                                 tail_plume = 1;
1060                         break;
1061                         case 'f': // fullscreen
1062                                 fullscreen = 1;
1063                         break;
1064                         case 'h': // help
1065                                 printf("Variations on RockDodger\n"
1066                                        " -e big tail [E]ngine\n"
1067                                        " -f [F]ull screen\n"
1068                                        " -h this [H]elp message\n"
1069                                        " -m enable [M]usic\n"
1070                                        " -p original [P]hysics (friction)\n"
1071                                        " -s [S]ilent (no sound)\n");
1072                                 exit(0);
1073                         break;
1074                         case 'm': // music
1075                                 music_flag = 1;
1076                         case 'p': // physics
1077                                 friction = 1;
1078                         break;
1079                         case 's': // silent
1080                                 sound_flag = 0;
1081                                 music_flag = 0;
1082                         break;
1083                 }
1084         }
1085
1086         if(init(fullscreen)) {
1087                 printf ("ta: '%s'\n",initerror);
1088                 return 1;
1089         }
1090
1091         for(i = 0; i<MAXROCKS; i++) {
1092                 rock[i].active = 0;
1093                 rock[i].dead = 0;
1094         }
1095         rockrate = 54.0;
1096         rockspeed = 5.0;
1097         initticks = SDL_GetTicks();
1098         gameloop();
1099
1100         return 0;
1101 }