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