1 /* Variations on RockDodger
2 * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
4 * Project fork 2004, Jason Woofenden and Joshua Grams.
5 * (a whole bunch of modifications and project rename)
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.
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.
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
24 extern int font_height;
29 #include <SDL/SDL_image.h>
36 #include <sys/types.h>
45 #define NROCKS 6 // Number of rock image files, not number of rocks visible
46 #define MAXROCKS 120 // MAX Rocks
47 #define MAXROCKHEIGHT 100
49 #define MAXBLACKPOINTS 500
50 #define MAXENGINEDOTS 5000
51 #define MAXBANGDOTS 50000
52 #define MAXSPACEDOTS 2000
55 #define BIG_FONT_FILE "fonts/score.png"
56 #define STARTSPACE 430 // pixels from the left which will be cleared of rocks when you die
59 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
60 #define NULLERROR(a) CONDERROR((a) == NULL)
63 // ************************************* STRUCTS
65 // Array of black pixel coordinates. This is scanned
66 // every frame to see if it's still black, and as
67 // soon as it isn't we BLOW UP
74 struct black_point_struct {
78 // Bang dots have the same colour as shield dots.
79 // Bang dots get darker as they age.
80 // Some are coloured the same as the ex-ship.
82 Uint16 c; // when zero, use heatcolor[bangdotlife]
83 float life; // When reduced to 0, set active = 0
85 float decay;// Amount by which to reduce life each time dot is drawn
88 // Engine dots stream out the back of the ship, getting darker as they go.
91 // The life of an engine dot
92 // is a number starting at between 0 and 50 and counting backward.
93 float life; // When reduced to 0, set active = 0
96 // Space dots are harmless background items
97 // All are active. When one falls off the edge, another is created at the start.
101 // High score table {{{
118 // ************************************* VARS
119 // SDL_Surface global variables {{{
121 *surf_screen, // Screen
122 *surf_b_variations, // "variations" banner
123 *surf_b_on, // "on" banner
124 *surf_b_rockdodger, // "rockdodger" banner
125 *surf_b_game, // Title element "game"
126 *surf_b_over, // Title element "over"
127 *surf_ship, // Spaceship element
128 *surf_life, // Indicator of number of ships remaining
129 *surf_rock[NROCKS], // THE ROCKS
130 *surf_deadrock[NROCKS], // THE DEAD ROCKS
131 *surf_font_big; // The big font
133 // Structure global variables {{{
134 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
135 struct rock_struct rock[MAXROCKS], *rockptr = rock;
136 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
137 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
138 struct spacedot sdot[MAXSPACEDOTS];
140 // Other global variables {{{
142 char *initerror = "";
143 char name[1024], debug1[1024];
145 float xship,yship = 240.0; // X position, 0..XSIZE
146 float xvel,yvel; // Change in X position per tick.
147 float rockrate,rockspeed;
149 float shieldlevel, shieldpulse = 0;
152 int nships,score,initticks,ticks_since_last, last_ticks;
153 int initialshield, gameover, fast;
158 int oss_sound_flag = 0;
159 int tail_plume = 0; // display big engine at the back?
160 int friction = 0; // should there be friction?
162 float fadetimer = 0,faderate;
164 int pausedown = 0,paused = 0;
166 // bangdot start (bd1) and end (bd2) position:
167 int bd1 = 0, bd2 = 0;
169 int xoffset[NROCKS][MAXROCKHEIGHT];
180 enum states state = TITLE_PAGE;
181 float state_timeout = 600.0;
183 const int fakesin[] = {0,1,0,-1};
184 const int fakecos[] = {1,0,-1,0};
187 "Press SPACE to start",
188 "http://qualdan.com/vor/"
191 int bangdotlife, nbangdots;
192 Uint16 heatcolor[W*3];
196 extern int optind, opterr, optopt;
199 // ************************************* FUNCS
201 int dotcollision(SDL_Surface *s) {
203 Uint16 *rawpixel, *r;
206 * Kill all the dots which collide with other objects.
207 * This does not work, it's probably in the wrong place or something.
210 rawpixel = (Uint16 *) s->pixels;
211 if(bangdotlife > 0 && bangdotlife<80) {
212 for(i = 0; i<nbangdots; i++) {
213 if(bdot[i].x>0 && bdot[i].x<XSIZE && bdot[i].y>0 && bdot[i].y<YSIZE) {
214 r = &rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)];
215 if(*r != (bdot[i].c ? bdot[i].c : heatcolor[bangdotlife*2]))
220 SDL_UnlockSurface(s);
226 FILE *hs_fopen(char *mode) {
230 if(f = fopen("/usr/share/vor/.highscore",mode)) {
237 sprintf(s,"%s/.vor-high",getenv("HOME"));
238 if(f = fopen(s,mode)) {
248 void read_high_score_table() {
251 if(f = hs_fopen("r")) {
252 // If the file exists, read from it
253 for(i = 0; i<8; i++) {
256 if(fscanf (f, "%d %[^\n]", &highscore, s) != 2) {
259 if(high[i].allocated) {
262 high[i].name = strdup(s);
263 high[i].score = highscore;
264 high[i].allocated = 1;
269 void write_high_score_table() {
272 if(f = hs_fopen("w")) {
273 // If the file exists, write to it
274 for(i = 0; i<8; i++) {
275 fprintf (f, "%d %s\n", high[i].score, high[i].name);
280 void snprintscore(char *s, size_t n, int score) {
281 int min = score/60000;
282 int sec = score/1000%60;
283 int tenths = score%1000/100;
285 snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
287 snprintf(s, n, " %2d.%d", sec, tenths);
291 return (float)random()/(float)RAND_MAX;
293 void init_engine_dots() {
295 for(i = 0; i<MAXENGINEDOTS; i++) {
299 void init_space_dots() {
301 for(i = 0; i<MAXSPACEDOTS; i++) {
304 sdot[i].x = rnd()*(XSIZE-5);
305 sdot[i].y = rnd()*(YSIZE-5);
311 intensity = (int)(r*180 + 70);
312 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
323 // let xc = x coordinate of the collision between the laser and a space rock
324 // 1. Calculate xc and determine the asteroid that was hit
325 for(i = 0; i<MAXROCKS; i++) {
327 if(yship + 12>rock[i].y && yship + 12<rock[i].y + rock[i].image->h && xship + 32<rock[i].x + (rock[i].image->w/2) && rock[i].x + (rock[i].image->w/2) < xc) {
328 xc = rock[i].x + (rock[i].image->w/2);
335 rock[hitrock].heat += movementrate*3;
338 // Plot a number of random dots between xship and XSIZE
339 SDL_LockSurface(surf_screen);
340 rawpixel = (Uint16 *) surf_screen->pixels;
341 c = SDL_MapRGB(surf_ship->format,rnd()*128,128 + rnd()*120,rnd()*128);
343 for(i = 0; i<(xc-xship)*5; i += 10) {
345 x = rnd()*(xc-(xship + 32)) + xship + 32;
346 y = yship + 12 + (rnd()-0.5)*1.5;
347 rawpixel[surf_screen->pitch/2*y + x] = c;
350 SDL_UnlockSurface(surf_screen);
354 int makebangdots(int xbang, int ybang, int xvel, int yvel, SDL_Surface *s, int power) {
356 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
357 // TODO - generate and display dots in a circular buffer
359 int i,x,y,n,endcount;
361 double theta,r,dx,dy;
364 begin_generate = SDL_GetTicks();
367 rawpixel = (Uint16 *) s->pixels;
369 //for(n = 0; n <= power/2; n++) {
374 for(x = 0; x<s->w; x++) {
375 for(y = 0; y<s->h; y++) {
376 c = rawpixel[s->pitch/2*y + x];
377 if(c && c != SDL_MapRGB(s->format,0,255,0)) {
379 theta = rnd()*M_PI*2;
383 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + xvel;
384 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + yvel;
385 bdot[bd2].x = x + xbang;
386 bdot[bd2].y = y + ybang;
388 // Replace the last few bang dots with the pixels from the exploding object
389 bdot[bd2].c = (endcount>0)?c:0;
390 bdot[bd2].life = 100;
391 bdot[bd2].decay = rnd()*3 + 1;
392 bdot[bd2].active = 1;
397 // If the circular buffer is filled, who cares? They've had their chance.
398 //if(bd2 == bd1-1) goto exitloop;
404 if(SDL_GetTicks() - begin_generate > 7) endcount++;
409 SDL_UnlockSurface(s);
413 void draw_bang_dots(SDL_Surface *s) {
417 rawpixel = (Uint16 *) s->pixels;
421 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
425 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
426 // If the dot has drifted outside the perimeter, kill it
434 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)];
435 bdot[i].life -= bdot[i].decay;
436 bdot[i].x += bdot[i].dx*movementrate;
437 bdot[i].y += bdot[i].dy*movementrate + yscroll;
456 void draw_space_dots(SDL_Surface *s) {
459 rawpixel = (Uint16 *) s->pixels;
461 for(i = 0; i<MAXSPACEDOTS; i++) {
465 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
466 sdot[i].x += sdot[i].dx*movementrate;
467 sdot[i].y += yscroll;
468 if(sdot[i].y > YSIZE) {
470 } else if(sdot[i].y < 0) {
479 void draw_engine_dots(SDL_Surface *s) {
482 rawpixel = (Uint16 *) s->pixels;
484 for(i = 0; i<MAXENGINEDOTS; i++) {
486 edot[i].x += edot[i].dx*movementrate;
487 edot[i].y += edot[i].dy*movementrate + yscroll;
488 if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
490 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
494 heatindex = edot[i].life * 6;
495 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
496 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
502 void create_engine_dots(int newdots) {
504 double theta,r,dx,dy;
506 if(!tail_plume) return;
508 if(state == GAMEPLAY) {
509 for(i = 0; i<newdots*movementrate; i++) {
510 if(dotptr->active == 0) {
511 theta = rnd()*M_PI*2;
517 dotptr->x = xship + surf_ship->w/2-14;
518 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*5-1;
519 dotptr->dx = 10*(dx-1.5) + xvel;
520 dotptr->dy = 1*dy + yvel;
521 dotptr->life = 45 + rnd(1)*5;
524 if(dotptr-edot >= MAXENGINEDOTS) {
532 void create_engine_dots2(int newdots, int m) {
534 double theta, theta2, dx, dy, adx, ady;
536 // Don't create fresh engine dots when
537 // the game is not being played and a demo is not beng shown
538 if(state != GAMEPLAY && state != DEMO) return;
540 for(i = 0; i<newdots; i++) {
541 if(dotptr->active == 0) {
542 theta = rnd()*M_PI*2;
543 theta2 = rnd()*M_PI*2;
545 dx = cos(theta) * fabs(cos(theta2));
546 dy = sin(theta) * fabs(cos(theta2));
552 dotptr->x = xship + surf_ship->w/2 + (rnd()-0.5)*3;
553 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*3;
558 dotptr->dx = -20*adx + xvel;
559 dotptr->dy = 2*dy + yvel;
560 dotptr->life = 60 * adx;
563 dotptr->dx = 2*dx + xvel;
564 dotptr->dy = -20*ady + yvel;
565 dotptr->life = 60 * ady;
569 dotptr->dx = 20*adx + xvel;
570 dotptr->dy = 2*dy + yvel;
571 dotptr->life = 60 * adx;
574 dotptr->dx = 2*dx + xvel;
575 dotptr->dy = 20*ady + yvel;
576 dotptr->life = 60 * ady;
580 if(dotptr-edot >= MAXENGINEDOTS) {
587 int drawdots(SDL_Surface *s) {
591 // Draw the background stars aka space dots
594 // Draw the score when playing the game or whn the game is freshly over
595 if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
596 SDL_UnlockSurface(s);
598 scorepos = XSIZE-250;
599 n = snprintf(topline, 50, "Time: ");
600 snprintscore(topline + n, 50-n, score);
601 PutString(s,scorepos,0,topline);
606 // Draw all the engine dots
609 // Create more engine dots comin out da back
611 create_engine_dots(200);
613 // Create engine dots out the side we're moving from
614 for(m = 0; m<4; m++) {
615 if(maneuver & 1<<m) { // 'maneuver' is a bit field
616 create_engine_dots2(80,m);
620 // Draw all outstanding bang dots
621 //if(bangdotlife-- > 0)
624 SDL_UnlockSurface(s);
627 char * load_file(char *s) {
628 static char retval[1024];
629 snprintf(retval, 1024, "%s/%s", data_dir, s);
634 int missing(char *dirname) {
637 return (!S_ISDIR(buf.st_mode));
640 int init(int fullscreen) {
647 read_high_score_table();
649 // Where are our data files?
651 // second alternative: RD_DATADIR
652 // final alternative: /usr/share/vor
653 data_dir = strdup("./data");
654 if(missing(data_dir)) {
656 env = getenv("RD_DATADIR");
658 data_dir = strdup(env);
659 if(missing(data_dir)) {
660 fprintf (stderr,"Cannot find data directory $RD_DATADIR\n");
664 data_dir = strdup("/usr/share/vor");
665 if(missing(data_dir)) {
666 fprintf (stderr,"Cannot find data in %s\n", data_dir);
674 // Initialise SDL with audio and video
675 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
677 printf ("Can't open sound, starting without it\n");
681 atexit(SDL_CloseAudio);
682 oss_sound_flag = init_sound();
686 // Initialise with video only
687 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
694 // Attempt to get the required video size
695 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
696 if(fullscreen) flag |= SDL_FULLSCREEN;
697 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
699 // Set the title bar text
700 SDL_WM_SetCaption("Rock Dodgers", "rockdodgers");
702 NULLERROR(surf_screen);
704 // Set the heat color from the range 0 (cold) to 300 (blue-white)
705 for(i = 0; i<W*3; i++) {
706 heatcolor[i] = SDL_MapRGB(
708 (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?
713 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
714 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
716 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
717 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
719 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
720 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
722 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
723 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
725 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
726 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
728 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
729 InitFont(surf_font_big);
731 // Load the spaceship graphic.
732 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
733 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
735 // Load the life indicator (small ship) graphic.
736 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
737 NULLERROR(surf_life = SDL_DisplayFormat(temp));
739 // Create the array of black points;
740 SDL_LockSurface(surf_ship);
741 raw_pixels = (Uint16 *) surf_ship->pixels;
742 for(i = 0; i<surf_ship->w; i++) {
743 for(j = 0; j<surf_ship->h; j++) {
744 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
752 SDL_UnlockSurface(surf_ship);
757 // Load all our lovely rocks
758 for(i = 0; i<NROCKS; i++) {
761 sprintf(a,load_file("sprites/rock%d.png"),i);
762 NULLERROR(temp = IMG_Load(a));
763 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
765 sprintf(a,load_file("sprites/deadrock%d.png"),i);
766 NULLERROR(temp = IMG_Load(a));
767 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
770 // Remove the mouse cursor
772 SDL_ShowCursor(SDL_DISABLE);
780 struct black_point_struct *p;
784 float fadegame,fadeover;
786 char *statedisplay, buf[1024];
795 // Draw a fully black background
796 SDL_FillRect(surf_screen,NULL,0);
801 // Show the current state
804 statedisplay = "title_page";
807 statedisplay = "gameplay";
810 statedisplay = "dead_pause";
813 statedisplay = "game_over";
815 case HIGH_SCORE_ENTRY:
816 statedisplay = "high_score_entry";
818 case HIGH_SCORE_DISPLAY:
819 statedisplay = "high_score_display";
822 statedisplay = "demo";
825 snprintf(buf,1024, "mode = %s", statedisplay);
826 PutString(surf_screen,0,YSIZE-50,buf);
830 // Draw the background dots
831 drawdots(surf_screen);
833 // If it's firing, draw the laser
839 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
840 src.w = surf_ship->w;
841 src.h = surf_ship->h;
846 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
849 // Draw all the rocks, in all states
850 for(i = 0; i<MAXROCKS; i++) {
853 src.w = rock[i].image->w;
854 src.h = rock[i].image->h;
857 dest.x = (int) rock[i].x;
858 dest.y = (int) rock[i].y;
861 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
863 // Draw the heated part of the rock, in an alpha which reflects the
864 // amount of heat in the rock.
866 SDL_Surface *deadrock;
867 deadrock = surf_deadrock[rock[i].type_number];
868 SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
869 dest.x = (int) rock[i].x; // kludge
870 SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
872 rock[i].heat -= movementrate;
876 // If the rock is heated past a certain point, the water content of
877 // the rock flashes to steam, releasing enough energy to destroy
878 // the rock in spectacular fashion.
879 if(rock[i].heat>rock[i].image->h) {
881 play_sound(1 + (int)(rnd()*3));
882 makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
888 // If it's game over, show the game over graphic in the dead centre
891 if(fadetimer<3.0/faderate) {
892 fadegame = fadetimer/(3.0/faderate);
897 if(fadetimer<3.0/faderate) {
899 } else if(fadetimer<6.0/faderate) {
900 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
905 src.w = surf_b_game->w;
906 src.h = surf_b_game->h;
909 dest.x = (XSIZE-src.w)/2;
910 dest.y = (YSIZE-src.h)/2-40;
911 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
912 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
914 src.w = surf_b_over->w;
915 src.h = surf_b_over->h;
918 dest.x = (XSIZE-src.w)/2;
919 dest.y = (YSIZE-src.h)/2 + 40;
920 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
921 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
926 src.w = surf_b_variations->w;
927 src.h = surf_b_variations->h;
930 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
931 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
932 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
933 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
935 src.w = surf_b_on->w;
936 src.h = surf_b_on->h;
939 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
940 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
941 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
942 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
944 src.w = surf_b_rockdodger->w;
945 src.h = surf_b_rockdodger->h;
948 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
949 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
950 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
951 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
953 text = "Version " VERSION;
954 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
955 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
957 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
958 //text = "Press SPACE to start!";
959 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
960 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
963 case HIGH_SCORE_ENTRY:
965 if(score >= high[7].score) {
967 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
968 // Insert name into high score table
970 // Lose the lowest name forever (loser!)
971 //if(high[7].allocated)
972 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
974 // Insert new high score
975 high[scorerank].score = score;
976 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
977 high[scorerank].allocated = 1;
979 // Set the global name string to "", ready for the next winner
982 // Change state to briefly show high scores page
983 state = HIGH_SCORE_DISPLAY;
986 // Write the high score table to the file
987 write_high_score_table();
989 // Play the title page tune
993 state = HIGH_SCORE_DISPLAY;
998 case HIGH_SCORE_DISPLAY:
999 // Display de list o high scores mon.
1000 PutString(surf_screen,180,50,"High scores");
1001 for(i = 0; i<8; i++) {
1003 sprintf(s, "#%1d",i + 1);
1004 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
1005 snprintscore(s, 1024, high[i].score);
1006 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
1007 sprintf(s, "%3s", high[i].name);
1008 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
1013 if(!gameover && state == GAMEPLAY) {
1014 // Show the freaky shields
1015 SDL_LockSurface(surf_screen);
1016 raw_pixels = (Uint16 *) surf_screen->pixels;
1017 if(initialshield>0 || shieldsup && shieldlevel>0) {
1021 if(initialshield>0) {
1022 initialshield -= movementrate;
1023 c = SDL_MapRGB(surf_screen->format,0,255,255);
1025 c = heatcolor[(int)shieldlevel];
1026 shieldlevel -= movementrate;
1030 for(p = black_point; p<blackptr; p++) {
1031 x = p->x + (int)xship + (rnd() + rnd()-1)*sin(shieldpulse)*4 + 1;
1032 y = p->y + (int)yship + (rnd() + rnd()-1)*sin(shieldpulse)*4 + 1;
1033 if(x>0 && y>0 && x<XSIZE && y<YSIZE) {
1034 offset = surf_screen->pitch/2 * y + x;
1035 raw_pixels[offset] = c;
1039 // When the shields are off, check that the black points
1040 // on the ship are still black, and not covered up by rocks
1041 for(p = black_point; p<blackptr; p++) {
1042 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
1043 if(raw_pixels[offset]) {
1044 // Set the bang flag
1049 SDL_UnlockSurface(surf_screen);
1053 dotcollision(surf_screen); // Kill dots that are not on their spots
1056 // Draw all the little ships
1057 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
1058 for(i = 0; i<nships-1; i++) {
1059 src.w = surf_life->w;
1060 src.h = surf_life->h;
1063 dest.x = (i + 1)*(src.w + 10);
1065 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
1071 n = SDL_GetTicks()-initticks;
1073 ticks_since_last = n-score;
1077 ticks_since_last = SDL_GetTicks()-last_ticks;
1078 last_ticks = SDL_GetTicks();
1079 if(ticks_since_last>200 || ticks_since_last<0) {
1083 movementrate = ticks_since_last/50.0;
1084 if(state == GAMEPLAY) {
1085 score += ticks_since_last;
1089 // Update the surface
1090 SDL_Flip(surf_screen);
1102 // Count down the game loop timer, and change state when it gets to zero or less;
1104 if((state_timeout -= movementrate*3) < 0) {
1107 // Create a new ship and start all over again
1116 for(i = 0; i<MAXROCKS; i++ ) {
1117 if(rock[i].x < STARTSPACE) {
1123 state = HIGH_SCORE_ENTRY;
1126 state_timeout = 5.0e6;
1128 if(score >= high[7].score) {
1129 // Read the high score table from the storage file
1130 read_high_score_table();
1132 // Find ranking of this score, store as scorerank
1133 for(i = 0; i<8; i++) {
1134 if(high[i].score <= score) {
1140 // Move all lower scores down a notch
1141 for(i = 7; i >= scorerank; i--)
1142 high[i] = high[i-1];
1144 // Insert blank high score
1145 high[scorerank].score = score;
1146 high[scorerank].name = "";
1147 high[scorerank].allocated = 0;
1151 case HIGH_SCORE_DISPLAY:
1153 state_timeout = 500.0;
1155 case HIGH_SCORE_ENTRY:
1156 // state = TITLE_PAGE;
1158 // state_timeout = 100.0;
1161 state = HIGH_SCORE_DISPLAY;
1162 state_timeout = 200.0;
1167 if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
1170 if(rockptr-rock >= MAXROCKS) {
1173 if(!rockptr->active) {
1174 rockptr->x = (float)XSIZE;
1175 rockptr->xvel = -(rockspeed)*(1 + rnd());
1176 rockptr->yvel = rnd()-0.5;
1177 rockptr->type_number = random() % NROCKS;
1179 rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
1180 rockptr->active = 1;
1181 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
1183 if(movementrate>0.1) {
1184 countdown = (int)(ROCKRATE/movementrate);
1192 xvel *= pow((double)0.9,(double)movementrate);
1193 yvel *= pow((double)0.9,(double)movementrate);
1194 // if(abs(xvel)<0.00001) xvel = 0;
1195 // if(abs(yvel)<0.00001) yvel = 0;
1199 xship += xvel*movementrate;
1200 yship += yvel*movementrate;
1203 yscroll = yship - (YSIZE / 2);
1205 yscroll = yscroll*movementrate;
1208 // Move all the rocks
1209 for(i = 0; i<MAXROCKS; i++) if(rock[i].active) {
1210 rock[i].x += rock[i].xvel*movementrate;
1211 rock[i].y += rock[i].yvel*movementrate + yscroll;
1212 if(rock[i].y > YSIZE) {
1214 rock[i].y -= rock[i].image->w;
1215 } else if(rock[i].y < -rock[i].image->w) {
1217 rock[i].y += rock[i].image->w;
1225 if(xship<0 || xship>XSIZE-surf_ship->w) {
1226 // BOUNCE from left and right wall
1227 xship -= xvel*movementrate;
1232 if(yship<0 || yship>YSIZE-surf_ship->h) {
1233 // BOUNCE from top and bottom wall
1239 if(draw() && state == GAMEPLAY) {
1240 if(oss_sound_flag) {
1241 // Play the explosion sound
1244 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1248 state_timeout = 200.0;
1250 faderate = movementrate;
1254 state_timeout = 50.0;
1260 keystate = SDL_GetKeyState(NULL);
1262 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1266 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1268 for(i = 0; i<MAXROCKS; i++ ) {
1295 keystate = SDL_GetKeyState(NULL);
1298 if(state == GAMEPLAY) {
1302 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver |= 1<<3;}
1303 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver |= 1<<1;}
1304 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver |= 1<<2;}
1305 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver |= 1;}
1306 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1309 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1314 src.w = surf_b_variations->w;
1315 src.h = surf_b_variations->h;
1318 dest.x = (XSIZE-src.w)/2;
1319 dest.y = (YSIZE-src.h)/2;
1320 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1321 // Update the surface
1322 SDL_Flip(surf_screen);
1336 } else if(state == GAME_OVER) {
1337 if(keystate[SDLK_SPACE]) {
1343 main(int argc, char **argv) {
1344 int i, x, fullscreen;
1351 while ((x = getopt(argc,argv,"efhsp")) >= 0) {
1356 case 'f': // fullscreen
1360 printf("Variations on RockDodger\n"
1361 " -e Big tail [E]ngine\n"
1362 " -f [F]ull screen\n"
1363 " -h This [H]elp message\n"
1364 " -p Stupid original [P]hysics (friction)\n"
1365 " -s [S]ilent (no sound)\n");
1368 case 'p': // physics
1377 if(init(fullscreen)) {
1378 printf ("ta: '%s'\n",initerror);
1383 for(i = 0; i<MAXROCKS; i++) {
1388 initticks = SDL_GetTicks();
1389 if(gameloop() == 0) {
1392 printf ("score = %d\n",score);