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;
33 #include <SDL/SDL_image.h>
42 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
43 #define NULLERROR(a) CONDERROR((a) == NULL)
45 // ************************************* STRUCTS
47 // Array of black pixel coordinates. This is scanned
48 // every frame to see if it's still black, and as
49 // soon as it isn't we BLOW UP
52 int dead; // has been blown out of the way
53 // to make room for a new ship appearing.
57 struct black_point_struct {
61 // Bang dots have the same colour as shield dots.
62 // Bang dots get darker as they age.
63 // Some are coloured the same as the ex-ship.
65 Uint16 c; // when zero, use heatcolor[bangdotlife]
66 float life; // When reduced to 0, set active = 0
68 float decay;// Amount by which to reduce life each time dot is drawn
71 // Engine dots stream out the back of the ship, getting darker as they go.
74 // The life of an engine dot
75 // is a number starting at between 0 and 50 and counting backward.
76 float life; // When reduced to 0, set active = 0
79 // Space dots are harmless background items
80 // All are active. When one falls off the edge, another is created at the start.
100 // ************************************* VARS
101 // SDL_Surface global variables
103 *surf_screen, // Screen
104 *surf_b_variations, // "variations" banner
105 *surf_b_on, // "on" banner
106 *surf_b_rockdodger, // "rockdodger" banner
107 *surf_b_game, // Title element "game"
108 *surf_b_over, // Title element "over"
109 *surf_ship, // Spaceship element
110 *surf_life, // Indicator of number of ships remaining
111 *surf_rock[NROCKS], // THE ROCKS
112 *surf_deadrock[NROCKS], // THE DEAD ROCKS
113 *surf_font_big; // The big font
115 // Structure global variables
116 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
117 struct rock_struct rock[MAXROCKS], *rockptr = rock;
118 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
119 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
120 struct spacedot sdot[MAXSPACEDOTS];
122 // Other global variables
124 char *initerror = "";
125 char name[1024], debug1[1024];
127 float xship,yship = 240.0; // X position, 0..XSIZE
128 float xvel,yvel; // Change in X position per tick.
129 float rockrate,rockspeed;
134 int nships,score,initticks,ticks_since_last, last_ticks;
138 int sound_flag = 1, music_flag = 0;
139 int tail_plume = 0; // display big engine at the back?
140 int friction = 0; // should there be friction?
142 float fadetimer = 0,faderate;
144 int pausedown = 0,paused = 0;
146 // bangdot start (bd1) and end (bd2) position:
147 int bd1 = 0, bd2 = 0;
149 int xoffset[NROCKS][MAXROCKHEIGHT];
160 enum states state = TITLE_PAGE;
161 float state_timeout = 600.0;
163 const int fakesin[] = {0,1,0,-1};
164 const int fakecos[] = {1,0,-1,0};
167 "Press SPACE to start",
168 "http://qualdan.com/vor/"
171 int bangdotlife, nbangdots;
172 Uint16 heatcolor[W*3];
176 extern int optind, opterr, optopt;
178 float dist_sq(float x1, float y1, float x2, float y2)
180 return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
183 // ************************************* FUNCS
186 read_high_score_table() {
190 f = open_score_file("r");
192 // If the file exists, read from it
193 for(i = 0; i<8; i++) {
196 if(fscanf (f, "%d %[^\n]", &highscore, s) != 2) {
199 if(high[i].allocated) {
202 high[i].name = strdup(s);
203 high[i].score = highscore;
204 high[i].allocated = 1;
211 write_high_score_table() {
215 f = open_score_file("w");
217 // If the file exists, write to it
218 for(i = 0; i<8; i++) {
219 fprintf (f, "%d %s\n", high[i].score, high[i].name);
226 snprintscore(char *s, size_t n, int score) {
227 int min = score/60000;
228 int sec = score/1000%60;
229 int tenths = score%1000/100;
231 snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
233 snprintf(s, n, " %2d.%d", sec, tenths);
239 return (float)random()/(float)RAND_MAX;
245 for(i = 0; i<MAXENGINEDOTS; i++) {
253 for(i = 0; i<MAXSPACEDOTS; i++) {
256 sdot[i].x = rnd()*(XSIZE-5);
257 sdot[i].y = rnd()*(YSIZE-5);
263 intensity = (int)(r*180 + 70);
264 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
270 makebangdots(int xbang, int ybang, int xvel, int yvel, SDL_Surface *s, int power) {
272 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
273 // TODO - generate and display dots in a circular buffer
280 begin_generate = SDL_GetTicks();
283 rawpixel = (Uint16 *) s->pixels;
285 //for(n = 0; n <= power/2; n++) {
289 for(x = 0; x<s->w; x++) {
290 for(y = 0; y<s->h; y++) {
291 c = rawpixel[s->pitch/2*y + x];
292 if(c && c != SDL_MapRGB(s->format,0,255,0)) {
294 theta = rnd()*M_PI*2;
298 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + xvel;
299 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + yvel;
300 bdot[bd2].x = x + xbang;
301 bdot[bd2].y = y + ybang;
303 // Replace the last few bang dots with the pixels from the exploding object
304 bdot[bd2].c = (endcount>0)?c:0;
305 bdot[bd2].life = 100;
306 bdot[bd2].decay = rnd()*3 + 1;
307 bdot[bd2].active = 1;
312 // If the circular buffer is filled, who cares? They've had their chance.
313 //if(bd2 == bd1-1) goto exitloop;
319 if(SDL_GetTicks() - begin_generate > 7) endcount++;
322 SDL_UnlockSurface(s);
327 draw_bang_dots(SDL_Surface *s) {
331 rawpixel = (Uint16 *) s->pixels;
335 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
339 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
340 // If the dot has drifted outside the perimeter, kill it
348 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)];
349 bdot[i].life -= bdot[i].decay;
350 bdot[i].x += bdot[i].dx*movementrate;
351 bdot[i].y += bdot[i].dy*movementrate + yscroll;
371 draw_space_dots(SDL_Surface *s) {
374 rawpixel = (Uint16 *) s->pixels;
376 for(i = 0; i<MAXSPACEDOTS; i++) {
380 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
381 sdot[i].x += sdot[i].dx*movementrate;
382 sdot[i].y += yscroll;
383 if(sdot[i].y > YSIZE) {
385 } else if(sdot[i].y < 0) {
395 draw_engine_dots(SDL_Surface *s) {
398 rawpixel = (Uint16 *) s->pixels;
400 for(i = 0; i<MAXENGINEDOTS; i++) {
402 edot[i].x += edot[i].dx*movementrate;
403 edot[i].y += edot[i].dy*movementrate + yscroll;
404 if((edot[i].life -= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
406 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
410 heatindex = edot[i].life * 6;
411 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
412 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
419 create_engine_dots(int newdots) {
421 double theta,r,dx,dy;
423 if(!tail_plume) return;
425 if(state == GAMEPLAY) {
426 for(i = 0; i<newdots*movementrate; i++) {
427 if(dotptr->active == 0) {
428 theta = rnd()*M_PI*2;
434 dotptr->x = xship + surf_ship->w/2-14;
435 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*5-1;
436 dotptr->dx = 10*(dx-1.5) + xvel;
437 dotptr->dy = 1*dy + yvel;
438 dotptr->life = 45 + rnd(1)*5;
441 if(dotptr-edot >= MAXENGINEDOTS) {
450 create_engine_dots2(int newdots, int m) {
452 double theta, theta2, dx, dy, adx, ady;
454 // Don't create fresh engine dots when
455 // the game is not being played and a demo is not beng shown
456 if(state != GAMEPLAY && state != DEMO) return;
458 for(i = 0; i<newdots; i++) {
459 if(dotptr->active == 0) {
460 theta = rnd()*M_PI*2;
461 theta2 = rnd()*M_PI*2;
463 dx = cos(theta) * fabs(cos(theta2));
464 dy = sin(theta) * fabs(cos(theta2));
470 dotptr->x = xship + surf_ship->w/2 + (rnd()-0.5)*3;
471 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*3;
476 dotptr->dx = -20*adx + xvel;
477 dotptr->dy = 2*dy + yvel;
478 dotptr->life = 60 * adx;
481 dotptr->dx = 2*dx + xvel;
482 dotptr->dy = -20*ady + yvel;
483 dotptr->life = 60 * ady;
487 dotptr->dx = 20*adx + xvel;
488 dotptr->dy = 2*dy + yvel;
489 dotptr->life = 60 * adx;
492 dotptr->dx = 2*dx + xvel;
493 dotptr->dy = 20*ady + yvel;
494 dotptr->life = 60 * ady;
498 if(dotptr-edot >= MAXENGINEDOTS) {
506 drawdots(SDL_Surface *s) {
510 // Draw the background stars aka space dots
513 // Draw the score when playing the game or whn the game is freshly over
514 if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
515 SDL_UnlockSurface(s);
517 scorepos = XSIZE-250;
518 n = snprintf(topline, 50, "Time: ");
519 snprintscore(topline + n, 50-n, score);
520 PutString(s,scorepos,0,topline);
525 // Draw all the engine dots
528 // Create more engine dots comin out da back
530 create_engine_dots(200);
532 // Create engine dots out the side we're moving from
533 for(m = 0; m<4; m++) {
534 if(maneuver & 1<<m) { // 'maneuver' is a bit field
535 create_engine_dots2(80,m);
539 // Draw all outstanding bang dots
540 //if(bangdotlife-- > 0)
543 SDL_UnlockSurface(s);
547 init(int fullscreen) {
554 // Where are our data files?
555 if(!find_files()) exit(1);
556 read_high_score_table();
559 // Initialize SDL with audio and video
560 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
562 printf ("Can't open sound, starting without it\n");
566 atexit(SDL_CloseAudio);
567 sound_flag = init_sound();
570 // Initialize with video only
571 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
577 // Attempt to get the required video size
578 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
579 if(fullscreen) flag |= SDL_FULLSCREEN;
580 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
582 // Set the title bar text
583 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
585 NULLERROR(surf_screen);
587 // Set the heat color from the range 0 (cold) to 300 (blue-white)
588 for(i = 0; i<W*3; i++) {
589 heatcolor[i] = SDL_MapRGB(
591 (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?
596 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
597 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
599 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
600 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
602 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
603 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
605 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
606 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
608 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
609 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
611 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
612 InitFont(surf_font_big);
614 // Load the spaceship graphic.
615 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
616 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
618 // Load the life indicator (small ship) graphic.
619 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
620 NULLERROR(surf_life = SDL_DisplayFormat(temp));
622 // Create the array of black points;
623 SDL_LockSurface(surf_ship);
624 raw_pixels = (Uint16 *) surf_ship->pixels;
625 for(i = 0; i<surf_ship->w; i++) {
626 for(j = 0; j<surf_ship->h; j++) {
627 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
635 SDL_UnlockSurface(surf_ship);
640 // Load all our lovely rocks
641 for(i = 0; i<NROCKS; i++) {
644 sprintf(a,load_file("sprites/rock%d.png"),i);
645 NULLERROR(temp = IMG_Load(a));
646 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
648 sprintf(a,load_file("sprites/deadrock%d.png"),i);
649 NULLERROR(temp = IMG_Load(a));
650 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
653 // Remove the mouse cursor
655 SDL_ShowCursor(SDL_DISABLE);
665 struct black_point_struct *p;
669 float fadegame,fadeover;
678 // Draw a fully black background
679 SDL_FillRect(surf_screen,NULL,0);
681 // Draw the background dots
682 drawdots(surf_screen);
685 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
686 src.w = surf_ship->w;
687 src.h = surf_ship->h;
692 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
695 // Draw all the rocks, in all states
696 for(i = 0; i<MAXROCKS; i++) {
699 src.w = rock[i].image->w;
700 src.h = rock[i].image->h;
703 dest.x = (int) rock[i].x;
704 dest.y = (int) rock[i].y;
707 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
712 // If it's game over, show the game over graphic in the dead centre
715 if(fadetimer<3.0/faderate) {
716 fadegame = fadetimer/(3.0/faderate);
721 if(fadetimer<3.0/faderate) {
723 } else if(fadetimer<6.0/faderate) {
724 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
729 src.w = surf_b_game->w;
730 src.h = surf_b_game->h;
733 dest.x = (XSIZE-src.w)/2;
734 dest.y = (YSIZE-src.h)/2-40;
735 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
736 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
738 src.w = surf_b_over->w;
739 src.h = surf_b_over->h;
742 dest.x = (XSIZE-src.w)/2;
743 dest.y = (YSIZE-src.h)/2 + 40;
744 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
745 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
750 src.w = surf_b_variations->w;
751 src.h = surf_b_variations->h;
754 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
755 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
756 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
757 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
759 src.w = surf_b_on->w;
760 src.h = surf_b_on->h;
763 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
764 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
765 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
766 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
768 src.w = surf_b_rockdodger->w;
769 src.h = surf_b_rockdodger->h;
772 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
773 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
774 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
775 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
777 text = "Version " VERSION;
778 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
779 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
781 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
782 //text = "Press SPACE to start!";
783 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
784 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
787 case HIGH_SCORE_ENTRY:
789 if(score >= high[7].score) {
791 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
792 // Insert name into high score table
794 // Lose the lowest name forever (loser!)
795 //if(high[7].allocated)
796 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
798 // Insert new high score
799 high[scorerank].score = score;
800 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
801 high[scorerank].allocated = 1;
803 // Set the global name string to "", ready for the next winner
806 // Change state to briefly show high scores page
807 state = HIGH_SCORE_DISPLAY;
810 // Write the high score table to the file
811 write_high_score_table();
813 // Play the title page tune
817 state = HIGH_SCORE_DISPLAY;
822 case HIGH_SCORE_DISPLAY:
823 // Display de list o high scores mon.
824 PutString(surf_screen,180,50,"High scores");
825 for(i = 0; i<8; i++) {
827 sprintf(s, "#%1d",i + 1);
828 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
829 snprintscore(s, 1024, high[i].score);
830 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
831 sprintf(s, "%3s", high[i].name);
832 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
837 if(!gameover && state == GAMEPLAY) {
838 SDL_LockSurface(surf_screen);
839 raw_pixels = (Uint16 *) surf_screen->pixels;
840 // Check that the black points on the ship are
841 // still black, and not covered up by rocks.
842 for(p = black_point; p<blackptr; p++) {
843 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
844 if(raw_pixels[offset]) {
849 SDL_UnlockSurface(surf_screen);
852 // Draw all the little ships
853 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
854 for(i = 0; i<nships-1; i++) {
855 src.w = surf_life->w;
856 src.h = surf_life->h;
859 dest.x = (i + 1)*(src.w + 10);
861 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
867 n = SDL_GetTicks()-initticks;
869 ticks_since_last = n-score;
873 ticks_since_last = SDL_GetTicks()-last_ticks;
874 last_ticks = SDL_GetTicks();
875 if(ticks_since_last>200 || ticks_since_last<0) {
879 movementrate = ticks_since_last/50.0;
880 if(state == GAMEPLAY) {
881 score += ticks_since_last;
885 // Update the surface
886 SDL_Flip(surf_screen);
900 // Count down the game loop timer, and change state when it gets to zero or less;
902 if((state_timeout -= movementrate*3) < 0) {
905 // Create a new ship and start all over again
911 state = HIGH_SCORE_ENTRY;
914 state_timeout = 5.0e6;
916 if(score >= high[7].score) {
917 // Read the high score table from the storage file
918 read_high_score_table();
920 // Find ranking of this score, store as scorerank
921 for(i = 0; i<8; i++) {
922 if(high[i].score <= score) {
928 // Move all lower scores down a notch
929 for(i = 7; i >= scorerank; i--)
932 // Insert blank high score
933 high[scorerank].score = score;
934 high[scorerank].name = "";
935 high[scorerank].allocated = 0;
939 case HIGH_SCORE_DISPLAY:
941 state_timeout = 500.0;
943 case HIGH_SCORE_ENTRY:
944 // state = TITLE_PAGE;
946 // state_timeout = 100.0;
949 state = HIGH_SCORE_DISPLAY;
950 state_timeout = 200.0;
954 if(state == DEAD_PAUSE) {
955 float blast_radius = BLAST_RADIUS * state_timeout / 20.0;
956 if(xship < 60) xship = 60;
957 for(i = 0; i<MAXROCKS; i++ ) {
959 if(rock[i].x <= 0) continue;
960 dx = rock[i].x - xship;
961 dy = rock[i].y - yship;
962 n = sqrt(dx*dx + dy*dy);
963 if(n < blast_radius) {
965 rock[i].xvel += rockrate*(dx+30)/n;
966 rock[i].yvel += rockrate*dy/n;
973 if(--countdown <= 0 && (rnd()*100.0<(rockrate += 0.025))) {
976 if(rockptr-rock >= MAXROCKS) {
979 if(!rockptr->active) {
980 rockptr->x = (float)XSIZE;
981 rockptr->xvel = -(rockspeed)*(1 + rnd());
982 rockptr->yvel = rnd()-0.5;
983 rockptr->type_number = random() % NROCKS;
984 rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
986 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
988 if(movementrate>0.1) {
989 countdown = (int)(ROCKRATE/movementrate);
997 xvel *= pow((double)0.9,(double)movementrate);
998 yvel *= pow((double)0.9,(double)movementrate);
999 // if(abs(xvel)<0.00001) xvel = 0;
1000 // if(abs(yvel)<0.00001) yvel = 0;
1004 xship += xvel*movementrate;
1005 yship += yvel*movementrate;
1008 yscroll = yship - (YSIZE / 2);
1009 yscroll += yvel * 25;
1011 yscroll = ((scrollvel * (12 - movementrate)) + (yscroll * movementrate)) / 12;
1012 scrollvel = yscroll;
1013 yscroll = yscroll*movementrate;
1016 // Move all the rocks
1017 for(i = 0; i < MAXROCKS; i++) {
1018 if(rock[i].active) {
1019 rock[i].x += rock[i].xvel*movementrate;
1020 rock[i].y += rock[i].yvel*movementrate + yscroll;
1021 if(rock[i].y > YSIZE || rock[i].y < -rock[i].image->h) {
1027 rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y;
1028 rock[i].y += (rock[i].yvel*movementrate + yscroll) * 1.01;
1031 if(rock[i].x < -rock[i].image->w || rock[i].x > XSIZE) {
1040 if(xship<0 || xship>XSIZE-surf_ship->w) {
1041 // BOUNCE from left and right wall
1042 xship -= xvel*movementrate;
1047 if(yship<0 || yship>YSIZE-surf_ship->h) {
1048 // BOUNCE from top and bottom wall
1054 if(draw() && state == GAMEPLAY) {
1055 // Play the explosion sound
1057 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1061 state_timeout = 200.0;
1063 faderate = movementrate;
1067 state_timeout = 20.0;
1074 keystate = SDL_GetKeyState(NULL);
1076 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1080 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1082 for(i = 0; i<MAXROCKS; i++ ) {
1107 keystate = SDL_GetKeyState(NULL);
1110 if(state == GAMEPLAY) {
1114 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver |= 1<<3;}
1115 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver |= 1<<1;}
1116 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver |= 1<<2;}
1117 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver |= 1;}
1118 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1121 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1126 src.w = surf_b_variations->w;
1127 src.h = surf_b_variations->h;
1130 dest.x = (XSIZE-src.w)/2;
1131 dest.y = (YSIZE-src.h)/2;
1132 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1133 // Update the surface
1134 SDL_Flip(surf_screen);
1147 } else if(state == GAME_OVER) {
1148 if(keystate[SDLK_SPACE]) {
1156 main(int argc, char **argv) {
1157 int i, x, fullscreen;
1165 while ((x = getopt(argc,argv,"efhmps")) >= 0) {
1170 case 'f': // fullscreen
1174 printf("Variations on RockDodger\n"
1175 " -e big tail [E]ngine\n"
1176 " -f [F]ull screen\n"
1177 " -h this [H]elp message\n"
1178 " -m enable [M]usic\n"
1179 " -p original [P]hysics (friction)\n"
1180 " -s [S]ilent (no sound)\n");
1185 case 'p': // physics
1195 if(init(fullscreen)) {
1196 printf ("ta: '%s'\n",initerror);
1200 for(i = 0; i<MAXROCKS; i++) {
1206 initticks = SDL_GetTicks();