1 /* Variations on RockDodger
2 * Space Rocks copyright (C) 2001 Paul Holt <pad@pcholt.com>
4 * Project fork 2004, Jason Woofenden and Joshua Grams.
5 * (a whole bunch of modifications and project rename)
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 extern int font_height;
29 #include <SDL/SDL_image.h>
36 #include <sys/types.h>
45 #define NROCKS 6 // Number of rock image files, not number of rocks visible
46 #define MAXROCKS 120 // MAX Rocks
47 #define MAXROCKHEIGHT 100
49 #define MAXBLACKPOINTS 500
50 #define MAXENGINEDOTS 5000
51 #define MAXBANGDOTS 50000
52 #define MAXSPACEDOTS 2000
55 #define BIG_FONT_FILE "fonts/score.png"
58 #define CONDERROR(a) if((a)) {initerror = strdup(SDL_GetError());return 1;}
59 #define NULLERROR(a) CONDERROR((a) == NULL)
62 // ************************************* STRUCTS
64 // Array of black pixel coordinates. This is scanned
65 // every frame to see if it's still black, and as
66 // soon as it isn't we BLOW UP
73 struct black_point_struct {
77 // Bang dots have the same colour as shield dots.
78 // Bang dots get darker as they age.
79 // Some are coloured the same as the ex-ship.
81 Uint16 c; // when zero, use heatcolor[bangdotlife]
82 float life; // When reduced to 0, set active = 0
84 float decay;// Amount by which to reduce life each time dot is drawn
87 // Engine dots stream out the back of the ship, getting darker as they go.
90 // The life of an engine dot
91 // is a number starting at between 0 and 50 and counting backward.
92 float life; // When reduced to 0, set active = 0
95 // Space dots are harmless background items
96 // All are active. When one falls off the edge, another is created at the start.
100 // High score table {{{
117 // ************************************* VARS
118 // SDL_Surface global variables {{{
120 *surf_screen, // Screen
121 *surf_b_variations, // "variations" banner
122 *surf_b_on, // "on" banner
123 *surf_b_rockdodger, // "rockdodger" banner
124 *surf_b_game, // Title element "game"
125 *surf_b_over, // Title element "over"
126 *surf_ship, // Spaceship element
127 *surf_life, // Indicator of number of ships remaining
128 *surf_rock[NROCKS], // THE ROCKS
129 *surf_deadrock[NROCKS], // THE DEAD ROCKS
130 *surf_font_big; // The big font
132 // Structure global variables {{{
133 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
134 struct rock_struct rock[MAXROCKS], *rockptr = rock;
135 struct black_point_struct black_point[MAXBLACKPOINTS], *blackptr = black_point;
136 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
137 struct spacedot sdot[MAXSPACEDOTS];
139 // Other global variables {{{
141 char *initerror = "";
142 char name[1024], debug1[1024];
144 float xship,yship = 240.0; // X position, 0..XSIZE
145 float xvel,yvel; // Change in X position per tick.
146 float rockrate,rockspeed;
148 float shieldlevel, shieldpulse = 0;
151 int nships,score,initticks,ticks_since_last, last_ticks;
152 int initialshield, gameover, fast;
157 int oss_sound_flag = 0;
158 int tail_plume = 0; // display big engine at the back?
159 int friction = 0; // should there be friction?
161 float fadetimer = 0,faderate;
163 int pausedown = 0,paused = 0;
165 // bangdot start (bd1) and end (bd2) position:
166 int bd1 = 0, bd2 = 0;
168 int xoffset[NROCKS][MAXROCKHEIGHT];
179 enum states state = TITLE_PAGE;
180 float state_timeout = 600.0;
182 const int fakesin[] = {0,1,0,-1};
183 const int fakecos[] = {1,0,-1,0};
186 "Press SPACE to start",
187 "http://qualdan.com/vor/"
190 int bangdotlife, nbangdots;
191 Uint16 heatcolor[W*3];
195 extern int optind, opterr, optopt;
198 // ************************************* FUNCS
200 int dotcollision(SDL_Surface *s) {
202 Uint16 *rawpixel, *r;
205 * Kill all the dots which collide with other objects.
206 * This does not work, it's probably in the wrong place or something.
209 rawpixel = (Uint16 *) s->pixels;
210 if(bangdotlife > 0 && bangdotlife<80) {
211 for(i = 0; i<nbangdots; i ++ ) {
212 if(bdot[i].x>0 && bdot[i].x<XSIZE && bdot[i].y>0 && bdot[i].y<YSIZE) {
213 r = &rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)];
214 if(*r != (bdot[i].c ? bdot[i].c : heatcolor[bangdotlife*2]))
219 SDL_UnlockSurface(s);
225 FILE *hs_fopen(char *mode) {
229 if(f = fopen("/usr/share/vor/.highscore",mode)) {
236 sprintf(s,"%s/.vor-high",getenv("HOME"));
237 if(f = fopen(s,mode)) {
247 void read_high_score_table() {
250 if(f = hs_fopen("r")) {
251 // If the file exists, read from it
252 for(i = 0; i<8; i ++ ) {
255 if(fscanf (f, "%d %[^\n]", &highscore, s)!= 2) {
258 if(high[i].allocated) {
261 high[i].name = strdup(s);
262 high[i].score = highscore;
263 high[i].allocated = 1;
268 void write_high_score_table() {
271 if(f = hs_fopen("w")) {
272 // If the file exists, write to it
273 for(i = 0; i<8; i ++ ) {
274 fprintf (f, "%d %s\n", high[i].score, high[i].name);
279 void snprintscore(char *s, size_t n, int score) {
280 int min = score/60000;
281 int sec = score/1000%60;
282 int tenths = score%1000/100;
284 snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
286 snprintf(s, n, " %2d.%d", sec, tenths);
290 return (float)random()/(float)RAND_MAX;
292 void init_engine_dots() {
294 for(i = 0; i<MAXENGINEDOTS; i ++ ) {
298 void init_space_dots() {
300 for(i = 0; i<MAXSPACEDOTS; i ++ ) {
303 sdot[i].x = rnd()*(XSIZE-5);
304 sdot[i].y = rnd()*(YSIZE-5);
310 intensity = (int)(r*180 + 70);
311 sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
322 // let xc = x coordinate of the collision between the laser and a space rock
323 // 1. Calculate xc and determine the asteroid that was hit
324 for(i = 0; i<MAXROCKS; i ++ ) {
326 if(yship + 12>rock[i].y && yship + 12<rock[i].y + rock[i].image->h && xship + 32<rock[i].x + (rock[i].image->w/2) && rock[i].x + (rock[i].image->w/2) < xc) {
327 xc = rock[i].x + (rock[i].image->w/2);
334 rock[hitrock].heat += movementrate*3;
337 // Plot a number of random dots between xship and XSIZE
338 SDL_LockSurface(surf_screen);
339 rawpixel = (Uint16 *) surf_screen->pixels;
340 c = SDL_MapRGB(surf_ship->format,rnd()*128,128 + rnd()*120,rnd()*128);
342 for(i = 0; i<(xc-xship)*5; i += 10) {
344 x = rnd()*(xc-(xship + 32)) + xship + 32;
345 y = yship + 12 + (rnd()-0.5)*1.5;
346 rawpixel[surf_screen->pitch/2*y + x] = c;
349 SDL_UnlockSurface(surf_screen);
353 int makebangdots(int xbang, int ybang, int xvel, int yvel, SDL_Surface *s, int power) {
355 // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
356 // TODO - generate and display dots in a circular buffer
358 int i,x,y,n,endcount;
360 double theta,r,dx,dy;
363 begin_generate = SDL_GetTicks();
366 rawpixel = (Uint16 *) s->pixels;
368 //for(n = 0; n<= power/2; n ++ ) {
373 for(x = 0; x<s->w; x ++ ) {
374 for(y = 0; y<s->h; y ++ ) {
375 c = rawpixel[s->pitch/2*y + x];
376 if(c && c != SDL_MapRGB(s->format,0,255,0)) {
378 theta = rnd()*M_PI*2;
382 bdot[bd2].dx = (power/50.0)*45.0*cos(theta)*r + xvel;
383 bdot[bd2].dy = (power/50.0)*45.0*sin(theta)*r + yvel;
384 bdot[bd2].x = x + xbang;
385 bdot[bd2].y = y + ybang;
387 // Replace the last few bang dots with the pixels from the exploding object
388 bdot[bd2].c = (endcount>0)?c:0;
389 bdot[bd2].life = 100;
390 bdot[bd2].decay = rnd()*3 + 1;
391 bdot[bd2].active = 1;
396 // If the circular buffer is filled, who cares? They've had their chance.
397 //if(bd2 == bd1-1) goto exitloop;
403 if(SDL_GetTicks() - begin_generate > 7) endcount ++ ;
408 SDL_UnlockSurface(s);
412 void draw_bang_dots(SDL_Surface *s) {
416 rawpixel = (Uint16 *) s->pixels;
420 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
424 if(bdot[i].x<= 0 || bdot[i].x>= XSIZE || bdot[i].y<= 0 || bdot[i].y>= YSIZE) {
425 // If the dot has drifted outside the perimeter, kill it
433 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)];
434 bdot[i].life-= bdot[i].decay;
435 bdot[i].x += bdot[i].dx*movementrate;
436 bdot[i].y += bdot[i].dy*movementrate + yscroll;
455 void draw_space_dots(SDL_Surface *s) {
458 rawpixel = (Uint16 *) s->pixels;
460 for(i = 0; i<MAXSPACEDOTS; i ++ ) {
464 rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color;
465 sdot[i].x += sdot[i].dx*movementrate;
466 sdot[i].y += yscroll;
467 if(sdot[i].y > YSIZE) {
469 } else if(sdot[i].y < 0) {
478 void draw_engine_dots(SDL_Surface *s) {
481 rawpixel = (Uint16 *) s->pixels;
483 for(i = 0; i<MAXENGINEDOTS; i ++ ) {
485 edot[i].x += edot[i].dx*movementrate;
486 edot[i].y += edot[i].dy*movementrate + yscroll;
487 if((edot[i].life-= movementrate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
489 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
493 heatindex = edot[i].life * 6;
494 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
495 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
501 void create_engine_dots(int newdots) {
503 double theta,r,dx,dy;
505 if(!tail_plume) return;
507 if(state == GAMEPLAY) {
508 for(i = 0; i<newdots*movementrate; i ++ ) {
509 if(dotptr->active == 0) {
510 theta = rnd()*M_PI*2;
516 dotptr->x = xship + surf_ship->w/2-14;
517 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*5-1;
518 dotptr->dx = 10*(dx-1.5) + xvel;
519 dotptr->dy = 1*dy + yvel;
520 dotptr->life = 45 + rnd(1)*5;
523 if(dotptr-edot>= MAXENGINEDOTS) {
531 void create_engine_dots2(int newdots, int m) {
533 double theta, theta2, dx, dy, adx, ady;
535 // Don't create fresh engine dots when
536 // the game is not being played and a demo is not beng shown
537 if(state!= GAMEPLAY && state!= DEMO) return;
539 for(i = 0; i<newdots; i ++ ) {
540 if(dotptr->active == 0) {
541 theta = rnd()*M_PI*2;
542 theta2 = rnd()*M_PI*2;
544 dx = cos(theta) * fabs(cos(theta2));
545 dy = sin(theta) * fabs(cos(theta2));
551 dotptr->x = xship + surf_ship->w/2 + (rnd()-0.5)*3;
552 dotptr->y = yship + surf_ship->h/2 + (rnd()-0.5)*3;
557 dotptr->dx = -20*adx + xvel;
558 dotptr->dy = 2*dy + yvel;
559 dotptr->life = 60 * adx;
562 dotptr->dx = 2*dx + xvel;
563 dotptr->dy = -20*ady + yvel;
564 dotptr->life = 60 * ady;
568 dotptr->dx = 20*adx + xvel;
569 dotptr->dy = 2*dy + yvel;
570 dotptr->life = 60 * adx;
573 dotptr->dx = 2*dx + xvel;
574 dotptr->dy = 20*ady + yvel;
575 dotptr->life = 60 * ady;
579 if(dotptr-edot>= MAXENGINEDOTS) {
586 int drawdots(SDL_Surface *s) {
590 // Draw the background stars aka space dots
593 // Draw the score when playing the game or whn the game is freshly over
594 if(1 || state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER ) {
595 SDL_UnlockSurface(s);
597 scorepos = XSIZE-250;
598 n = snprintf(topline, 50, "Time: ");
599 snprintscore(topline + n, 50-n, score);
600 PutString(s,scorepos,0,topline);
605 // Draw all the engine dots
608 // Create more engine dots comin out da back
610 create_engine_dots(200);
612 // Create engine dots out the side we're moving from
613 for(m = 0; m<4; m ++ ) {
614 if(maneuver & 1<<m) { // 'maneuver' is a bit field
615 create_engine_dots2(80,m);
619 // Draw all outstanding bang dots
620 //if(bangdotlife-- > 0)
623 SDL_UnlockSurface(s);
626 char * load_file(char *s) {
627 static char retval[1024];
628 snprintf(retval, 1024, "%s/%s", data_dir, s);
633 int missing(char *dirname) {
636 return (!S_ISDIR(buf.st_mode));
639 int init(int fullscreen) {
646 read_high_score_table();
648 // Where are our data files?
650 // second alternative: RD_DATADIR
651 // final alternative: /usr/share/vor
652 data_dir = strdup("./data");
653 if(missing(data_dir)) {
655 env = getenv("RD_DATADIR");
657 data_dir = strdup(env);
658 if(missing(data_dir)) {
659 fprintf (stderr,"Cannot find data directory $RD_DATADIR\n");
663 data_dir = strdup("/usr/share/vor");
664 if(missing(data_dir)) {
665 fprintf (stderr,"Cannot find data in %s\n", data_dir);
673 // Initialise SDL with audio and video
674 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)!= 0) {
676 printf ("Can't open sound, starting without it\n");
680 atexit(SDL_CloseAudio);
681 oss_sound_flag = init_sound();
685 // Initialise with video only
686 CONDERROR(SDL_Init(SDL_INIT_VIDEO)!= 0);
693 // Attempt to get the required video size
694 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
695 if(fullscreen) flag |= SDL_FULLSCREEN;
696 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
698 // Set the title bar text
699 SDL_WM_SetCaption("Rock Dodgers", "rockdodgers");
701 NULLERROR(surf_screen);
703 // Set the heat color from the range 0 (cold) to 300 (blue-white)
704 for(i = 0; i<W*3; i ++ ) {
705 heatcolor[i] = SDL_MapRGB(
707 (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?
712 NULLERROR(temp = IMG_Load(load_file("banners/variations.png")));
713 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
715 NULLERROR(temp = IMG_Load(load_file("banners/on.png")));
716 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
718 NULLERROR(temp = IMG_Load(load_file("banners/rockdodger.png")));
719 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
721 NULLERROR(temp = IMG_Load(load_file("banners/game.png")));
722 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
724 NULLERROR(temp = IMG_Load(load_file("banners/over.png")));
725 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
727 surf_font_big = IMG_Load(load_file(BIG_FONT_FILE));
728 InitFont(surf_font_big);
730 // Load the spaceship graphic.
731 NULLERROR(temp = IMG_Load(load_file("sprites/ship.png")));
732 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
734 // Load the life indicator (small ship) graphic.
735 NULLERROR(temp = IMG_Load(load_file("indicators/life.png")));
736 NULLERROR(surf_life = SDL_DisplayFormat(temp));
738 // Create the array of black points;
739 SDL_LockSurface(surf_ship);
740 raw_pixels = (Uint16 *) surf_ship->pixels;
741 for(i = 0; i<surf_ship->w; i ++ ) {
742 for(j = 0; j<surf_ship->h; j ++ ) {
743 if(raw_pixels[j*(surf_ship->pitch)/2 + i] == 0) {
751 SDL_UnlockSurface(surf_ship);
756 // Load all our lovely rocks
757 for(i = 0; i<NROCKS; i ++ ) {
760 sprintf(a,load_file("sprites/rock%d.png"),i);
761 NULLERROR(temp = IMG_Load(a));
762 NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp));
764 sprintf(a,load_file("sprites/deadrock%d.png"),i);
765 NULLERROR(temp = IMG_Load(a));
766 NULLERROR(surf_deadrock[i] = SDL_DisplayFormat(temp));
769 // Remove the mouse cursor
771 SDL_ShowCursor(SDL_DISABLE);
779 struct black_point_struct *p;
783 float fadegame,fadeover;
785 char *statedisplay, buf[1024];
794 // Draw a fully black background
795 SDL_FillRect(surf_screen,NULL,0);
800 // Show the current state
803 statedisplay = "title_page";
806 statedisplay = "gameplay";
809 statedisplay = "dead_pause";
812 statedisplay = "game_over";
814 case HIGH_SCORE_ENTRY:
815 statedisplay = "high_score_entry";
817 case HIGH_SCORE_DISPLAY:
818 statedisplay = "high_score_display";
821 statedisplay = "demo";
824 snprintf(buf,1024, "mode = %s", statedisplay);
825 PutString(surf_screen,0,YSIZE-50,buf);
829 // Draw the background dots
830 drawdots(surf_screen);
832 // If it's firing, draw the laser
838 if(!gameover && (state == GAMEPLAY || state == DEMO) ) {
839 src.w = surf_ship->w;
840 src.h = surf_ship->h;
845 SDL_BlitSurface(surf_ship,&src,surf_screen,&dest);
848 // Draw all the rocks, in all states
849 for(i = 0; i<MAXROCKS; i ++ ) {
852 src.w = rock[i].image->w;
853 src.h = rock[i].image->h;
856 dest.x = (int) rock[i].x;
857 dest.y = (int) rock[i].y;
860 SDL_BlitSurface(rock[i].image,&src,surf_screen,&dest);
862 // Draw the heated part of the rock, in an alpha which reflects the
863 // amount of heat in the rock.
865 SDL_Surface *deadrock;
866 deadrock = surf_deadrock[rock[i].type_number];
867 SDL_SetAlpha(deadrock,SDL_SRCALPHA,rock[i].heat*255/rock[i].image->h);
868 dest.x = (int) rock[i].x; // kludge
869 SDL_BlitSurface(deadrock,&src,surf_screen,&dest);
871 rock[i].heat-= movementrate;
875 // If the rock is heated past a certain point, the water content of
876 // the rock flashes to steam, releasing enough energy to destroy
877 // the rock in spectacular fashion.
878 if(rock[i].heat>rock[i].image->h) {
880 play_sound(1 + (int)(rnd()*3));
881 makebangdots(rock[i].x,rock[i].y,rock[i].xvel,rock[i].yvel,rock[i].image,10);
887 // If it's game over, show the game over graphic in the dead centre
890 if(fadetimer<3.0/faderate) {
891 fadegame = fadetimer/(3.0/faderate);
896 if(fadetimer<3.0/faderate) {
898 } else if(fadetimer<6.0/faderate) {
899 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
904 src.w = surf_b_game->w;
905 src.h = surf_b_game->h;
908 dest.x = (XSIZE-src.w)/2;
909 dest.y = (YSIZE-src.h)/2-40;
910 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += movementrate/1.0))));
911 SDL_BlitSurface(surf_b_game,&src,surf_screen,&dest);
913 src.w = surf_b_over->w;
914 src.h = surf_b_over->h;
917 dest.x = (XSIZE-src.w)/2;
918 dest.y = (YSIZE-src.h)/2 + 40;
919 SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
920 SDL_BlitSurface(surf_b_over,&src,surf_screen,&dest);
925 src.w = surf_b_variations->w;
926 src.h = surf_b_variations->h;
929 dest.x = (XSIZE-src.w)/2 + cos(fadetimer/6.5)*10;
930 dest.y = (YSIZE/2-src.h)/2 + sin(fadetimer/5.0)*10;
931 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += movementrate/2.0)));
932 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
934 src.w = surf_b_on->w;
935 src.h = surf_b_on->h;
938 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 1.0)/6.5)*10;
939 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
940 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
941 SDL_BlitSurface(surf_b_on,&src,surf_screen,&dest);
943 src.w = surf_b_rockdodger->w;
944 src.h = surf_b_rockdodger->h;
947 dest.x = (XSIZE-src.w)/2 + cos((fadetimer + 2.0)/6.5)*10;
948 dest.y = (YSIZE/2-src.h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
949 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
950 SDL_BlitSurface(surf_b_rockdodger,&src,surf_screen,&dest);
952 text = "Version " VERSION;
953 x = (XSIZE-SFont_wide(text))/2 + sin(fadetimer/4.5)*10;
954 PutString(surf_screen,x,YSIZE-50 + sin(fadetimer/2)*5,text);
956 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
957 //text = "Press SPACE to start!";
958 x = (XSIZE-SFont_wide(text))/2 + cos(fadetimer/4.5)*10;
959 PutString(surf_screen,x,YSIZE-100 + cos(fadetimer/3)*5,text);
962 case HIGH_SCORE_ENTRY:
964 if(score >= high[7].score) {
966 if(SFont_Input (surf_screen, 330, 50 + (scorerank + 2)*font_height, 300, name)) {
967 // Insert name into high score table
969 // Lose the lowest name forever (loser!)
970 //if(high[7].allocated)
971 // free(high[7].name); // THIS WAS CRASHING SO I REMOVED IT
973 // Insert new high score
974 high[scorerank].score = score;
975 high[scorerank].name = strdup(name); // MEMORY NEVER FREED!
976 high[scorerank].allocated = 1;
978 // Set the global name string to "", ready for the next winner
981 // Change state to briefly show high scores page
982 state = HIGH_SCORE_DISPLAY;
985 // Write the high score table to the file
986 write_high_score_table();
988 // Play the title page tune
992 state = HIGH_SCORE_DISPLAY;
997 case HIGH_SCORE_DISPLAY:
998 // Display de list o high scores mon.
999 PutString(surf_screen,180,50,"High scores");
1000 for(i = 0; i<8; i ++ ) {
1002 sprintf(s, "#%1d",i + 1);
1003 PutString(surf_screen, 150, 50 + (i + 2)*font_height,s);
1004 snprintscore(s, 1024, high[i].score);
1005 PutString(surf_screen, 200, 50 + (i + 2)*font_height,s);
1006 sprintf(s, "%3s", high[i].name);
1007 PutString(surf_screen, 330, 50 + (i + 2)*font_height,s);
1012 if(!gameover && state == GAMEPLAY) {
1013 // Show the freaky shields
1014 SDL_LockSurface(surf_screen);
1015 raw_pixels = (Uint16 *) surf_screen->pixels;
1016 if(initialshield>0 || shieldsup && shieldlevel>0) {
1020 if(initialshield>0) {
1021 initialshield-= movementrate;
1022 c = SDL_MapRGB(surf_screen->format,0,255,255);
1024 c = heatcolor[(int)shieldlevel];
1025 shieldlevel-= movementrate;
1029 for(p = black_point; p<blackptr; p ++ ) {
1030 x = p->x + (int)xship + (rnd() + rnd()-1)*sin(shieldpulse)*4 + 1;
1031 y = p->y + (int)yship + (rnd() + rnd()-1)*sin(shieldpulse)*4 + 1;
1032 if(x>0 && y>0 && x<XSIZE && y<YSIZE) {
1033 offset = surf_screen->pitch/2 * y + x;
1034 raw_pixels[offset] = c;
1038 // When the shields are off, check that the black points
1039 // on the ship are still black, and not covered up by rocks
1040 for(p = black_point; p<blackptr; p ++ ) {
1041 offset = surf_screen->pitch/2 * (p->y + (int)yship) + p->x + (int)xship;
1042 if(raw_pixels[offset]) {
1043 // Set the bang flag
1048 SDL_UnlockSurface(surf_screen);
1052 dotcollision(surf_screen); // Kill dots that are not on their spots
1055 // Draw all the little ships
1056 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER)
1057 for(i = 0; i<nships-1; i ++ ) {
1058 src.w = surf_life->w;
1059 src.h = surf_life->h;
1062 dest.x = (i + 1)*(src.w + 10);
1064 SDL_BlitSurface(surf_life,&src,surf_screen,&dest);
1070 n = SDL_GetTicks()-initticks;
1072 ticks_since_last = n-score;
1076 ticks_since_last = SDL_GetTicks()-last_ticks;
1077 last_ticks = SDL_GetTicks();
1078 if(ticks_since_last>200 || ticks_since_last<0) {
1082 movementrate = ticks_since_last/50.0;
1083 if(state == GAMEPLAY) {
1084 score += ticks_since_last;
1088 // Update the surface
1089 SDL_Flip(surf_screen);
1101 // Count down the game loop timer, and change state when it gets to zero or less;
1103 if((state_timeout -= movementrate*3) < 0) {
1106 // Create a new ship and start all over again
1109 initialshield = 150;
1117 state = HIGH_SCORE_ENTRY;
1120 state_timeout = 5.0e6;
1122 if(score>= high[7].score) {
1123 // Read the high score table from the storage file
1124 read_high_score_table();
1126 // Find ranking of this score, store as scorerank
1127 for(i = 0; i<8; i ++ ) {
1128 if(high[i].score <= score) {
1134 // Move all lower scores down a notch
1135 for(i = 7; i>= scorerank; i--)
1136 high[i] = high[i-1];
1138 // Insert blank high score
1139 high[scorerank].score = score;
1140 high[scorerank].name = "";
1141 high[scorerank].allocated = 0;
1145 case HIGH_SCORE_DISPLAY:
1147 state_timeout = 500.0;
1149 case HIGH_SCORE_ENTRY:
1150 // state = TITLE_PAGE;
1152 // state_timeout = 100.0;
1155 state = HIGH_SCORE_DISPLAY;
1156 state_timeout = 200.0;
1161 if(--countdown<= 0 && (rnd()*100.0<(rockrate += 0.025))) {
1164 if(rockptr-rock>= MAXROCKS) {
1167 if(!rockptr->active) {
1168 rockptr->x = (float)XSIZE;
1169 rockptr->xvel = -(rockspeed)*(1 + rnd());
1170 rockptr->yvel = rnd()-0.5;
1171 rockptr->type_number = random() % NROCKS;
1173 rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
1174 rockptr->active = 1;
1175 rockptr->y = rnd()*(YSIZE + rockptr->image->h);
1177 if(movementrate>0.1) {
1178 countdown = (int)(ROCKRATE/movementrate);
1186 xvel *= pow((double)0.9,(double)movementrate);
1187 yvel *= pow((double)0.9,(double)movementrate);
1188 // if(abs(xvel)<0.00001) xvel = 0;
1189 // if(abs(yvel)<0.00001) yvel = 0;
1193 xship += xvel*movementrate;
1194 yship += yvel*movementrate;
1197 yscroll = yship - (YSIZE / 2);
1199 yscroll = yscroll*movementrate;
1202 // Move all the rocks
1203 for(i = 0; i<MAXROCKS; i ++ ) if(rock[i].active) {
1204 rock[i].x += rock[i].xvel*movementrate;
1205 rock[i].y += rock[i].yvel*movementrate + yscroll;
1206 if(rock[i].y > YSIZE) {
1208 rock[i].y -= rock[i].image->w;
1209 } else if(rock[i].y < -rock[i].image->w) {
1211 rock[i].y += rock[i].image->w;
1219 if(xship<0 || xship>XSIZE-surf_ship->w) {
1220 // BOUNCE from left and right wall
1221 xship -= xvel*movementrate;
1226 if(yship<0 || yship>YSIZE-surf_ship->h) {
1227 // BOUNCE from top and bottom wall
1233 if(draw() && state == GAMEPLAY) {
1234 if(oss_sound_flag) {
1235 // Play the explosion sound
1238 makebangdots(xship,yship,xvel,yvel,surf_ship,30);
1242 state_timeout = 200.0;
1244 faderate = movementrate;
1248 state_timeout = 100.0;
1253 keystate = SDL_GetKeyState(NULL);
1255 if(state!= HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
1259 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE || state == DEMO)) {
1261 for(i = 0; i<MAXROCKS; i++ ) {
1288 keystate = SDL_GetKeyState(NULL);
1291 if(state == GAMEPLAY) {
1295 if(keystate[SDLK_UP] | keystate[SDLK_c]) { yvel -= 1.5*movementrate; maneuver|= 1<<3;}
1296 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { yvel += 1.5*movementrate; maneuver|= 1<<1;}
1297 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { xvel -= 1.5*movementrate; maneuver|= 1<<2;}
1298 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { xvel += 1.5*movementrate; maneuver|= 1;}
1299 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
1302 if(keystate[SDLK_p] | keystate[SDLK_s]) {
1307 src.w = surf_b_variations->w;
1308 src.h = surf_b_variations->h;
1311 dest.x = (XSIZE-src.w)/2;
1312 dest.y = (YSIZE-src.h)/2;
1313 SDL_BlitSurface(surf_b_variations,&src,surf_screen,&dest);
1314 // Update the surface
1315 SDL_Flip(surf_screen);
1329 } else if(state == GAME_OVER) {
1330 if(keystate[SDLK_SPACE]) {
1336 main(int argc, char **argv) {
1337 int i, x, fullscreen;
1344 while ((x = getopt(argc,argv,"efhsp"))>= 0) {
1349 case 'f': // fullscreen
1353 printf("Variations on RockDodger\n"
1354 " -e Big tail [E]ngine\n"
1355 " -f [F]ull screen\n"
1356 " -h This [H]elp message\n"
1357 " -p Stupid original [P]hysics (friction)\n"
1358 " -s [S]ilent (no sound)\n");
1361 case 'p': // physics
1370 if(init(fullscreen)) {
1371 printf ("ta: '%s'\n",initerror);
1376 for(i = 0; i<MAXROCKS; i ++ ) {
1381 initticks = SDL_GetTicks();
1382 if(gameloop() == 0) {
1385 printf ("score = %d\n",score);