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
61 struct black_point_struct {
65 // Bang dots have the same colour as shield dots.
66 // Bang dots get darker as they age.
67 // Some are coloured the same as the ex-ship.
69 Uint16 c; // when zero, use heatcolor[bangdotlife]
70 float life; // When reduced to 0, set active = 0
72 float decay;// Amount by which to reduce life each time dot is drawn
75 // Engine dots stream out the back of the ship, getting darker as they go.
78 // The life of an engine dot
79 // is a number starting at between 0 and 50 and counting backward.
80 float life; // When reduced to 0, set active = 0
83 // Space dots are harmless background items
84 // All are active. When one falls off the edge, another is created at the start.
88 // High score table {{{
105 // ************************************* VARS
106 // SDL_Surface global variables {{{
108 *surf_screen, // Screen
109 *surf_b_variations, // "variations" banner
110 *surf_b_on, // "on" banner
111 *surf_b_rockdodger, // "rockdodger" banner
112 *surf_b_game, // Title element "game"
113 *surf_b_over, // Title element "over"
114 *surf_ship, // Spaceship element
115 *surf_life, // Indicator of number of ships remaining
116 *surf_rock[NROCKS], // THE ROCKS
117 *surf_deadrock[NROCKS], // THE DEAD ROCKS
118 *surf_font_big; // The big font
120 // Structure global variables {{{
121 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
122 struct rock_struct rock[MAXROCKS], *rockptr = rock;
123 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
124 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
125 struct spacedot sdot[MAXSPACEDOTS];
127 // Other global variables {{{
129 char *initerror = "";
130 char name[1024], debug1[1024];
132 float xship,yship = 240.0; // X position, 0..XSIZE
133 float xvel,yvel; // Change in X position per tick.
134 float rockrate,rockspeed;
139 int nships,score,initticks,ticks_since_last, last_ticks;
143 int oss_sound_flag = 0;
144 int tail_plume = 0; // display big engine at the back?
145 int friction = 0; // should there be friction?
147 float fadetimer = 0,faderate;
149 int pausedown = 0,paused = 0;
151 // bangdot start (bd1) and end (bd2) position:
152 int bd1 = 0, bd2 = 0;
154 int xoffset[NROCKS][MAXROCKHEIGHT];
165 enum states state = TITLE_PAGE;
166 float state_timeout = 600.0;
168 const int fakesin[] = {0,1,0,-1};
169 const int fakecos[] = {1,0,-1,0};
172 "Press SPACE to start",
173 "http://qualdan.com/vor/"
176 int bangdotlife, nbangdots;
177 Uint16 heatcolor[W*3];
181 extern int optind, opterr, optopt;
184 float dist_sq(float x1, float y1, float x2, float y2)
186 return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
189 // ************************************* FUNCS
191 FILE *hs_fopen(char *mode) {
195 if(f = fopen("/usr/share/vor/.highscore",mode)) {
202 sprintf(s,"%s/.vor-high",getenv("HOME"));
203 if(f = fopen(s,mode)) {
213 void read_high_score_table() {
216 if(f = hs_fopen("r")) {
217 // If the file exists, read from it
218 for(i = 0; i<8; i++) {
221 if(fscanf (f, "%d %[^\n]", &highscore, s) != 2) {
224 if(high[i].allocated) {
227 high[i].name = strdup(s);
228 high[i].score = highscore;
229 high[i].allocated = 1;
234 void write_high_score_table() {
237 if(f = hs_fopen("w")) {
238 // If the file exists, write to it
239 for(i = 0; i<8; i++) {
240 fprintf (f, "%d %s\n", high[i].score, high[i].name);
245 void snprintscore(char *s, size_t n, int score) {
246 int min = score/60000;
247 int sec = score/1000%60;
248 int tenths = score%1000/100;
250 snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
252 snprintf(s, n, " %2d.%d", sec, tenths);
256 return (float)random()/(float)RAND_MAX;
258 void init_engine_dots() {
260 for(i = 0; i<MAXENGINEDOTS; i++) {
264 void init_space_dots() {
266 for(i = 0; i<MAXSPACEDOTS; i++) {
269 sdot[i].x = rnd()*(XSIZE-5);
270 sdot[i].y = rnd()*(YSIZE-5);
276 intensity = (int)(r*180 + 70);
277 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
282 int makebangdots(int xbang, int ybang, int xvel, int yvel, SDL_Surface *s, int power) {
284 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
285 // TODO - generate and display dots in a circular buffer
287 int i,x,y,n,endcount;
289 double theta,r,dx,dy;
292 begin_generate = SDL_GetTicks();
295 rawpixel = (Uint16 *) s->pixels;
297 //for(n = 0; n <= power/2; n++) {
302 for(x = 0; x<s->w; x++) {
303 for(y = 0; y<s->h; y++) {
304 c = rawpixel[s->pitch/2*y + x];
305 if(c && c != SDL_MapRGB(s->format,0,255,0)) {
307 theta = rnd()*M_PI*2;
311 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + xvel;
312 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + yvel;
313 bdot[bd2].x = x + xbang;
314 bdot[bd2].y = y + ybang;
316 // Replace the last few bang dots with the pixels from the exploding object
317 bdot[bd2].c = (endcount>0)?c:0;
318 bdot[bd2].life = 100;
319 bdot[bd2].decay = rnd()*3 + 1;
320 bdot[bd2].active = 1;
325 // If the circular buffer is filled, who cares? They've had their chance.
326 //if(bd2 == bd1-1) goto exitloop;
332 if(SDL_GetTicks() - begin_generate > 7) endcount++;
337 SDL_UnlockSurface(s);
341 void draw_bang_dots(SDL_Surface *s) {
345 rawpixel = (Uint16 *) s->pixels;
349 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
353 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
354 // If the dot has drifted outside the perimeter, kill it
362 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)];
363 bdot[i].life -= bdot[i].decay;
364 bdot[i].x += bdot[i].dx*movementrate;
365 bdot[i].y += bdot[i].dy*movementrate + yscroll;
384 void draw_space_dots(SDL_Surface *s) {
387 rawpixel = (Uint16 *) s->pixels;
389 for(i = 0; i<MAXSPACEDOTS; i++) {
393 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
394 sdot[i].x += sdot[i].dx*movementrate;
395 sdot[i].y += yscroll;
396 if(sdot[i].y > YSIZE) {
398 } else if(sdot[i].y < 0) {
407 void draw_engine_dots(SDL_Surface *s) {
410 rawpixel = (Uint16 *) s->pixels;
412 for(i = 0; i<MAXENGINEDOTS; i++) {
414 edot[i].x += edot[i].dx*movementrate;
415 edot[i].y += edot[i].dy*movementrate + yscroll;
416 if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
418 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
422 heatindex = edot[i].life * 6;
423 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
424 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
430 void create_engine_dots(int newdots) {
432 double theta,r,dx,dy;
434 if(!tail_plume) return;
436 if(state == GAMEPLAY) {
437 for(i = 0; i<newdots*movementrate; i++) {
438 if(dotptr->active == 0) {
439 theta = rnd()*M_PI*2;
445 dotptr->x = xship + surf_ship->w/2-14;
446 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*5-1;
447 dotptr->dx = 10*(dx-1.5) + xvel;
448 dotptr->dy = 1*dy + yvel;
449 dotptr->life = 45 + rnd(1)*5;
452 if(dotptr-edot >= MAXENGINEDOTS) {
460 void create_engine_dots2(int newdots, int m) {
462 double theta, theta2, dx, dy, adx, ady;
464 // Don't create fresh engine dots when
465 // the game is not being played and a demo is not beng shown
466 if(state != GAMEPLAY && state != DEMO) return;
468 for(i = 0; i<newdots; i++) {
469 if(dotptr->active == 0) {
470 theta = rnd()*M_PI*2;
471 theta2 = rnd()*M_PI*2;
473 dx = cos(theta) * fabs(cos(theta2));
474 dy = sin(theta) * fabs(cos(theta2));
480 dotptr->x = xship + surf_ship->w/2 + (rnd()-0.5)*3;
481 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*3;
486 dotptr->dx = -20*adx + xvel;
487 dotptr->dy = 2*dy + yvel;
488 dotptr->life = 60 * adx;
491 dotptr->dx = 2*dx + xvel;
492 dotptr->dy = -20*ady + yvel;
493 dotptr->life = 60 * ady;
497 dotptr->dx = 20*adx + xvel;
498 dotptr->dy = 2*dy + yvel;
499 dotptr->life = 60 * adx;
502 dotptr->dx = 2*dx + xvel;
503 dotptr->dy = 20*ady + yvel;
504 dotptr->life = 60 * ady;
508 if(dotptr-edot >= MAXENGINEDOTS) {
515 int drawdots(SDL_Surface *s) {
519 // Draw the background stars aka space dots
522 // Draw the score when playing the game or whn the game is freshly over
523 if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
524 SDL_UnlockSurface(s);
526 scorepos = XSIZE-250;
527 n = snprintf(topline, 50, "Time: ");
528 snprintscore(topline + n, 50-n, score);
529 PutString(s,scorepos,0,topline);
534 // Draw all the engine dots
537 // Create more engine dots comin out da back
539 create_engine_dots(200);
541 // Create engine dots out the side we're moving from
542 for(m = 0; m<4; m++) {
543 if(maneuver & 1<<m) { // 'maneuver' is a bit field
544 create_engine_dots2(80,m);
548 // Draw all outstanding bang dots
549 //if(bangdotlife-- > 0)
552 SDL_UnlockSurface(s);
555 char * load_file(char *s) {
556 static char retval[1024];
557 snprintf(retval, 1024, "%s/%s", data_dir, s);
562 int missing(char *dirname) {
565 return (!S_ISDIR(buf.st_mode));
568 int init(int fullscreen) {
575 read_high_score_table();
577 // Where are our data files?
579 // second alternative: RD_DATADIR
580 // final alternative: /usr/share/vor
581 data_dir = strdup("./data");
582 if(missing(data_dir)) {
584 env = getenv("RD_DATADIR");
586 data_dir = strdup(env);
587 if(missing(data_dir)) {
588 fprintf (stderr,"Cannot find data directory $RD_DATADIR\n");
592 data_dir = strdup("/usr/share/vor");
593 if(missing(data_dir)) {
594 fprintf (stderr,"Cannot find data in %s\n", data_dir);
602 // Initialise SDL with audio and video
603 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
605 printf ("Can't open sound, starting without it\n");
609 atexit(SDL_CloseAudio);
610 oss_sound_flag = init_sound();
614 // Initialise with video only
615 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
622 // Attempt to get the required video size
623 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
624 if(fullscreen) flag |= SDL_FULLSCREEN;
625 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
627 // Set the title bar text
628 SDL_WM_SetCaption("Rock Dodgers", "rockdodgers");
630 NULLERROR(surf_screen);
632 // Set the heat color from the range 0 (cold) to 300 (blue-white)
633 for(i = 0; i<W*3; i++) {
634 heatcolor[i] = SDL_MapRGB(
636 (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?
641 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
642 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
644 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
645 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
647 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
648 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
650 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
651 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
653 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
654 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
656 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
657 InitFont(surf_font_big);
659 // Load the spaceship graphic.
660 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
661 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
663 // Load the life indicator (small ship) graphic.
664 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
665 NULLERROR(surf_life = SDL_DisplayFormat(temp));
667 // Create the array of black points;
668 SDL_LockSurface(surf_ship);
669 raw_pixels = (Uint16 *) surf_ship->pixels;
670 for(i = 0; i<surf_ship->w; i++) {
671 for(j = 0; j<surf_ship->h; j++) {
672 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
680 SDL_UnlockSurface(surf_ship);
685 // Load all our lovely rocks
686 for(i = 0; i<NROCKS; i++) {
689 sprintf(a,load_file("sprites/rock%d.png"),i);
690 NULLERROR(temp = IMG_Load(a));
691 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
693 sprintf(a,load_file("sprites/deadrock%d.png"),i);
694 NULLERROR(temp = IMG_Load(a));
695 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
698 // Remove the mouse cursor
700 SDL_ShowCursor(SDL_DISABLE);
708 struct black_point_struct *p;
712 float fadegame,fadeover;
714 char *statedisplay, buf[1024];
723 // Draw a fully black background
724 SDL_FillRect(surf_screen,NULL,0);
729 // Show the current state
732 statedisplay = "title_page";
735 statedisplay = "gameplay";
738 statedisplay = "dead_pause";
741 statedisplay = "game_over";
743 case HIGH_SCORE_ENTRY:
744 statedisplay = "high_score_entry";
746 case HIGH_SCORE_DISPLAY:
747 statedisplay = "high_score_display";
750 statedisplay = "demo";
753 snprintf(buf,1024, "mode = %s", statedisplay);
754 PutString(surf_screen,0,YSIZE-50,buf);
758 // Draw the background dots
759 drawdots(surf_screen);
762 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
763 src.w = surf_ship->w;
764 src.h = surf_ship->h;
769 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
772 // Draw all the rocks, in all states
773 for(i = 0; i<MAXROCKS; i++) {
776 src.w = rock[i].image->w;
777 src.h = rock[i].image->h;
780 dest.x = (int) rock[i].x;
781 dest.y = (int) rock[i].y;
784 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
786 // Draw the heated part of the rock, in an alpha which reflects the
787 // amount of heat in the rock.
789 SDL_Surface *deadrock;
790 deadrock = surf_deadrock[rock[i].type_number];
791 SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
792 dest.x = (int) rock[i].x; // kludge
793 SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
795 rock[i].heat -= movementrate;
799 // If the rock is heated past a certain point, the water content of
800 // the rock flashes to steam, releasing enough energy to destroy
801 // the rock in spectacular fashion.
802 if(rock[i].heat>rock[i].image->h) {
804 play_sound(1 + (int)(rnd()*3));
805 makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
811 // If it's game over, show the game over graphic in the dead centre
814 if(fadetimer<3.0/faderate) {
815 fadegame = fadetimer/(3.0/faderate);
820 if(fadetimer<3.0/faderate) {
822 } else if(fadetimer<6.0/faderate) {
823 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
828 src.w = surf_b_game->w;
829 src.h = surf_b_game->h;
832 dest.x = (XSIZE-src.w)/2;
833 dest.y = (YSIZE-src.h)/2-40;
834 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
835 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
837 src.w = surf_b_over->w;
838 src.h = surf_b_over->h;
841 dest.x = (XSIZE-src.w)/2;
842 dest.y = (YSIZE-src.h)/2 + 40;
843 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
844 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
849 src.w = surf_b_variations->w;
850 src.h = surf_b_variations->h;
853 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
854 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
855 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
856 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
858 src.w = surf_b_on->w;
859 src.h = surf_b_on->h;
862 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
863 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
864 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
865 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
867 src.w = surf_b_rockdodger->w;
868 src.h = surf_b_rockdodger->h;
871 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
872 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
873 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
874 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
876 text = "Version " VERSION;
877 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
878 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
880 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
881 //text = "Press SPACE to start!";
882 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
883 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
886 case HIGH_SCORE_ENTRY:
888 if(score >= high[7].score) {
890 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
891 // Insert name into high score table
893 // Lose the lowest name forever (loser!)
894 //if(high[7].allocated)
895 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
897 // Insert new high score
898 high[scorerank].score = score;
899 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
900 high[scorerank].allocated = 1;
902 // Set the global name string to "", ready for the next winner
905 // Change state to briefly show high scores page
906 state = HIGH_SCORE_DISPLAY;
909 // Write the high score table to the file
910 write_high_score_table();
912 // Play the title page tune
916 state = HIGH_SCORE_DISPLAY;
921 case HIGH_SCORE_DISPLAY:
922 // Display de list o high scores mon.
923 PutString(surf_screen,180,50,"High scores");
924 for(i = 0; i<8; i++) {
926 sprintf(s, "#%1d",i + 1);
927 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
928 snprintscore(s, 1024, high[i].score);
929 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
930 sprintf(s, "%3s", high[i].name);
931 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
936 if(!gameover && state == GAMEPLAY) {
937 SDL_LockSurface(surf_screen);
938 raw_pixels = (Uint16 *) surf_screen->pixels;
939 // Check that the black points on the ship are
940 // still black, and not covered up by rocks.
941 for(p = black_point; p<blackptr; p++) {
942 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
943 if(raw_pixels[offset]) {
948 SDL_UnlockSurface(surf_screen);
951 // Draw all the little ships
952 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
953 for(i = 0; i<nships-1; i++) {
954 src.w = surf_life->w;
955 src.h = surf_life->h;
958 dest.x = (i + 1)*(src.w + 10);
960 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
966 n = SDL_GetTicks()-initticks;
968 ticks_since_last = n-score;
972 ticks_since_last = SDL_GetTicks()-last_ticks;
973 last_ticks = SDL_GetTicks();
974 if(ticks_since_last>200 || ticks_since_last<0) {
978 movementrate = ticks_since_last/50.0;
979 if(state == GAMEPLAY) {
980 score += ticks_since_last;
984 // Update the surface
985 SDL_Flip(surf_screen);
997 // Count down the game loop timer, and change state when it gets to zero or less;
999 if((state_timeout -= movementrate*3) < 0) {
1002 // Create a new ship and start all over again
1011 state = HIGH_SCORE_ENTRY;
1014 state_timeout = 5.0e6;
1016 if(score >= high[7].score) {
1017 // Read the high score table from the storage file
1018 read_high_score_table();
1020 // Find ranking of this score, store as scorerank
1021 for(i = 0; i<8; i++) {
1022 if(high[i].score <= score) {
1028 // Move all lower scores down a notch
1029 for(i = 7; i >= scorerank; i--)
1030 high[i] = high[i-1];
1032 // Insert blank high score
1033 high[scorerank].score = score;
1034 high[scorerank].name = "";
1035 high[scorerank].allocated = 0;
1039 case HIGH_SCORE_DISPLAY:
1041 state_timeout = 500.0;
1043 case HIGH_SCORE_ENTRY:
1044 // state = TITLE_PAGE;
1046 // state_timeout = 100.0;
1049 state = HIGH_SCORE_DISPLAY;
1050 state_timeout = 200.0;
1054 if(state == DEAD_PAUSE) {
1055 float blast_radius = START_RAD * state_timeout / 50.0;
1056 for(i = 0; i<MAXROCKS; i++ ) {
1058 if(rock[i].x <= 0) continue;
1059 dx = rock[i].x + 70;
1060 dy = rock[i].y - YSIZE/2;
1061 n = sqrt(dx*dx + dy*dy);
1062 if(n < blast_radius+70) {
1064 rock[i].xvel += rockrate*(dx+30)/n;
1065 rock[i].yvel += rockrate*dy/n;
1066 if(rock[i].xvel <= 0) rock[i].xvel = 1;
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++) if(rock[i].active) {
1118 rock[i].x += rock[i].xvel*movementrate;
1119 rock[i].y += rock[i].yvel*movementrate + yscroll;
1120 if(rock[i].y > YSIZE) {
1122 rock[i].y -= rock[i].image->w;
1123 } else if(rock[i].y < -rock[i].image->w) {
1125 rock[i].y += rock[i].image->w;
1127 if(rock[i].x < -32.0) rock[i].active = 0;
1128 if(rock[i].xvel > 0) {
1129 if(rock[i].y < 0 || rock[i].y > YSIZE) rock[i].active = 0;
1130 if(rock[i].x > XSIZE) rock[i].active = 0;
1136 if(xship<0 || xship>XSIZE-surf_ship->w) {
1137 // BOUNCE from left and right wall
1138 xship -= xvel*movementrate;
1143 if(yship<0 || yship>YSIZE-surf_ship->h) {
1144 // BOUNCE from top and bottom wall
1150 if(draw() && state == GAMEPLAY) {
1151 if(oss_sound_flag) {
1152 // Play the explosion sound
1155 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1159 state_timeout = 200.0;
1161 faderate = movementrate;
1165 state_timeout = 50.0;
1170 keystate = SDL_GetKeyState(NULL);
1172 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1176 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1178 for(i = 0; i<MAXROCKS; i++ ) {
1202 keystate = SDL_GetKeyState(NULL);
1205 if(state == GAMEPLAY) {
1209 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver |= 1<<3;}
1210 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver |= 1<<1;}
1211 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver |= 1<<2;}
1212 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver |= 1;}
1213 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1216 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1221 src.w = surf_b_variations->w;
1222 src.h = surf_b_variations->h;
1225 dest.x = (XSIZE-src.w)/2;
1226 dest.y = (YSIZE-src.h)/2;
1227 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1228 // Update the surface
1229 SDL_Flip(surf_screen);
1242 } else if(state == GAME_OVER) {
1243 if(keystate[SDLK_SPACE]) {
1249 main(int argc, char **argv) {
1250 int i, x, fullscreen;
1257 while ((x = getopt(argc,argv,"efhsp")) >= 0) {
1262 case 'f': // fullscreen
1266 printf("Variations on RockDodger\n"
1267 " -e big tail [E]ngine\n"
1268 " -f [F]ull screen\n"
1269 " -h this [H]elp message\n"
1270 " -p original [P]hysics (friction)\n"
1271 " -s [S]ilent (no sound)\n");
1274 case 'p': // physics
1283 if(init(fullscreen)) {
1284 printf ("ta: '%s'\n",initerror);
1289 for(i = 0; i<MAXROCKS; i++) {
1294 initticks = SDL_GetTicks();
1295 if(gameloop() == 0) {
1298 printf ("score = %d\n",score);