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 // ************************************* VARS
46 // SDL_Surface global variables
48 *surf_screen, // Screen
49 *surf_b_variations, // "variations" banner
50 *surf_b_on, // "on" banner
51 *surf_b_rockdodger, // "rockdodger" banner
52 *surf_b_game, // Title element "game"
53 *surf_b_over, // Title element "over"
54 *surf_life, // Indicator of number of ships remaining
55 *surf_rock[NROCKS], // THE ROCKS
56 *surf_font_big; // The big font
65 float mass; // in DOT_MASS_UNITs
66 float decay; // rate at which to reduce mass.
69 struct dot edot[MAXENGINEDOTS], *dotptr = edot;
70 struct dot bdot[MAXBANGDOTS];
72 // Other global variables
76 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, BARRIER_SPEED, 0.0 };
78 float screendx = BARRIER_SPEED, screendy = 0.0;
81 // all movement is based on t_frame.
82 unsigned long frames, start, end;
83 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
84 int ms_frame; // length of this frame (milliseconds)
85 int ms_end; // end of this frame (milliseconds)
87 float gamespeed = 1.00;
93 int pausedown = 0, paused = 0;
95 // bangdot start (bd1) and end (bd2) position:
106 enum states state = TITLE_PAGE;
107 float state_timeout = 600.0;
112 "Press SPACE for normal game",
113 "Press '1' for easy game",
114 "http://jasonwoof.org/vor"
117 "Press SPACE for easy game",
118 "Press '2' for normal game",
119 "http://jasonwoof.org/vor"
123 int bangdotlife, nbangdots;
124 Uint16 heatcolor[W*3];
128 extern int optind, opterr, optopt;
130 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
132 // ************************************* FUNCS
137 for(i = 0; i<MAXENGINEDOTS; i++) {
144 new_engine_dots(float time_span) {
146 int n = time_span * ENGINE_DOTS_PER_TIC;
147 float a, r; // angle, random length
149 float hx, hy; // half ship width/height.
150 static const int s[4] = { 2, 1, 0, 1 };
152 float accelh, accelv, past_ship_dx, past_ship_dy;
154 hx = ship.image->w / 2;
155 hy = ship.image->h / 2;
157 for(dir=0; dir<4; dir++) {
158 if(!(ship.jets & 1<<dir)) continue;
160 for(i = 0; i<n; i++) {
161 if(dotptr->active == 0) {
162 a = frnd()*M_PI + (dir-1)*M_PI_2;
163 r = sin(frnd()*M_PI);
165 dy = r * -sin(a); // screen y is "backwards".
170 // dot was created at a random time during the time span
171 time = frnd() * time_span; // this is how long ago
173 // calculate how fast the ship was going when this engine dot was
174 // created (as if it had a smooth acceleration). This is used in
175 // determining the velocity of the dots, but not their starting
177 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
178 accelh *= THRUSTER_STRENGTH * time;
179 past_ship_dx = ship.dx - accelh;
180 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
181 accelv *= THRUSTER_STRENGTH * time;
182 past_ship_dy = ship.dy - accelv;
184 // the starting position (not speed) of the dot is calculated as
185 // though the ship were traveling at a constant speed for this
187 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
188 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
190 dotptr->dx = past_ship_dx + 2*dx;
191 dotptr->dy = past_ship_dy + 20*dy;
192 dotptr->mass = 60 * fabs(dy);
194 dotptr->dx = past_ship_dx + 20*dx;
195 dotptr->dy = past_ship_dy + 2*dy;
196 dotptr->mass = 60 * fabs(dx);
199 // move the dot as though it were created in the past
200 dotptr->x += (dotptr->dx - screendx) * time;
201 dotptr->y += (dotptr->dy - screendy) * time;
203 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
212 new_bang_dots(struct sprite *s)
219 SDL_Surface *img = s->image;
223 row_inc = img->pitch/sizeof(uint16_t) - img->w;
224 colorkey = img->format->colorkey;
226 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
230 for(y=0; y<img->h; y++) {
231 for(x = 0; x<img->w; x++) {
233 if(c && c != colorkey) {
234 theta = frnd()*M_PI*2;
235 r = frnd(); r = 1 - r*r;
237 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
238 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
239 bdot[bd2].x = x + s->x;
240 bdot[bd2].y = y + s->y;
241 bdot[bd2].mass = frnd() * 99;
242 bdot[bd2].decay = frnd()*1.5 + 0.5;
243 bdot[bd2].active = 1;
245 bd2 = (bd2+1) % MAXBANGDOTS;
252 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
257 move_dot(struct dot *d, float ticks)
263 d->x += (d->dx - screendx) * ticks;
264 d->y += (d->dy - screendy) * ticks;
265 d->mass -= ticks * d->decay;
266 if(d->mass < 0 || fclip(d->x, XSIZE) || fclip(d->y, YSIZE))
269 hit = pixel_collides(d->x, d->y);
270 if(hit && hit->type != SHIP) {
272 mass = sprite_mass(hit);
273 hit->dx += DOT_MASS_UNIT * d->mass * d->dx / mass;
274 hit->dy += DOT_MASS_UNIT * d->mass * d->dy / mass;
281 move_dots(float ticks)
285 for(i=0; i<MAXBANGDOTS; i++) move_dot(&bdot[i], ticks);
286 for(i=0; i<MAXENGINEDOTS; i++) move_dot(&edot[i], ticks);
291 draw_bang_dots(SDL_Surface *s)
294 uint16_t *pixels, *pixel;
295 int row_inc = s->pitch/sizeof(uint16_t);
297 pixels = (uint16_t *) s->pixels;
299 for(i=0; i<MAXBANGDOTS; i++) {
300 if(!bdot[i].active) continue;
301 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
302 *pixel = heatcolor[(int)(bdot[i].mass)*3];
307 draw_engine_dots(SDL_Surface *s)
310 uint16_t *pixels, *pixel;
311 int row_inc = s->pitch/sizeof(uint16_t);
313 pixels = (uint16_t *) s->pixels;
315 for(i = 0; i<MAXENGINEDOTS; i++) {
316 if(!edot[i].active) continue;
317 pixel = pixels + row_inc*(int)(edot[i].y) + (int)(edot[i].x);
318 *pixel = heatcolor[min(3*W-1, (int)(edot[i].mass)*6)];
323 draw_dots(SDL_Surface *s) {
324 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
328 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
332 load_image(char *filename)
334 SDL_Surface *tmp, *img = NULL;
335 char *s = add_data_path(filename);
340 img = SDL_DisplayFormat(tmp);
341 SDL_FreeSurface(tmp);
350 load_sprite(SPRITE(&ship), "ship.png");
353 void font_cleanup() {
364 // Where are our data files?
365 if(!find_files()) exit(1);
366 read_high_score_table();
369 // Initialize SDL with audio and video
370 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
372 fputs("Can't open sound, starting without it\n", stderr);
376 atexit(SDL_CloseAudio);
377 opt_sound = init_sound();
380 // Initialize with video only
381 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
385 play_tune(TUNE_TITLE_PAGE);
387 // Attempt to get the required video size
388 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
389 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
390 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
392 // Set the title bar text
393 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
395 NULLERROR(surf_screen);
397 // Set the heat color from the range 0 (cold) to 300 (blue-white)
398 for(i = 0; i<W*3; i++) {
399 heatcolor[i] = SDL_MapRGB(
401 (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?
406 NULLERROR(surf_b_variations = load_image("b_variations.png"));
407 NULLERROR(surf_b_on = load_image("b_on.png"));
408 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
410 NULLERROR(surf_b_game = load_image("b_game.png"));
411 NULLERROR(surf_b_over = load_image("b_over.png"));
413 // Load the life indicator (small ship) graphic.
414 NULLERROR(surf_life = load_image("life.png"));
416 // Load the font image
417 s = add_data_path("font.png");
419 g_font = font_load(s);
420 atexit(&font_cleanup);
422 fprintf(stderr, "could create path to font\n");
430 add_sprite(SPRITE(&ship));
432 // Remove the mouse cursor
434 SDL_ShowCursor(SDL_DISABLE);
446 for(i=0; i<ship.lives-1; i++) {
447 dest.x = (i + 1)*(surf_life->w + 10);
449 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
460 fadetimer += t_frame;
462 dest.x = (XSIZE-surf_b_game->w)/2;
463 dest.y = (YSIZE-surf_b_game->h)/2-40;
464 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
466 dest.x = (XSIZE-surf_b_over->w)/2;
467 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
468 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
470 if(new_high_score(score)) {
471 text0 = "New High Score!";
472 text1 = "Press SPACE to continue";
474 text0 = msgs[g_easy][0];
475 text1 = msgs[g_easy][1];
478 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
479 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
481 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
482 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
486 draw_title_page(void)
492 fadetimer += t_frame/2.0;
494 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
495 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
496 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
498 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
499 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
500 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
502 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
503 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
504 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
506 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
507 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
508 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
510 text = "Version " VERSION;
511 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
512 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
518 SDL_FillRect(surf_screen,NULL,0); // black background
519 draw_dots(surf_screen); // background dots
520 draw_sprite(SPRITE(&ship));
527 case GAME_OVER: draw_game_over(); break;
529 case TITLE_PAGE: draw_title_page(); break;
531 case HIGH_SCORE_ENTRY: play_tune(TUNE_HIGH_SCORE_ENTRY);
532 // and fall through to
533 case HIGH_SCORE_DISPLAY:
534 // Display de list o high scores mon.
535 display_scores(surf_screen, 150,50);
539 ; // no action necessary
542 // Update the surface
543 SDL_Flip(surf_screen);
547 kill_ship(struct ship *ship)
549 play_sound(SOUND_BANG);
550 new_bang_dots(SPRITE(ship));
553 state_timeout = DEAD_PAUSE_LENGTH;
554 // want ship to be invisible, but keep drifting at sqrt(speed)
555 // to leave it in the middle of the space from the explosion.
557 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
558 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
559 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
562 state_timeout = 200.0;
565 // scrolling is based on the ship speed, so we need to reset it.
566 ship->dx = BARRIER_SPEED; ship->dy = 0;
571 do_collision(Sprite *a, Sprite *b)
573 if(a->type == SHIP) kill_ship((struct ship *)a);
574 else if(b->type == SHIP) kill_ship((struct ship *)b);
579 init_score_entry(void)
582 state = HIGH_SCORE_ENTRY;
583 state_timeout = 5.0e6;
584 SDL_EnableUNICODE(1);
585 while(SDL_PollEvent(&e))
590 // Count down the state timer, and change state when it gets to zero or less;
592 update_state(float ticks)
594 state_timeout -= ticks*3;
595 if(state_timeout > 0) return;
598 case GAMEPLAY: break; // no action necessary
600 // Restore the ship and continue playing
601 ship.flags = DRAW|MOVE|COLLIDE;
603 play_tune(TUNE_GAMEPLAY);
606 if(new_high_score(score)) init_score_entry();
608 state = HIGH_SCORE_DISPLAY;
612 case HIGH_SCORE_DISPLAY:
614 state_timeout = 600.0;
617 case HIGH_SCORE_ENTRY:
620 state = HIGH_SCORE_DISPLAY;
621 state_timeout = 200.0;
633 ms_frame = SDL_GetTicks() - ms_end;
635 if(ms_frame > 1000) {
638 t_frame = gamespeed * ms_frame / 50;
641 while(SDL_PollEvent(&e)) {
643 case SDL_QUIT: return;
645 if(state == HIGH_SCORE_ENTRY) {
646 if(!process_score_input(&e.key.keysym)) {
647 // Write the high score table to the file
648 write_high_score_table();
649 // continue to display the scores briefly
650 state = HIGH_SCORE_DISPLAY;
652 play_tune(TUNE_TITLE_PAGE);
654 } else if(e.key.keysym.sym == SDLK_q) {
658 if(e.key.keysym.sym == SDLK_ESCAPE) {
664 keystate = SDL_GetKeyState(NULL);
666 if(state == GAMEPLAY) {
670 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
671 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
672 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
673 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
674 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
677 if(keystate[SDLK_p] | keystate[SDLK_s]) {
681 if(!paused) ms_end = SDL_GetTicks();
689 update_state(t_frame);
692 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
693 screendy += tmp * t_frame/12;
694 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
695 screendx += tmp * t_frame/12;
696 // taper off so we don't hit the barrier abruptly.
697 // (if we would hit in < 2 seconds, adjust to 2 seconds).
698 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
699 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
700 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
701 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
703 move_sprites(t_frame);
708 new_engine_dots(t_frame);
712 // BOUNCE off left or right edge of screen
713 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
714 ship.x -= (ship.dx-screendx)*t_frame;
715 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
718 // BOUNCE off top or bottom of screen
719 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
720 ship.y -= (ship.dy-screendy)*t_frame;
721 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
727 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
728 && (state == HIGH_SCORE_DISPLAY
729 || state == TITLE_PAGE
730 || state == GAME_OVER)) {
731 if(state == GAME_OVER && new_high_score(score))
734 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
736 initial_rocks = NORMAL_I_ROCKS;
737 final_rocks = NORMAL_F_ROCKS;
738 if(gamespeed == EASY_GAMESPEED)
739 gamespeed = NORMAL_GAMESPEED;
740 } else if(keystate[SDLK_1]) {
742 initial_rocks = EASY_I_ROCKS;
743 final_rocks = EASY_F_ROCKS;
744 gamespeed = EASY_GAMESPEED;
748 screendx = BARRIER_SPEED; screendy = 0;
750 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
751 ship.dx = screendx; ship.dy = screendy;
753 ship.flags = MOVE|DRAW|COLLIDE;
754 add_sprite(SPRITE(&ship));
759 play_tune(TUNE_GAMEPLAY);
766 if(state == TITLE_PAGE && keystate[SDLK_h]) {
767 state = HIGH_SCORE_DISPLAY;
774 main(int argc, char **argv) {
775 if(!parse_opts(argc, argv)) return 1;
778 printf ("vor: SDL error: '%s'\n",initerror);
782 start = SDL_GetTicks();
785 end = SDL_GetTicks();
786 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));