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 sound_flag = 1, music_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);
603 // Initialize SDL with audio and video
604 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
606 printf ("Can't open sound, starting without it\n");
610 atexit(SDL_CloseAudio);
611 sound_flag = init_sound();
614 // Initialize with video only
615 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
621 // Attempt to get the required video size
622 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
623 if(fullscreen) flag |= SDL_FULLSCREEN;
624 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
626 // Set the title bar text
627 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
629 NULLERROR(surf_screen);
631 // Set the heat color from the range 0 (cold) to 300 (blue-white)
632 for(i = 0; i<W*3; i++) {
633 heatcolor[i] = SDL_MapRGB(
635 (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?
640 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
641 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
643 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
644 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
646 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
647 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
649 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
650 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
652 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
653 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
655 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
656 InitFont(surf_font_big);
658 // Load the spaceship graphic.
659 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
660 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
662 // Load the life indicator (small ship) graphic.
663 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
664 NULLERROR(surf_life = SDL_DisplayFormat(temp));
666 // Create the array of black points;
667 SDL_LockSurface(surf_ship);
668 raw_pixels = (Uint16 *) surf_ship->pixels;
669 for(i = 0; i<surf_ship->w; i++) {
670 for(j = 0; j<surf_ship->h; j++) {
671 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
679 SDL_UnlockSurface(surf_ship);
684 // Load all our lovely rocks
685 for(i = 0; i<NROCKS; i++) {
688 sprintf(a,load_file("sprites/rock%d.png"),i);
689 NULLERROR(temp = IMG_Load(a));
690 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
692 sprintf(a,load_file("sprites/deadrock%d.png"),i);
693 NULLERROR(temp = IMG_Load(a));
694 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
697 // Remove the mouse cursor
699 SDL_ShowCursor(SDL_DISABLE);
707 struct black_point_struct *p;
711 float fadegame,fadeover;
713 char *statedisplay, buf[1024];
722 // Draw a fully black background
723 SDL_FillRect(surf_screen,NULL,0);
728 // Show the current state
731 statedisplay = "title_page";
734 statedisplay = "gameplay";
737 statedisplay = "dead_pause";
740 statedisplay = "game_over";
742 case HIGH_SCORE_ENTRY:
743 statedisplay = "high_score_entry";
745 case HIGH_SCORE_DISPLAY:
746 statedisplay = "high_score_display";
749 statedisplay = "demo";
752 snprintf(buf,1024, "mode = %s", statedisplay);
753 PutString(surf_screen,0,YSIZE-50,buf);
757 // Draw the background dots
758 drawdots(surf_screen);
761 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
762 src.w = surf_ship->w;
763 src.h = surf_ship->h;
768 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
771 // Draw all the rocks, in all states
772 for(i = 0; i<MAXROCKS; i++) {
775 src.w = rock[i].image->w;
776 src.h = rock[i].image->h;
779 dest.x = (int) rock[i].x;
780 dest.y = (int) rock[i].y;
783 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
785 // Draw the heated part of the rock, in an alpha which reflects the
786 // amount of heat in the rock.
788 SDL_Surface *deadrock;
789 deadrock = surf_deadrock[rock[i].type_number];
790 SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
791 dest.x = (int) rock[i].x; // kludge
792 SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
794 rock[i].heat -= movementrate;
798 // If the rock is heated past a certain point, the water content of
799 // the rock flashes to steam, releasing enough energy to destroy
800 // the rock in spectacular fashion.
801 if(rock[i].heat>rock[i].image->h) {
803 play_sound(1 + (int)(rnd()*3));
804 makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
810 // If it's game over, show the game over graphic in the dead centre
813 if(fadetimer<3.0/faderate) {
814 fadegame = fadetimer/(3.0/faderate);
819 if(fadetimer<3.0/faderate) {
821 } else if(fadetimer<6.0/faderate) {
822 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
827 src.w = surf_b_game->w;
828 src.h = surf_b_game->h;
831 dest.x = (XSIZE-src.w)/2;
832 dest.y = (YSIZE-src.h)/2-40;
833 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
834 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
836 src.w = surf_b_over->w;
837 src.h = surf_b_over->h;
840 dest.x = (XSIZE-src.w)/2;
841 dest.y = (YSIZE-src.h)/2 + 40;
842 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
843 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
848 src.w = surf_b_variations->w;
849 src.h = surf_b_variations->h;
852 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
853 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
854 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
855 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
857 src.w = surf_b_on->w;
858 src.h = surf_b_on->h;
861 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
862 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
863 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
864 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
866 src.w = surf_b_rockdodger->w;
867 src.h = surf_b_rockdodger->h;
870 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
871 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
872 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
873 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
875 text = "Version " VERSION;
876 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
877 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
879 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
880 //text = "Press SPACE to start!";
881 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
882 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
885 case HIGH_SCORE_ENTRY:
887 if(score >= high[7].score) {
889 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
890 // Insert name into high score table
892 // Lose the lowest name forever (loser!)
893 //if(high[7].allocated)
894 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
896 // Insert new high score
897 high[scorerank].score = score;
898 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
899 high[scorerank].allocated = 1;
901 // Set the global name string to "", ready for the next winner
904 // Change state to briefly show high scores page
905 state = HIGH_SCORE_DISPLAY;
908 // Write the high score table to the file
909 write_high_score_table();
911 // Play the title page tune
915 state = HIGH_SCORE_DISPLAY;
920 case HIGH_SCORE_DISPLAY:
921 // Display de list o high scores mon.
922 PutString(surf_screen,180,50,"High scores");
923 for(i = 0; i<8; i++) {
925 sprintf(s, "#%1d",i + 1);
926 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
927 snprintscore(s, 1024, high[i].score);
928 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
929 sprintf(s, "%3s", high[i].name);
930 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
935 if(!gameover && state == GAMEPLAY) {
936 SDL_LockSurface(surf_screen);
937 raw_pixels = (Uint16 *) surf_screen->pixels;
938 // Check that the black points on the ship are
939 // still black, and not covered up by rocks.
940 for(p = black_point; p<blackptr; p++) {
941 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
942 if(raw_pixels[offset]) {
947 SDL_UnlockSurface(surf_screen);
950 // Draw all the little ships
951 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
952 for(i = 0; i<nships-1; i++) {
953 src.w = surf_life->w;
954 src.h = surf_life->h;
957 dest.x = (i + 1)*(src.w + 10);
959 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
965 n = SDL_GetTicks()-initticks;
967 ticks_since_last = n-score;
971 ticks_since_last = SDL_GetTicks()-last_ticks;
972 last_ticks = SDL_GetTicks();
973 if(ticks_since_last>200 || ticks_since_last<0) {
977 movementrate = ticks_since_last/50.0;
978 if(state == GAMEPLAY) {
979 score += ticks_since_last;
983 // Update the surface
984 SDL_Flip(surf_screen);
996 // Count down the game loop timer, and change state when it gets to zero or less;
998 if((state_timeout -= movementrate*3) < 0) {
1001 // Create a new ship and start all over again
1007 state = HIGH_SCORE_ENTRY;
1010 state_timeout = 5.0e6;
1012 if(score >= high[7].score) {
1013 // Read the high score table from the storage file
1014 read_high_score_table();
1016 // Find ranking of this score, store as scorerank
1017 for(i = 0; i<8; i++) {
1018 if(high[i].score <= score) {
1024 // Move all lower scores down a notch
1025 for(i = 7; i >= scorerank; i--)
1026 high[i] = high[i-1];
1028 // Insert blank high score
1029 high[scorerank].score = score;
1030 high[scorerank].name = "";
1031 high[scorerank].allocated = 0;
1035 case HIGH_SCORE_DISPLAY:
1037 state_timeout = 500.0;
1039 case HIGH_SCORE_ENTRY:
1040 // state = TITLE_PAGE;
1042 // state_timeout = 100.0;
1045 state = HIGH_SCORE_DISPLAY;
1046 state_timeout = 200.0;
1050 if(state == DEAD_PAUSE) {
1051 float blast_radius = BLAST_RADIUS * state_timeout / 20.0;
1052 if(xship < 60) xship = 60;
1053 for(i = 0; i<MAXROCKS; i++ ) {
1055 if(rock[i].x <= 0) continue;
1056 dx = rock[i].x - xship;
1057 dy = rock[i].y - yship;
1058 n = sqrt(dx*dx + dy*dy);
1059 if(n < blast_radius) {
1061 rock[i].xvel += rockrate*(dx+30)/n;
1062 rock[i].yvel += rockrate*dy/n;
1069 if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
1072 if(rockptr-rock >= MAXROCKS) {
1075 if(!rockptr->active) {
1076 rockptr->x = (float)XSIZE;
1077 rockptr->xvel = -(rockspeed)*(1 + rnd());
1078 rockptr->yvel = rnd()-0.5;
1079 rockptr->type_number = random() % NROCKS;
1081 rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
1082 rockptr->active = 1;
1083 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
1085 if(movementrate>0.1) {
1086 countdown = (int)(ROCKRATE/movementrate);
1094 xvel *= pow((double)0.9,(double)movementrate);
1095 yvel *= pow((double)0.9,(double)movementrate);
1096 // if(abs(xvel)<0.00001) xvel = 0;
1097 // if(abs(yvel)<0.00001) yvel = 0;
1101 xship += xvel*movementrate;
1102 yship += yvel*movementrate;
1105 yscroll = yship - (YSIZE / 2);
1106 yscroll += yvel * 25;
1108 yscroll = ((scrollvel * (12 - movementrate)) + (yscroll * movementrate)) / 12;
1109 scrollvel = yscroll;
1110 yscroll = yscroll*movementrate;
1113 // Move all the rocks
1114 for(i = 0; i < MAXROCKS; i++) {
1115 if(rock[i].active) {
1116 rock[i].x += rock[i].xvel*movementrate;
1117 rock[i].y += rock[i].yvel*movementrate + yscroll;
1118 if(rock[i].y > YSIZE || rock[i].y < -rock[i].image->h) {
1124 rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y;
1125 rock[i].y += (rock[i].yvel*movementrate + yscroll) * 1.01;
1128 if(rock[i].x < -rock[i].image->w || rock[i].x > XSIZE) {
1137 if(xship<0 || xship>XSIZE-surf_ship->w) {
1138 // BOUNCE from left and right wall
1139 xship -= xvel*movementrate;
1144 if(yship<0 || yship>YSIZE-surf_ship->h) {
1145 // BOUNCE from top and bottom wall
1151 if(draw() && state == GAMEPLAY) {
1152 // Play the explosion sound
1154 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1158 state_timeout = 200.0;
1160 faderate = movementrate;
1164 state_timeout = 20.0;
1171 keystate = SDL_GetKeyState(NULL);
1173 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1177 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1179 for(i = 0; i<MAXROCKS; i++ ) {
1204 keystate = SDL_GetKeyState(NULL);
1207 if(state == GAMEPLAY) {
1211 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver |= 1<<3;}
1212 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver |= 1<<1;}
1213 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver |= 1<<2;}
1214 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver |= 1;}
1215 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1218 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1223 src.w = surf_b_variations->w;
1224 src.h = surf_b_variations->h;
1227 dest.x = (XSIZE-src.w)/2;
1228 dest.y = (YSIZE-src.h)/2;
1229 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1230 // Update the surface
1231 SDL_Flip(surf_screen);
1244 } else if(state == GAME_OVER) {
1245 if(keystate[SDLK_SPACE]) {
1251 main(int argc, char **argv) {
1252 int i, x, fullscreen;
1260 while ((x = getopt(argc,argv,"efhmps")) >= 0) {
1265 case 'f': // fullscreen
1269 printf("Variations on RockDodger\n"
1270 " -e big tail [E]ngine\n"
1271 " -f [F]ull screen\n"
1272 " -h this [H]elp message\n"
1273 " -m enable [M]usic\n"
1274 " -p original [P]hysics (friction)\n"
1275 " -s [S]ilent (no sound)\n");
1280 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);