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.
67 int heat; // heat multiplier (color).
70 struct dot edot[MAXENGINEDOTS], *dotptr = edot;
71 struct dot bdot[MAXBANGDOTS];
73 // Other global variables
77 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, BARRIER_SPEED, 0.0 };
79 float screendx = BARRIER_SPEED, screendy = 0.0;
82 // all movement is based on t_frame.
83 unsigned long frames, start, end;
84 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
85 int ms_frame; // length of this frame (milliseconds)
86 int ms_end; // end of this frame (milliseconds)
88 float gamespeed = 1.00;
94 int pausedown = 0, paused = 0;
96 // bangdot start (bd1) and end (bd2) position:
107 enum states state = TITLE_PAGE;
108 float state_timeout = 600.0;
113 "Press SPACE for normal game",
114 "Press '1' for easy game",
115 "http://jasonwoof.org/vor"
118 "Press SPACE for easy game",
119 "Press '2' for normal game",
120 "http://jasonwoof.org/vor"
124 int bangdotlife, nbangdots;
125 Uint16 heatcolor[W*3];
129 extern int optind, opterr, optopt;
131 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
133 // ************************************* FUNCS
138 for(i = 0; i<MAXENGINEDOTS; i++) {
145 new_engine_dots(void) {
147 int n = t_frame * ENGINE_DOTS_PER_TIC;
148 float a, r; // angle, random length
150 float hx, hy; // half ship width/height.
151 static const int s[4] = { 2, 1, 0, 1 };
153 float accelh, accelv, past_ship_dx, past_ship_dy;
155 hx = ship.image->w / 2;
156 hy = ship.image->h / 2;
158 for(dir=0; dir<4; dir++) {
159 if(!(ship.jets & 1<<dir)) continue;
161 for(i = 0; i<n; i++) {
162 if(dotptr->active == 0) {
163 a = frnd()*M_PI + (dir-1)*M_PI_2;
164 r = sin(frnd()*M_PI);
166 dy = r * -sin(a); // screen y is "backwards".
172 // dot was created at a random time during the time span
173 time = frnd() * t_frame; // this is how long ago
175 // calculate how fast the ship was going when this engine dot was
176 // created (as if it had a smooth acceleration). This is used in
177 // determining the velocity of the dots, but not their starting
179 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
180 accelh *= THRUSTER_STRENGTH * time;
181 past_ship_dx = ship.dx - accelh;
182 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
183 accelv *= THRUSTER_STRENGTH * time;
184 past_ship_dy = ship.dy - accelv;
186 // the starting position (not speed) of the dot is calculated as
187 // though the ship were traveling at a constant speed for this
189 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
190 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
192 dotptr->dx = past_ship_dx + 2*dx;
193 dotptr->dy = past_ship_dy + 20*dy;
194 dotptr->mass = 60 * fabs(dy);
196 dotptr->dx = past_ship_dx + 20*dx;
197 dotptr->dy = past_ship_dy + 2*dy;
198 dotptr->mass = 60 * fabs(dx);
201 // move the dot as though it were created in the past
202 dotptr->x += (dotptr->dx - screendx) * time;
203 dotptr->y += (dotptr->dy - screendy) * time;
205 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
214 new_bang_dots(struct sprite *s)
221 SDL_Surface *img = s->image;
225 row_inc = img->pitch/sizeof(uint16_t) - img->w;
226 colorkey = img->format->colorkey;
228 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
232 for(y=0; y<img->h; y++) {
233 for(x = 0; x<img->w; x++) {
235 if(c && c != colorkey) {
236 theta = frnd()*M_PI*2;
237 r = frnd(); r = 1 - r*r;
239 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
240 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
241 bdot[bd2].x = x + s->x;
242 bdot[bd2].y = y + s->y;
243 bdot[bd2].mass = frnd() * 99;
244 bdot[bd2].decay = frnd()*1.5 + 0.5;
246 bdot[bd2].active = 1;
248 bd2 = (bd2+1) % MAXBANGDOTS;
255 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
260 move_dot(struct dot *d)
266 d->x += (d->dx - screendx) * t_frame;
267 d->y += (d->dy - screendy) * t_frame;
268 d->mass -= t_frame * d->decay;
269 if(d->mass < 0 || fclip(d->x, XSIZE) || fclip(d->y, YSIZE))
272 hit = pixel_collides(d->x, d->y);
273 if(hit && hit->type != SHIP) {
275 mass = sprite_mass(hit);
276 hit->dx += DOT_MASS_UNIT * d->mass * d->dx / mass;
277 hit->dy += DOT_MASS_UNIT * d->mass * d->dy / mass;
288 for(i=0; i<MAXBANGDOTS; i++) move_dot(&bdot[i]);
289 for(i=0; i<MAXENGINEDOTS; i++) move_dot(&edot[i]);
294 draw_dot(struct dot *d)
296 uint16_t *pixels, *pixel;
300 pixels = (uint16_t *) surf_screen->pixels;
301 row_inc = surf_screen->pitch / sizeof(uint16_t);
302 pixel = pixels + (int)d->y * row_inc + (int)d->x;
303 *pixel = heatcolor[min(3*W-1, (int)(d->mass * d->heat))];
311 if(SDL_MUSTLOCK(surf_screen)) { SDL_LockSurface(surf_screen); }
313 for(i=0; i<MAXBANGDOTS; i++) draw_dot(&bdot[i]);
314 for(i=0; i<MAXENGINEDOTS; i++) draw_dot(&edot[i]);
315 if(SDL_MUSTLOCK(surf_screen)) { SDL_UnlockSurface(surf_screen); }
319 load_image(char *filename)
321 SDL_Surface *tmp, *img = NULL;
322 char *s = add_data_path(filename);
327 img = SDL_DisplayFormat(tmp);
328 SDL_FreeSurface(tmp);
337 load_sprite(SPRITE(&ship), "ship.png");
340 void font_cleanup() {
351 // Where are our data files?
352 if(!find_files()) exit(1);
353 read_high_score_table();
356 // Initialize SDL with audio and video
357 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
359 fputs("Can't open sound, starting without it\n", stderr);
363 atexit(SDL_CloseAudio);
364 opt_sound = init_sound();
367 // Initialize with video only
368 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
372 play_tune(TUNE_TITLE_PAGE);
374 // Attempt to get the required video size
375 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
376 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
377 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
379 // Set the title bar text
380 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
382 NULLERROR(surf_screen);
384 // Set the heat color from the range 0 (cold) to 300 (blue-white)
385 for(i = 0; i<W*3; i++) {
386 heatcolor[i] = SDL_MapRGB(
388 (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?
393 NULLERROR(surf_b_variations = load_image("b_variations.png"));
394 NULLERROR(surf_b_on = load_image("b_on.png"));
395 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
397 NULLERROR(surf_b_game = load_image("b_game.png"));
398 NULLERROR(surf_b_over = load_image("b_over.png"));
400 // Load the life indicator (small ship) graphic.
401 NULLERROR(surf_life = load_image("life.png"));
403 // Load the font image
404 s = add_data_path("font.png");
406 g_font = font_load(s);
407 atexit(&font_cleanup);
409 fprintf(stderr, "could create path to font\n");
417 add_sprite(SPRITE(&ship));
419 // Remove the mouse cursor
421 SDL_ShowCursor(SDL_DISABLE);
433 for(i=0; i<ship.lives-1; i++) {
434 dest.x = (i + 1)*(surf_life->w + 10);
436 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
447 fadetimer += t_frame;
449 dest.x = (XSIZE-surf_b_game->w)/2;
450 dest.y = (YSIZE-surf_b_game->h)/2-40;
451 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
453 dest.x = (XSIZE-surf_b_over->w)/2;
454 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
455 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
457 if(new_high_score(score)) {
458 text0 = "New High Score!";
459 text1 = "Press SPACE to continue";
461 text0 = msgs[g_easy][0];
462 text1 = msgs[g_easy][1];
465 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
466 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
468 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
469 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
473 draw_title_page(void)
479 fadetimer += t_frame/2.0;
481 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
482 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
483 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
485 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
486 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
487 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
489 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
490 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
491 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
493 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
494 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
495 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
497 text = "Version " VERSION;
498 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
499 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
505 SDL_FillRect(surf_screen,NULL,0); // black background
506 draw_dots(); // background dots
507 draw_sprite(SPRITE(&ship));
514 case GAME_OVER: draw_game_over(); break;
516 case TITLE_PAGE: draw_title_page(); break;
518 case HIGH_SCORE_ENTRY: play_tune(TUNE_HIGH_SCORE_ENTRY);
519 // and fall through to
520 case HIGH_SCORE_DISPLAY:
521 // Display de list o high scores mon.
522 display_scores(150,50);
526 ; // no action necessary
529 // Update the surface
530 SDL_Flip(surf_screen);
534 kill_ship(struct ship *ship)
536 play_sound(SOUND_BANG);
537 new_bang_dots(SPRITE(ship));
540 state_timeout = DEAD_PAUSE_LENGTH;
541 // want ship to be invisible, but keep drifting at sqrt(speed)
542 // to leave it in the middle of the space from the explosion.
544 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
545 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
546 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
549 state_timeout = 200.0;
552 // scrolling is based on the ship speed, so we need to reset it.
553 ship->dx = BARRIER_SPEED; ship->dy = 0;
558 do_collision(Sprite *a, Sprite *b)
560 if(a->type == SHIP) kill_ship((struct ship *)a);
561 else if(b->type == SHIP) kill_ship((struct ship *)b);
566 init_score_entry(void)
569 state = HIGH_SCORE_ENTRY;
570 state_timeout = 5.0e6;
571 SDL_EnableUNICODE(1);
572 while(SDL_PollEvent(&e))
577 // Count down the state timer, and change state when it gets to zero or less;
581 state_timeout -= t_frame*3;
582 if(state_timeout > 0) return;
585 case GAMEPLAY: break; // no action necessary
587 // Restore the ship and continue playing
588 ship.flags = DRAW|MOVE|COLLIDE;
590 play_tune(TUNE_GAMEPLAY);
593 if(new_high_score(score)) init_score_entry();
595 state = HIGH_SCORE_DISPLAY;
599 case HIGH_SCORE_DISPLAY:
601 state_timeout = 600.0;
604 case HIGH_SCORE_ENTRY:
607 state = HIGH_SCORE_DISPLAY;
608 state_timeout = 200.0;
620 ms_frame = SDL_GetTicks() - ms_end;
622 if(ms_frame > 1000) {
625 t_frame = gamespeed * ms_frame / 50;
628 while(SDL_PollEvent(&e)) {
630 case SDL_QUIT: return;
632 if(state == HIGH_SCORE_ENTRY) {
633 if(!process_score_input(&e.key.keysym)) {
634 // Write the high score table to the file
635 write_high_score_table();
636 // continue to display the scores briefly
637 state = HIGH_SCORE_DISPLAY;
639 play_tune(TUNE_TITLE_PAGE);
641 } else if(e.key.keysym.sym == SDLK_q) {
645 if(e.key.keysym.sym == SDLK_ESCAPE) {
651 keystate = SDL_GetKeyState(NULL);
653 if(state == GAMEPLAY) {
657 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
658 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
659 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
660 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
661 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
664 if(keystate[SDLK_p] | keystate[SDLK_s]) {
668 if(!paused) ms_end = SDL_GetTicks();
679 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
680 screendy += tmp * t_frame/12;
681 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
682 screendx += tmp * t_frame/12;
683 // taper off so we don't hit the barrier abruptly.
684 // (if we would hit in < 2 seconds, adjust to 2 seconds).
685 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
686 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
687 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
688 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
699 // BOUNCE off left or right edge of screen
700 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
701 ship.x -= (ship.dx-screendx)*t_frame;
702 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
705 // BOUNCE off top or bottom of screen
706 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
707 ship.y -= (ship.dy-screendy)*t_frame;
708 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
714 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
715 && (state == HIGH_SCORE_DISPLAY
716 || state == TITLE_PAGE
717 || state == GAME_OVER)) {
718 if(state == GAME_OVER && new_high_score(score))
721 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
723 initial_rocks = NORMAL_I_ROCKS;
724 final_rocks = NORMAL_F_ROCKS;
725 if(gamespeed == EASY_GAMESPEED)
726 gamespeed = NORMAL_GAMESPEED;
727 } else if(keystate[SDLK_1]) {
729 initial_rocks = EASY_I_ROCKS;
730 final_rocks = EASY_F_ROCKS;
731 gamespeed = EASY_GAMESPEED;
735 screendx = BARRIER_SPEED; screendy = 0;
737 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
738 ship.dx = screendx; ship.dy = screendy;
740 ship.flags = MOVE|DRAW|COLLIDE;
741 add_sprite(SPRITE(&ship));
746 play_tune(TUNE_GAMEPLAY);
753 if(state == TITLE_PAGE && keystate[SDLK_h]) {
754 state = HIGH_SCORE_DISPLAY;
761 main(int argc, char **argv) {
762 if(!parse_opts(argc, argv)) return 1;
765 printf ("vor: SDL error: '%s'\n",initerror);
769 start = SDL_GetTicks();
772 end = SDL_GetTicks();
773 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));