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 #include <SDL_image.h>
45 #define SDL_SetAlpha(surf, flag, alpha)
48 // ************************************* VARS
49 // SDL_Surface global variables
51 *surf_screen, // Screen
52 *surf_b_variations, // "variations" banner
53 *surf_b_on, // "on" banner
54 *surf_b_rockdodger, // "rockdodger" banner
55 *surf_b_game, // Title element "game"
56 *surf_b_over, // Title element "over"
57 *surf_life, // Indicator of number of ships remaining
58 *surf_rock[NROCKS], // THE ROCKS
59 *surf_font_big; // The big font
64 // Structure global variables
65 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
66 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
68 // Other global variables
72 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
74 float screendx = SCREENDXMIN, screendy = 0.0;
77 // all movement is based on t_frame.
78 unsigned long frames, start, end;
79 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
80 int ms_frame; // length of this frame (milliseconds)
81 int ms_end; // end of this frame (milliseconds)
84 float bangx, bangy, bangdx, bangdy;
88 float fadetimer = 0, faderate;
90 int pausedown = 0, paused = 0;
92 // bangdot start (bd1) and end (bd2) position:
103 enum states state = TITLE_PAGE;
104 float state_timeout = 600.0;
109 "Press SPACE for normal game",
110 "Press '1' for easy game",
111 "http://jasonwoof.org/vor"
114 "Press SPACE for easy game",
115 "Press '2' for normal game",
116 "http://jasonwoof.org/vor"
120 int bangdotlife, nbangdots;
121 Uint16 heatcolor[W*3];
125 extern int optind, opterr, optopt;
127 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
129 // ************************************* FUNCS
134 for(i = 0; i<MAXENGINEDOTS; i++) {
140 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s)
150 row_inc = s->pitch/sizeof(uint16_t) - s->w;
151 colorkey = s->format->colorkey;
153 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
157 for(y=0; y<s->h; y++) {
158 for(x = 0; x<s->w; x++) {
160 if(c && c != colorkey) {
161 theta = frnd()*M_PI*2;
162 r = frnd(); r = 1 - r*r;
164 bdot[bd2].dx = 45*r*cos(theta) + dx;
165 bdot[bd2].dy = 45*r*sin(theta) + dy;
166 bdot[bd2].x = x + xbang;
167 bdot[bd2].y = y + ybang;
168 //bdot[bd2].c = (i < n-3) ? 0 : c;
169 bdot[bd2].life = frnd() * 99;
170 bdot[bd2].decay = frnd()*3 + 1;
171 bdot[bd2].active = 1;
173 bd2 = (bd2+1) % MAXBANGDOTS;
180 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
184 move_bang_dots(float ticks)
189 for(i=0; i<MAXBANGDOTS; i++) {
190 if(!bdot[i].active) continue;
192 // decrement life and maybe kill
193 bdot[i].life -= bdot[i].decay * ticks/2.0;
194 if(bdot[i].life < 0) { bdot[i].active = 0; continue; }
197 bdot[i].x += (bdot[i].dx - screendx)*ticks;
198 bdot[i].y += (bdot[i].dy - screendy)*ticks;
199 if(bdot[i].x < 0 || bdot[i].x >= (XSIZE - 0.000001) || bdot[i].y < 0 || bdot[i].y >= (YSIZE - 0.000001)) {
205 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
206 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
208 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
209 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
218 draw_bang_dots(SDL_Surface *s)
221 uint16_t *pixels, *pixel, c;
222 int row_inc = s->pitch/sizeof(uint16_t);
224 pixels = (uint16_t *) s->pixels;
226 for(i=0; i<MAXBANGDOTS; i++) {
227 if(!bdot[i].active) continue;
229 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
230 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
237 new_engine_dots(float time_span) {
239 int n = time_span * ENGINE_DOTS_PER_TIC;
240 float a, r; // angle, random length
242 float hx, hy; // half ship width/height.
243 static const int s[4] = { 2, 1, 0, 1 };
245 float accelh, accelv, past_ship_dx, past_ship_dy;
247 hx = ship.image->w / 2;
248 hy = ship.image->h / 2;
250 for(dir=0; dir<4; dir++) {
251 if(!(ship.jets & 1<<dir)) continue;
253 for(i = 0; i<n; i++) {
254 if(dotptr->active == 0) {
255 a = frnd()*M_PI + (dir-1)*M_PI_2;
256 r = sin(frnd()*M_PI);
258 dy = r * -sin(a); // screen y is "backwards".
262 // dot was created at a random time during the time span
263 time = frnd() * time_span; // this is how long ago
265 // calculate how fast the ship was going when this engine dot was
266 // created (as if it had a smooth acceleration). This is used in
267 // determining the velocity of the dots, but not their starting
269 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
270 accelh *= THRUSTER_STRENGTH * time;
271 past_ship_dx = ship.dx - accelh;
272 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
273 accelv *= THRUSTER_STRENGTH * time;
274 past_ship_dy = ship.dy - accelv;
276 // the starting position (not speed) of the dot is calculated as
277 // though the ship were traveling at a constant speed for this
279 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
280 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
282 dotptr->dx = past_ship_dx + 2*dx;
283 dotptr->dy = past_ship_dy + 20*dy;
284 dotptr->life = 60 * fabs(dy);
286 dotptr->dx = past_ship_dx + 20*dx;
287 dotptr->dy = past_ship_dy + 2*dy;
288 dotptr->life = 60 * fabs(dx);
291 // move the dot as though it were created in the past
292 dotptr->x += (dotptr->dx - screendx) * time;
293 dotptr->y += (dotptr->dy - screendy) * time;
295 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
303 move_engine_dots(float ticks) {
307 for(i = 0; i<MAXENGINEDOTS; i++) {
308 if(!edot[i].active) continue;
310 edot[i].x += (edot[i].dx - screendx)*ticks;
311 edot[i].y += (edot[i].dy - screendy)*ticks;
312 edot[i].life -= t_frame*3;
313 if(edot[i].life < 0 || edot[i].x<0 || edot[i].x >= (XSIZE - 0.000001) || edot[i].y < 0 || edot[i].y >= (YSIZE - 0.000001)) {
319 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
320 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
322 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
323 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
331 draw_engine_dots(SDL_Surface *s) {
334 uint16_t *pixels = (uint16_t *) s->pixels;
335 int row_inc = s->pitch/sizeof(uint16_t);
338 for(i = 0; i<MAXENGINEDOTS; i++) {
339 if(!edot[i].active) continue;
341 heatindex = edot[i].life * 6;
342 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
343 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
348 draw_dots(SDL_Surface *s) {
349 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
353 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
357 load_image(char *filename)
359 SDL_Surface *tmp, *img = NULL;
360 char *s = add_data_path(filename);
365 img = SDL_DisplayFormat(tmp);
366 SDL_FreeSurface(tmp);
375 load_sprite(SPRITE(&ship), "ship.png");
378 void font_cleanup() {
389 // Where are our data files?
390 if(!find_files()) exit(1);
391 read_high_score_table();
394 // Initialize SDL with audio and video
395 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
397 fputs("Can't open sound, starting without it\n", stderr);
401 atexit(SDL_CloseAudio);
402 opt_sound = init_sound();
405 // Initialize with video only
406 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
410 play_tune(TUNE_TITLE_PAGE);
412 // Attempt to get the required video size
413 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
414 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
415 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
417 // Set the title bar text
418 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
420 NULLERROR(surf_screen);
422 // Set the heat color from the range 0 (cold) to 300 (blue-white)
423 for(i = 0; i<W*3; i++) {
424 heatcolor[i] = SDL_MapRGB(
426 (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?
431 NULLERROR(surf_b_variations = load_image("b_variations.png"));
432 NULLERROR(surf_b_on = load_image("b_on.png"));
433 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
435 NULLERROR(surf_b_game = load_image("b_game.png"));
436 NULLERROR(surf_b_over = load_image("b_over.png"));
438 // Load the life indicator (small ship) graphic.
439 NULLERROR(surf_life = load_image("life.png"));
441 // Load the font image
442 s = add_data_path("font.png");
444 g_font = font_load(s);
445 atexit(&font_cleanup);
447 fprintf(stderr, "could create path to font\n");
455 add_sprite(SPRITE(&ship));
457 // Remove the mouse cursor
459 SDL_ShowCursor(SDL_DISABLE);
471 for(i=0; i<ship.lives-1; i++) {
472 dest.x = (i + 1)*(surf_life->w + 10);
474 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
484 float a_game = 0, a_over = 0;
486 // fade in "GAME", then "OVER".
487 a_game = min(1.0, faderate*fadetimer/3.0);
488 if(a_game == 1.0) a_over = min(1.0, faderate*fadetimer/3.0 - 1);
490 fadetimer += t_frame;
492 dest.x = (XSIZE-surf_b_game->w)/2;
493 dest.y = (YSIZE-surf_b_game->h)/2-40;
494 //SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(a_game*(200 + 55*cos(fadetimer))));
495 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
497 dest.x = (XSIZE-surf_b_over->w)/2;
498 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
499 //SDL_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(a_over*(200 + 55*sin(fadetimer))));
500 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
502 if(new_high_score(score)) {
503 text0 = "New High Score!";
504 text1 = "Press SPACE to continue";
506 text0 = msgs[g_easy][0];
507 text1 = msgs[g_easy][1];
510 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
511 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
513 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
514 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
518 draw_title_page(void)
524 fadetimer += t_frame/2.0;
526 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
527 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
528 //SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer)));
529 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
531 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
532 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
533 //SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
534 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
536 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
537 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
538 //SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
539 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
541 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
542 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
543 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
545 text = "Version " VERSION;
546 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
547 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
553 SDL_FillRect(surf_screen,NULL,0); // black background
554 draw_dots(surf_screen); // background dots
555 draw_sprite(SPRITE(&ship));
561 // If it's game over, show the game over graphic in the dead centre
563 case GAME_OVER: draw_game_over(); break;
565 case TITLE_PAGE: draw_title_page(); break;
567 case HIGH_SCORE_ENTRY:
568 play_tune(TUNE_HIGH_SCORE_ENTRY);
570 case HIGH_SCORE_DISPLAY:
571 // Display de list o high scores mon.
572 display_scores(surf_screen, 150,50);
576 ; // no action necessary
581 // Update the surface
582 SDL_Flip(surf_screen);
586 kill_ship(Sprite *ship)
593 do_collision(Sprite *a, Sprite *b)
595 if(a->type == SHIP) kill_ship(a);
596 else if (b->type == SHIP) kill_ship(b);
601 init_score_entry(void)
604 state = HIGH_SCORE_ENTRY;
605 state_timeout = 5.0e6;
606 SDL_EnableUNICODE(1);
607 while(SDL_PollEvent(&e))
612 // Count down the state timer, and change state when it gets to zero or less;
614 update_state(float ticks)
616 state_timeout -= ticks*3;
617 if(state_timeout > 0) return;
621 // Restore the ship and continue playing
622 ship.flags = DRAW|MOVE|COLLIDE;
624 play_tune(TUNE_GAMEPLAY);
627 if(new_high_score(score)) init_score_entry();
629 state = HIGH_SCORE_DISPLAY;
633 case HIGH_SCORE_DISPLAY:
635 state_timeout = 600.0;
638 case HIGH_SCORE_ENTRY:
641 state = HIGH_SCORE_DISPLAY;
642 state_timeout = 200.0;
645 ; // no action necessary
656 ms_frame = SDL_GetTicks() - ms_end;
658 if(ms_frame > 1000) {
661 t_frame = opt_gamespeed * ms_frame / 50;
664 while(SDL_PollEvent(&e)) {
666 case SDL_QUIT: return;
668 if(state == HIGH_SCORE_ENTRY) {
669 if(!process_score_input(&e.key.keysym)) {
670 // Write the high score table to the file
671 write_high_score_table();
672 // continue to display the scores briefly
673 state = HIGH_SCORE_DISPLAY;
675 play_tune(TUNE_TITLE_PAGE);
677 } else if(e.key.keysym.sym == SDLK_q) {
681 if(e.key.keysym.sym == SDLK_ESCAPE) {
687 keystate = SDL_GetKeyState(NULL);
689 if(state == GAMEPLAY) {
693 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
694 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
695 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
696 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
697 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
700 if(keystate[SDLK_p] | keystate[SDLK_s]) {
704 if(!paused) ms_end = SDL_GetTicks();
712 update_state(t_frame);
714 if(state == DEAD_PAUSE && bangx < 60) bangx = 60;
717 tmp = (ship.y+ship.h/2+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
718 screendy += tmp * t_frame/12;
719 tmp = (ship.x+ship.w/2+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
720 screendx += tmp * t_frame/12;
721 // taper off so we don't hit the barrier abruptly.
722 // (if we would hit in < 2 seconds, adjust to 2 seconds).
723 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0)
724 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
725 back_dist += (screendx - SCREENDXMIN)*t_frame;
726 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
729 bangx += (bangdx - screendx)*t_frame;
730 bangy += (bangdy - screendy)*t_frame;
732 move_sprites(t_frame);
733 move_engine_dots(t_frame);
734 move_bang_dots(t_frame);
737 // BOUNCE off left or right edge of screen
738 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
739 ship.x -= (ship.dx-screendx)*t_frame;
740 ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
743 // BOUNCE off top or bottom of screen
744 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
745 ship.y -= (ship.dy-screendy)*t_frame;
746 ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
750 new_engine_dots(t_frame);
754 if(state == GAMEPLAY && bang) {
757 play_sound(SOUND_BANG); // Play the explosion sound
758 bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy;
759 new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image);
763 state_timeout = DEAD_PAUSE_LENGTH;
764 ship.dx = (ship.dx < 0) ? -sqrt(-ship.dx) : sqrt(ship.dx);
765 ship.dy = (ship.dy < 0) ? -sqrt(-ship.dy) : sqrt(ship.dy);
766 if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN;
769 ship.dx = SCREENDXMIN; ship.dy = 0;
770 state_timeout = 200.0;
777 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
778 && (state == HIGH_SCORE_DISPLAY
779 || state == TITLE_PAGE
780 || state == GAME_OVER)) {
781 if(state == GAME_OVER && new_high_score(score))
784 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
786 initial_rocks = NORMAL_I_ROCKS;
787 final_rocks = NORMAL_F_ROCKS;
788 if(opt_gamespeed == EASY_GAMESPEED)
789 opt_gamespeed = NORMAL_GAMESPEED;
790 } else if(keystate[SDLK_1]) {
792 initial_rocks = EASY_I_ROCKS;
793 final_rocks = EASY_F_ROCKS;
794 opt_gamespeed = EASY_GAMESPEED;
798 screendx = SCREENDXMIN; screendy = 0;
800 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
801 ship.dx = screendx; ship.dy = screendy;
803 ship.flags = MOVE|DRAW|COLLIDE;
804 add_sprite(SPRITE(&ship));
809 play_tune(TUNE_GAMEPLAY);
816 if(state == TITLE_PAGE && keystate[SDLK_h]) {
817 state = HIGH_SCORE_DISPLAY;
824 main(int argc, char **argv) {
825 if(!parse_opts(argc, argv)) return 1;
828 printf ("vor: SDL error: '%s'\n",initerror);
832 start = SDL_GetTicks();
835 end = SDL_GetTicks();
836 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));