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;
30 #include <SDL/SDL_image.h>
37 #include <sys/types.h>
46 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
47 #define NULLERROR(a) CONDERROR((a) == NULL)
50 // ************************************* STRUCTS
52 // Array of black pixel coordinates. This is scanned
53 // every frame to see if it's still black, and as
54 // soon as it isn't we BLOW UP
57 int dead; // has been blown out of the way
58 // to make room for a new ship appearing.
63 struct black_point_struct {
67 // Bang dots have the same colour as shield dots.
68 // Bang dots get darker as they age.
69 // Some are coloured the same as the ex-ship.
71 Uint16 c; // when zero, use heatcolor[bangdotlife]
72 float life; // When reduced to 0, set active = 0
74 float decay;// Amount by which to reduce life each time dot is drawn
77 // Engine dots stream out the back of the ship, getting darker as they go.
80 // The life of an engine dot
81 // is a number starting at between 0 and 50 and counting backward.
82 float life; // When reduced to 0, set active = 0
85 // Space dots are harmless background items
86 // All are active. When one falls off the edge, another is created at the start.
90 // High score table {{{
107 // ************************************* VARS
108 // SDL_Surface global variables {{{
110 *surf_screen, // Screen
111 *surf_b_variations, // "variations" banner
112 *surf_b_on, // "on" banner
113 *surf_b_rockdodger, // "rockdodger" banner
114 *surf_b_game, // Title element "game"
115 *surf_b_over, // Title element "over"
116 *surf_ship, // Spaceship element
117 *surf_life, // Indicator of number of ships remaining
118 *surf_rock[NROCKS], // THE ROCKS
119 *surf_deadrock[NROCKS], // THE DEAD ROCKS
120 *surf_font_big; // The big font
122 // Structure global variables {{{
123 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
124 struct rock_struct rock[MAXROCKS], *rockptr = rock;
125 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
126 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
127 struct spacedot sdot[MAXSPACEDOTS];
129 // Other global variables {{{
131 char *initerror = "";
132 char name[1024], debug1[1024];
134 float xship,yship = 240.0; // X position, 0..XSIZE
135 float xvel,yvel; // Change in X position per tick.
136 float rockrate,rockspeed;
141 int nships,score,initticks,ticks_since_last, last_ticks;
145 int oss_sound_flag = 0;
146 int tail_plume = 0; // display big engine at the back?
147 int friction = 0; // should there be friction?
149 float fadetimer = 0,faderate;
151 int pausedown = 0,paused = 0;
153 // bangdot start (bd1) and end (bd2) position:
154 int bd1 = 0, bd2 = 0;
156 int xoffset[NROCKS][MAXROCKHEIGHT];
167 enum states state = TITLE_PAGE;
168 float state_timeout = 600.0;
170 const int fakesin[] = {0,1,0,-1};
171 const int fakecos[] = {1,0,-1,0};
174 "Press SPACE to start",
175 "http://qualdan.com/vor/"
178 int bangdotlife, nbangdots;
179 Uint16 heatcolor[W*3];
183 extern int optind, opterr, optopt;
186 float dist_sq(float x1, float y1, float x2, float y2)
188 return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
191 // ************************************* FUNCS
193 FILE *hs_fopen(char *mode) {
197 if(f = fopen("/usr/share/vor/.highscore",mode)) {
204 sprintf(s,"%s/.vor-high",getenv("HOME"));
205 if(f = fopen(s,mode)) {
215 void read_high_score_table() {
218 if(f = hs_fopen("r")) {
219 // If the file exists, read from it
220 for(i = 0; i<8; i++) {
223 if(fscanf (f, "%d %[^\n]", &highscore, s) != 2) {
226 if(high[i].allocated) {
229 high[i].name = strdup(s);
230 high[i].score = highscore;
231 high[i].allocated = 1;
236 void write_high_score_table() {
239 if(f = hs_fopen("w")) {
240 // If the file exists, write to it
241 for(i = 0; i<8; i++) {
242 fprintf (f, "%d %s\n", high[i].score, high[i].name);
247 void snprintscore(char *s, size_t n, int score) {
248 int min = score/60000;
249 int sec = score/1000%60;
250 int tenths = score%1000/100;
252 snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
254 snprintf(s, n, " %2d.%d", sec, tenths);
258 return (float)random()/(float)RAND_MAX;
260 void init_engine_dots() {
262 for(i = 0; i<MAXENGINEDOTS; i++) {
266 void init_space_dots() {
268 for(i = 0; i<MAXSPACEDOTS; i++) {
271 sdot[i].x = rnd()*(XSIZE-5);
272 sdot[i].y = rnd()*(YSIZE-5);
278 intensity = (int)(r*180 + 70);
279 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
284 int makebangdots(int xbang, int ybang, int xvel, int yvel, SDL_Surface *s, int power) {
286 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
287 // TODO - generate and display dots in a circular buffer
289 int i,x,y,n,endcount;
291 double theta,r,dx,dy;
294 begin_generate = SDL_GetTicks();
297 rawpixel = (Uint16 *) s->pixels;
299 //for(n = 0; n <= power/2; n++) {
304 for(x = 0; x<s->w; x++) {
305 for(y = 0; y<s->h; y++) {
306 c = rawpixel[s->pitch/2*y + x];
307 if(c && c != SDL_MapRGB(s->format,0,255,0)) {
309 theta = rnd()*M_PI*2;
313 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + xvel;
314 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + yvel;
315 bdot[bd2].x = x + xbang;
316 bdot[bd2].y = y + ybang;
318 // Replace the last few bang dots with the pixels from the exploding object
319 bdot[bd2].c = (endcount>0)?c:0;
320 bdot[bd2].life = 100;
321 bdot[bd2].decay = rnd()*3 + 1;
322 bdot[bd2].active = 1;
327 // If the circular buffer is filled, who cares? They've had their chance.
328 //if(bd2 == bd1-1) goto exitloop;
334 if(SDL_GetTicks() - begin_generate > 7) endcount++;
339 SDL_UnlockSurface(s);
343 void draw_bang_dots(SDL_Surface *s) {
347 rawpixel = (Uint16 *) s->pixels;
351 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
355 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
356 // If the dot has drifted outside the perimeter, kill it
364 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)];
365 bdot[i].life -= bdot[i].decay;
366 bdot[i].x += bdot[i].dx*movementrate;
367 bdot[i].y += bdot[i].dy*movementrate + yscroll;
386 void draw_space_dots(SDL_Surface *s) {
389 rawpixel = (Uint16 *) s->pixels;
391 for(i = 0; i<MAXSPACEDOTS; i++) {
395 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
396 sdot[i].x += sdot[i].dx*movementrate;
397 sdot[i].y += yscroll;
398 if(sdot[i].y > YSIZE) {
400 } else if(sdot[i].y < 0) {
409 void draw_engine_dots(SDL_Surface *s) {
412 rawpixel = (Uint16 *) s->pixels;
414 for(i = 0; i<MAXENGINEDOTS; i++) {
416 edot[i].x += edot[i].dx*movementrate;
417 edot[i].y += edot[i].dy*movementrate + yscroll;
418 if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
420 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
424 heatindex = edot[i].life * 6;
425 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
426 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
432 void create_engine_dots(int newdots) {
434 double theta,r,dx,dy;
436 if(!tail_plume) return;
438 if(state == GAMEPLAY) {
439 for(i = 0; i<newdots*movementrate; i++) {
440 if(dotptr->active == 0) {
441 theta = rnd()*M_PI*2;
447 dotptr->x = xship + surf_ship->w/2-14;
448 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*5-1;
449 dotptr->dx = 10*(dx-1.5) + xvel;
450 dotptr->dy = 1*dy + yvel;
451 dotptr->life = 45 + rnd(1)*5;
454 if(dotptr-edot >= MAXENGINEDOTS) {
462 void create_engine_dots2(int newdots, int m) {
464 double theta, theta2, dx, dy, adx, ady;
466 // Don't create fresh engine dots when
467 // the game is not being played and a demo is not beng shown
468 if(state != GAMEPLAY && state != DEMO) return;
470 for(i = 0; i<newdots; i++) {
471 if(dotptr->active == 0) {
472 theta = rnd()*M_PI*2;
473 theta2 = rnd()*M_PI*2;
475 dx = cos(theta) * fabs(cos(theta2));
476 dy = sin(theta) * fabs(cos(theta2));
482 dotptr->x = xship + surf_ship->w/2 + (rnd()-0.5)*3;
483 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*3;
488 dotptr->dx = -20*adx + xvel;
489 dotptr->dy = 2*dy + yvel;
490 dotptr->life = 60 * adx;
493 dotptr->dx = 2*dx + xvel;
494 dotptr->dy = -20*ady + yvel;
495 dotptr->life = 60 * ady;
499 dotptr->dx = 20*adx + xvel;
500 dotptr->dy = 2*dy + yvel;
501 dotptr->life = 60 * adx;
504 dotptr->dx = 2*dx + xvel;
505 dotptr->dy = 20*ady + yvel;
506 dotptr->life = 60 * ady;
510 if(dotptr-edot >= MAXENGINEDOTS) {
517 int drawdots(SDL_Surface *s) {
521 // Draw the background stars aka space dots
524 // Draw the score when playing the game or whn the game is freshly over
525 if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
526 SDL_UnlockSurface(s);
528 scorepos = XSIZE-250;
529 n = snprintf(topline, 50, "Time: ");
530 snprintscore(topline + n, 50-n, score);
531 PutString(s,scorepos,0,topline);
536 // Draw all the engine dots
539 // Create more engine dots comin out da back
541 create_engine_dots(200);
543 // Create engine dots out the side we're moving from
544 for(m = 0; m<4; m++) {
545 if(maneuver & 1<<m) { // 'maneuver' is a bit field
546 create_engine_dots2(80,m);
550 // Draw all outstanding bang dots
551 //if(bangdotlife-- > 0)
554 SDL_UnlockSurface(s);
557 char * load_file(char *s) {
558 static char retval[1024];
559 snprintf(retval, 1024, "%s/%s", data_dir, s);
564 int missing(char *dirname) {
567 return (!S_ISDIR(buf.st_mode));
570 int init(int fullscreen) {
577 read_high_score_table();
579 // Where are our data files?
581 // second alternative: RD_DATADIR
582 // final alternative: /usr/share/vor
583 data_dir = strdup("./data");
584 if(missing(data_dir)) {
586 env = getenv("RD_DATADIR");
588 data_dir = strdup(env);
589 if(missing(data_dir)) {
590 fprintf (stderr,"Cannot find data directory $RD_DATADIR\n");
594 data_dir = strdup("/usr/share/vor");
595 if(missing(data_dir)) {
596 fprintf (stderr,"Cannot find data in %s\n", data_dir);
604 // Initialise SDL with audio and video
605 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
607 printf ("Can't open sound, starting without it\n");
611 atexit(SDL_CloseAudio);
612 oss_sound_flag = init_sound();
616 // Initialise with video only
617 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
624 // Attempt to get the required video size
625 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
626 if(fullscreen) flag |= SDL_FULLSCREEN;
627 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
629 // Set the title bar text
630 SDL_WM_SetCaption("Rock Dodgers", "rockdodgers");
632 NULLERROR(surf_screen);
634 // Set the heat color from the range 0 (cold) to 300 (blue-white)
635 for(i = 0; i<W*3; i++) {
636 heatcolor[i] = SDL_MapRGB(
638 (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?
643 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
644 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
646 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
647 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
649 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
650 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
652 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
653 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
655 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
656 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
658 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
659 InitFont(surf_font_big);
661 // Load the spaceship graphic.
662 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
663 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
665 // Load the life indicator (small ship) graphic.
666 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
667 NULLERROR(surf_life = SDL_DisplayFormat(temp));
669 // Create the array of black points;
670 SDL_LockSurface(surf_ship);
671 raw_pixels = (Uint16 *) surf_ship->pixels;
672 for(i = 0; i<surf_ship->w; i++) {
673 for(j = 0; j<surf_ship->h; j++) {
674 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
682 SDL_UnlockSurface(surf_ship);
687 // Load all our lovely rocks
688 for(i = 0; i<NROCKS; i++) {
691 sprintf(a,load_file("sprites/rock%d.png"),i);
692 NULLERROR(temp = IMG_Load(a));
693 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
695 sprintf(a,load_file("sprites/deadrock%d.png"),i);
696 NULLERROR(temp = IMG_Load(a));
697 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
700 // Remove the mouse cursor
702 SDL_ShowCursor(SDL_DISABLE);
710 struct black_point_struct *p;
714 float fadegame,fadeover;
716 char *statedisplay, buf[1024];
725 // Draw a fully black background
726 SDL_FillRect(surf_screen,NULL,0);
731 // Show the current state
734 statedisplay = "title_page";
737 statedisplay = "gameplay";
740 statedisplay = "dead_pause";
743 statedisplay = "game_over";
745 case HIGH_SCORE_ENTRY:
746 statedisplay = "high_score_entry";
748 case HIGH_SCORE_DISPLAY:
749 statedisplay = "high_score_display";
752 statedisplay = "demo";
755 snprintf(buf,1024, "mode = %s", statedisplay);
756 PutString(surf_screen,0,YSIZE-50,buf);
760 // Draw the background dots
761 drawdots(surf_screen);
764 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
765 src.w = surf_ship->w;
766 src.h = surf_ship->h;
771 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
774 // Draw all the rocks, in all states
775 for(i = 0; i<MAXROCKS; i++) {
778 src.w = rock[i].image->w;
779 src.h = rock[i].image->h;
782 dest.x = (int) rock[i].x;
783 dest.y = (int) rock[i].y;
786 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
788 // Draw the heated part of the rock, in an alpha which reflects the
789 // amount of heat in the rock.
791 SDL_Surface *deadrock;
792 deadrock = surf_deadrock[rock[i].type_number];
793 SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
794 dest.x = (int) rock[i].x; // kludge
795 SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
797 rock[i].heat -= movementrate;
801 // If the rock is heated past a certain point, the water content of
802 // the rock flashes to steam, releasing enough energy to destroy
803 // the rock in spectacular fashion.
804 if(rock[i].heat>rock[i].image->h) {
806 play_sound(1 + (int)(rnd()*3));
807 makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
813 // If it's game over, show the game over graphic in the dead centre
816 if(fadetimer<3.0/faderate) {
817 fadegame = fadetimer/(3.0/faderate);
822 if(fadetimer<3.0/faderate) {
824 } else if(fadetimer<6.0/faderate) {
825 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
830 src.w = surf_b_game->w;
831 src.h = surf_b_game->h;
834 dest.x = (XSIZE-src.w)/2;
835 dest.y = (YSIZE-src.h)/2-40;
836 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
837 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
839 src.w = surf_b_over->w;
840 src.h = surf_b_over->h;
843 dest.x = (XSIZE-src.w)/2;
844 dest.y = (YSIZE-src.h)/2 + 40;
845 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
846 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
851 src.w = surf_b_variations->w;
852 src.h = surf_b_variations->h;
855 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
856 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
857 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
858 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
860 src.w = surf_b_on->w;
861 src.h = surf_b_on->h;
864 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
865 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
866 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
867 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
869 src.w = surf_b_rockdodger->w;
870 src.h = surf_b_rockdodger->h;
873 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
874 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
875 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
876 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
878 text = "Version " VERSION;
879 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
880 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
882 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
883 //text = "Press SPACE to start!";
884 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
885 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
888 case HIGH_SCORE_ENTRY:
890 if(score >= high[7].score) {
892 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
893 // Insert name into high score table
895 // Lose the lowest name forever (loser!)
896 //if(high[7].allocated)
897 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
899 // Insert new high score
900 high[scorerank].score = score;
901 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
902 high[scorerank].allocated = 1;
904 // Set the global name string to "", ready for the next winner
907 // Change state to briefly show high scores page
908 state = HIGH_SCORE_DISPLAY;
911 // Write the high score table to the file
912 write_high_score_table();
914 // Play the title page tune
918 state = HIGH_SCORE_DISPLAY;
923 case HIGH_SCORE_DISPLAY:
924 // Display de list o high scores mon.
925 PutString(surf_screen,180,50,"High scores");
926 for(i = 0; i<8; i++) {
928 sprintf(s, "#%1d",i + 1);
929 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
930 snprintscore(s, 1024, high[i].score);
931 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
932 sprintf(s, "%3s", high[i].name);
933 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
938 if(!gameover && state == GAMEPLAY) {
939 SDL_LockSurface(surf_screen);
940 raw_pixels = (Uint16 *) surf_screen->pixels;
941 // Check that the black points on the ship are
942 // still black, and not covered up by rocks.
943 for(p = black_point; p<blackptr; p++) {
944 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
945 if(raw_pixels[offset]) {
950 SDL_UnlockSurface(surf_screen);
953 // Draw all the little ships
954 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
955 for(i = 0; i<nships-1; i++) {
956 src.w = surf_life->w;
957 src.h = surf_life->h;
960 dest.x = (i + 1)*(src.w + 10);
962 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
968 n = SDL_GetTicks()-initticks;
970 ticks_since_last = n-score;
974 ticks_since_last = SDL_GetTicks()-last_ticks;
975 last_ticks = SDL_GetTicks();
976 if(ticks_since_last>200 || ticks_since_last<0) {
980 movementrate = ticks_since_last/50.0;
981 if(state == GAMEPLAY) {
982 score += ticks_since_last;
986 // Update the surface
987 SDL_Flip(surf_screen);
999 // Count down the game loop timer, and change state when it gets to zero or less;
1001 if((state_timeout -= movementrate*3) < 0) {
1004 // Create a new ship and start all over again
1010 state = HIGH_SCORE_ENTRY;
1013 state_timeout = 5.0e6;
1015 if(score >= high[7].score) {
1016 // Read the high score table from the storage file
1017 read_high_score_table();
1019 // Find ranking of this score, store as scorerank
1020 for(i = 0; i<8; i++) {
1021 if(high[i].score <= score) {
1027 // Move all lower scores down a notch
1028 for(i = 7; i >= scorerank; i--)
1029 high[i] = high[i-1];
1031 // Insert blank high score
1032 high[scorerank].score = score;
1033 high[scorerank].name = "";
1034 high[scorerank].allocated = 0;
1038 case HIGH_SCORE_DISPLAY:
1040 state_timeout = 500.0;
1042 case HIGH_SCORE_ENTRY:
1043 // state = TITLE_PAGE;
1045 // state_timeout = 100.0;
1048 state = HIGH_SCORE_DISPLAY;
1049 state_timeout = 200.0;
1053 if(state == DEAD_PAUSE) {
1054 float blast_radius = START_RAD * state_timeout / 20.0;
1055 if(xship < 60) xship = 60;
1056 for(i = 0; i<MAXROCKS; i++ ) {
1058 if(rock[i].x <= 0) continue;
1059 dx = rock[i].x - xship;
1060 dy = rock[i].y - yship;
1061 n = sqrt(dx*dx + dy*dy);
1062 if(n < blast_radius) {
1064 rock[i].xvel += rockrate*(dx+30)/n;
1065 rock[i].yvel += rockrate*dy/n;
1072 if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
1075 if(rockptr-rock >= MAXROCKS) {
1078 if(!rockptr->active) {
1079 rockptr->x = (float)XSIZE;
1080 rockptr->xvel = -(rockspeed)*(1 + rnd());
1081 rockptr->yvel = rnd()-0.5;
1082 rockptr->type_number = random() % NROCKS;
1084 rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
1085 rockptr->active = 1;
1086 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
1088 if(movementrate>0.1) {
1089 countdown = (int)(ROCKRATE/movementrate);
1097 xvel *= pow((double)0.9,(double)movementrate);
1098 yvel *= pow((double)0.9,(double)movementrate);
1099 // if(abs(xvel)<0.00001) xvel = 0;
1100 // if(abs(yvel)<0.00001) yvel = 0;
1104 xship += xvel*movementrate;
1105 yship += yvel*movementrate;
1108 yscroll = yship - (YSIZE / 2);
1109 yscroll += yvel * 25;
1111 yscroll = ((scrollvel * (12 - movementrate)) + (yscroll * movementrate)) / 12;
1112 scrollvel = yscroll;
1113 yscroll = yscroll*movementrate;
1116 // Move all the rocks
1117 for(i = 0; i < MAXROCKS; i++) {
1118 if(rock[i].active) {
1119 rock[i].x += rock[i].xvel*movementrate;
1120 rock[i].y += rock[i].yvel*movementrate + yscroll;
1121 if((rock[i].y > YSIZE && rock[i].y > 0) || (rock[i].y < -rock[i].image->h && rock[i].y < 0)) {
1127 rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y;
1128 rock[i].y += (rock[i].yvel*movementrate + yscroll) * 2;
1131 if(rock[i].x < -32.0 || rock[i].x > XSIZE + 32.0) {
1140 if(xship<0 || xship>XSIZE-surf_ship->w) {
1141 // BOUNCE from left and right wall
1142 xship -= xvel*movementrate;
1147 if(yship<0 || yship>YSIZE-surf_ship->h) {
1148 // BOUNCE from top and bottom wall
1154 if(draw() && state == GAMEPLAY) {
1155 if(oss_sound_flag) {
1156 // Play the explosion sound
1159 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1163 state_timeout = 200.0;
1165 faderate = movementrate;
1169 state_timeout = 20.0;
1176 keystate = SDL_GetKeyState(NULL);
1178 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1182 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1184 for(i = 0; i<MAXROCKS; i++ ) {
1209 keystate = SDL_GetKeyState(NULL);
1212 if(state == GAMEPLAY) {
1216 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver |= 1<<3;}
1217 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver |= 1<<1;}
1218 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver |= 1<<2;}
1219 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver |= 1;}
1220 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1223 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1228 src.w = surf_b_variations->w;
1229 src.h = surf_b_variations->h;
1232 dest.x = (XSIZE-src.w)/2;
1233 dest.y = (YSIZE-src.h)/2;
1234 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1235 // Update the surface
1236 SDL_Flip(surf_screen);
1249 } else if(state == GAME_OVER) {
1250 if(keystate[SDLK_SPACE]) {
1256 main(int argc, char **argv) {
1257 int i, x, fullscreen;
1264 while ((x = getopt(argc,argv,"efhsp")) >= 0) {
1269 case 'f': // fullscreen
1273 printf("Variations on RockDodger\n"
1274 " -e big tail [E]ngine\n"
1275 " -f [F]ull screen\n"
1276 " -h this [H]elp message\n"
1277 " -p original [P]hysics (friction)\n"
1278 " -s [S]ilent (no sound)\n");
1281 case 'p': // physics
1290 if(init(fullscreen)) {
1291 printf ("ta: '%s'\n",initerror);
1296 for(i = 0; i<MAXROCKS; i++) {
1302 initticks = SDL_GetTicks();
1303 if(gameloop() == 0) {
1306 printf ("score = %d\n",score);