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