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>
44 // ************************************* VARS
45 // SDL_Surface global variables
47 *surf_screen, // Screen
48 *surf_b_variations, // "variations" banner
49 *surf_b_on, // "on" banner
50 *surf_b_rockdodger, // "rockdodger" banner
51 *surf_b_game, // Title element "game"
52 *surf_b_over, // Title element "over"
53 *surf_life, // Indicator of number of ships remaining
54 *surf_rock[NROCKS], // THE ROCKS
55 *surf_font_big; // The big font
60 // Structure global variables
61 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
62 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
64 // Other global variables
68 struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 };
70 float screendx = SCREENDXMIN, screendy = 0.0;
73 // all movement is based on t_frame.
74 unsigned long frames, start, end;
75 float t_frame; // length of this frame (in ticks = 1/20th second) adjusted for gamespeed
76 int ms_frame; // length of this frame (milliseconds)
77 int ms_end; // end of this frame (milliseconds)
83 int pausedown = 0, paused = 0;
85 // bangdot start (bd1) and end (bd2) position:
96 enum states state = TITLE_PAGE;
97 float state_timeout = 600.0;
102 "Press SPACE for normal game",
103 "Press '1' for easy game",
104 "http://jasonwoof.org/vor"
107 "Press SPACE for easy game",
108 "Press '2' for normal game",
109 "http://jasonwoof.org/vor"
113 int bangdotlife, nbangdots;
114 Uint16 heatcolor[W*3];
118 extern int optind, opterr, optopt;
120 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
122 // ************************************* FUNCS
127 for(i = 0; i<MAXENGINEDOTS; i++) {
133 new_bang_dots(struct sprite *s)
140 SDL_Surface *img = s->image;
144 row_inc = img->pitch/sizeof(uint16_t) - img->w;
145 colorkey = img->format->colorkey;
147 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
151 for(y=0; y<img->h; y++) {
152 for(x = 0; x<img->w; x++) {
154 if(c && c != colorkey) {
155 theta = frnd()*M_PI*2;
156 r = frnd(); r = 1 - r*r;
158 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
159 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
160 bdot[bd2].x = x + s->x;
161 bdot[bd2].y = y + s->y;
162 //bdot[bd2].c = (i < n-3) ? 0 : c;
163 bdot[bd2].life = frnd() * 99;
164 bdot[bd2].decay = frnd()*3 + 1;
165 bdot[bd2].active = 1;
167 bd2 = (bd2+1) % MAXBANGDOTS;
174 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
178 move_bang_dots(float ticks)
183 for(i=0; i<MAXBANGDOTS; i++) {
184 if(!bdot[i].active) continue;
186 // decrement life and maybe kill
187 bdot[i].life -= bdot[i].decay * ticks/2.0;
188 if(bdot[i].life < 0) { bdot[i].active = 0; continue; }
191 bdot[i].x += (bdot[i].dx - screendx)*ticks;
192 bdot[i].y += (bdot[i].dy - screendy)*ticks;
193 if(bdot[i].x < 0 || bdot[i].x >= (XSIZE - 0.000001) || bdot[i].y < 0 || bdot[i].y >= (YSIZE - 0.000001)) {
199 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
200 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
202 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
203 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
212 draw_bang_dots(SDL_Surface *s)
215 uint16_t *pixels, *pixel, c;
216 int row_inc = s->pitch/sizeof(uint16_t);
218 pixels = (uint16_t *) s->pixels;
220 for(i=0; i<MAXBANGDOTS; i++) {
221 if(!bdot[i].active) continue;
223 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
224 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
231 new_engine_dots(float time_span) {
233 int n = time_span * ENGINE_DOTS_PER_TIC;
234 float a, r; // angle, random length
236 float hx, hy; // half ship width/height.
237 static const int s[4] = { 2, 1, 0, 1 };
239 float accelh, accelv, past_ship_dx, past_ship_dy;
241 hx = ship.image->w / 2;
242 hy = ship.image->h / 2;
244 for(dir=0; dir<4; dir++) {
245 if(!(ship.jets & 1<<dir)) continue;
247 for(i = 0; i<n; i++) {
248 if(dotptr->active == 0) {
249 a = frnd()*M_PI + (dir-1)*M_PI_2;
250 r = sin(frnd()*M_PI);
252 dy = r * -sin(a); // screen y is "backwards".
256 // dot was created at a random time during the time span
257 time = frnd() * time_span; // this is how long ago
259 // calculate how fast the ship was going when this engine dot was
260 // created (as if it had a smooth acceleration). This is used in
261 // determining the velocity of the dots, but not their starting
263 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
264 accelh *= THRUSTER_STRENGTH * time;
265 past_ship_dx = ship.dx - accelh;
266 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
267 accelv *= THRUSTER_STRENGTH * time;
268 past_ship_dy = ship.dy - accelv;
270 // the starting position (not speed) of the dot is calculated as
271 // though the ship were traveling at a constant speed for this
273 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
274 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
276 dotptr->dx = past_ship_dx + 2*dx;
277 dotptr->dy = past_ship_dy + 20*dy;
278 dotptr->life = 60 * fabs(dy);
280 dotptr->dx = past_ship_dx + 20*dx;
281 dotptr->dy = past_ship_dy + 2*dy;
282 dotptr->life = 60 * fabs(dx);
285 // move the dot as though it were created in the past
286 dotptr->x += (dotptr->dx - screendx) * time;
287 dotptr->y += (dotptr->dy - screendy) * time;
289 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
297 move_engine_dots(float ticks) {
301 for(i = 0; i<MAXENGINEDOTS; i++) {
302 if(!edot[i].active) continue;
304 edot[i].x += (edot[i].dx - screendx)*ticks;
305 edot[i].y += (edot[i].dy - screendy)*ticks;
306 edot[i].life -= t_frame*3;
307 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)) {
313 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
314 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
316 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
317 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
325 draw_engine_dots(SDL_Surface *s) {
328 uint16_t *pixels = (uint16_t *) s->pixels;
329 int row_inc = s->pitch/sizeof(uint16_t);
332 for(i = 0; i<MAXENGINEDOTS; i++) {
333 if(!edot[i].active) continue;
335 heatindex = edot[i].life * 6;
336 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
337 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
342 draw_dots(SDL_Surface *s) {
343 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
347 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
351 load_image(char *filename)
353 SDL_Surface *tmp, *img = NULL;
354 char *s = add_data_path(filename);
359 img = SDL_DisplayFormat(tmp);
360 SDL_FreeSurface(tmp);
369 load_sprite(SPRITE(&ship), "ship.png");
372 void font_cleanup() {
383 // Where are our data files?
384 if(!find_files()) exit(1);
385 read_high_score_table();
388 // Initialize SDL with audio and video
389 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
391 fputs("Can't open sound, starting without it\n", stderr);
395 atexit(SDL_CloseAudio);
396 opt_sound = init_sound();
399 // Initialize with video only
400 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
404 play_tune(TUNE_TITLE_PAGE);
406 // Attempt to get the required video size
407 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
408 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
409 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
411 // Set the title bar text
412 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
414 NULLERROR(surf_screen);
416 // Set the heat color from the range 0 (cold) to 300 (blue-white)
417 for(i = 0; i<W*3; i++) {
418 heatcolor[i] = SDL_MapRGB(
420 (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?
425 NULLERROR(surf_b_variations = load_image("b_variations.png"));
426 NULLERROR(surf_b_on = load_image("b_on.png"));
427 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
429 NULLERROR(surf_b_game = load_image("b_game.png"));
430 NULLERROR(surf_b_over = load_image("b_over.png"));
432 // Load the life indicator (small ship) graphic.
433 NULLERROR(surf_life = load_image("life.png"));
435 // Load the font image
436 s = add_data_path("font.png");
438 g_font = font_load(s);
439 atexit(&font_cleanup);
441 fprintf(stderr, "could create path to font\n");
449 add_sprite(SPRITE(&ship));
451 // Remove the mouse cursor
453 SDL_ShowCursor(SDL_DISABLE);
465 for(i=0; i<ship.lives-1; i++) {
466 dest.x = (i + 1)*(surf_life->w + 10);
468 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
479 fadetimer += t_frame;
481 dest.x = (XSIZE-surf_b_game->w)/2;
482 dest.y = (YSIZE-surf_b_game->h)/2-40;
483 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
485 dest.x = (XSIZE-surf_b_over->w)/2;
486 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
487 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
489 if(new_high_score(score)) {
490 text0 = "New High Score!";
491 text1 = "Press SPACE to continue";
493 text0 = msgs[g_easy][0];
494 text1 = msgs[g_easy][1];
497 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
498 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
500 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
501 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
505 draw_title_page(void)
511 fadetimer += t_frame/2.0;
513 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
514 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
515 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
517 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
518 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
519 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
521 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
522 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
523 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
525 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
526 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
527 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
529 text = "Version " VERSION;
530 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
531 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
537 SDL_FillRect(surf_screen,NULL,0); // black background
538 draw_dots(surf_screen); // background dots
539 draw_sprite(SPRITE(&ship));
546 case GAME_OVER: draw_game_over(); break;
548 case TITLE_PAGE: draw_title_page(); break;
550 case HIGH_SCORE_ENTRY: play_tune(TUNE_HIGH_SCORE_ENTRY);
551 // and fall through to
552 case HIGH_SCORE_DISPLAY:
553 // Display de list o high scores mon.
554 display_scores(surf_screen, 150,50);
558 ; // no action necessary
561 // Update the surface
562 SDL_Flip(surf_screen);
566 kill_ship(struct ship *ship)
568 play_sound(SOUND_BANG);
569 new_bang_dots(SPRITE(ship));
572 state_timeout = DEAD_PAUSE_LENGTH;
573 // want ship to be invisible, but keep drifting at sqrt(speed)
574 // to leave it in the middle of the space from the explosion.
576 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
577 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
578 if(ship->dx < SCREENDXMIN) ship->dx = SCREENDXMIN;
581 state_timeout = 200.0;
584 // scrolling is based on the ship speed, so we need to reset it.
585 ship->dx = SCREENDXMIN; ship->dy = 0;
590 do_collision(Sprite *a, Sprite *b)
592 if(a->type == SHIP) kill_ship((struct ship *)a);
593 else if(b->type == SHIP) kill_ship((struct ship *)b);
598 init_score_entry(void)
601 state = HIGH_SCORE_ENTRY;
602 state_timeout = 5.0e6;
603 SDL_EnableUNICODE(1);
604 while(SDL_PollEvent(&e))
609 // Count down the state timer, and change state when it gets to zero or less;
611 update_state(float ticks)
613 state_timeout -= ticks*3;
614 if(state_timeout > 0) return;
617 case GAMEPLAY: break; // no action necessary
619 // Restore the ship and continue playing
620 ship.flags = DRAW|MOVE|COLLIDE;
622 play_tune(TUNE_GAMEPLAY);
625 if(new_high_score(score)) init_score_entry();
627 state = HIGH_SCORE_DISPLAY;
631 case HIGH_SCORE_DISPLAY:
633 state_timeout = 600.0;
636 case HIGH_SCORE_ENTRY:
639 state = HIGH_SCORE_DISPLAY;
640 state_timeout = 200.0;
652 ms_frame = SDL_GetTicks() - ms_end;
654 if(ms_frame > 1000) {
657 t_frame = opt_gamespeed * ms_frame / 50;
660 while(SDL_PollEvent(&e)) {
662 case SDL_QUIT: return;
664 if(state == HIGH_SCORE_ENTRY) {
665 if(!process_score_input(&e.key.keysym)) {
666 // Write the high score table to the file
667 write_high_score_table();
668 // continue to display the scores briefly
669 state = HIGH_SCORE_DISPLAY;
671 play_tune(TUNE_TITLE_PAGE);
673 } else if(e.key.keysym.sym == SDLK_q) {
677 if(e.key.keysym.sym == SDLK_ESCAPE) {
683 keystate = SDL_GetKeyState(NULL);
685 if(state == GAMEPLAY) {
689 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
690 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
691 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
692 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
693 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
696 if(keystate[SDLK_p] | keystate[SDLK_s]) {
700 if(!paused) ms_end = SDL_GetTicks();
708 update_state(t_frame);
711 tmp = (ship.y+ship.h/2+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy);
712 screendy += tmp * t_frame/12;
713 tmp = (ship.x+ship.w/2+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx);
714 screendx += tmp * t_frame/12;
715 // taper off so we don't hit the barrier abruptly.
716 // (if we would hit in < 2 seconds, adjust to 2 seconds).
717 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0)
718 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
719 back_dist += (screendx - SCREENDXMIN)*t_frame;
720 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
722 move_sprites(t_frame); new_rocks(t_frame);
723 move_engine_dots(t_frame); new_engine_dots(t_frame);
724 move_bang_dots(t_frame);
729 // BOUNCE off left or right edge of screen
730 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
731 ship.x -= (ship.dx-screendx)*t_frame;
732 ship.dx = screendx - (ship.dx-screendx)*opt_bounciness;
735 // BOUNCE off top or bottom of screen
736 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
737 ship.y -= (ship.dy-screendy)*t_frame;
738 ship.dy = screendy - (ship.dy-screendy)*opt_bounciness;
744 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
745 && (state == HIGH_SCORE_DISPLAY
746 || state == TITLE_PAGE
747 || state == GAME_OVER)) {
748 if(state == GAME_OVER && new_high_score(score))
751 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
753 initial_rocks = NORMAL_I_ROCKS;
754 final_rocks = NORMAL_F_ROCKS;
755 if(opt_gamespeed == EASY_GAMESPEED)
756 opt_gamespeed = NORMAL_GAMESPEED;
757 } else if(keystate[SDLK_1]) {
759 initial_rocks = EASY_I_ROCKS;
760 final_rocks = EASY_F_ROCKS;
761 opt_gamespeed = EASY_GAMESPEED;
765 screendx = SCREENDXMIN; screendy = 0;
767 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
768 ship.dx = screendx; ship.dy = screendy;
770 ship.flags = MOVE|DRAW|COLLIDE;
771 add_sprite(SPRITE(&ship));
776 play_tune(TUNE_GAMEPLAY);
783 if(state == TITLE_PAGE && keystate[SDLK_h]) {
784 state = HIGH_SCORE_DISPLAY;
791 main(int argc, char **argv) {
792 if(!parse_opts(argc, argv)) return 1;
795 printf ("vor: SDL error: '%s'\n",initerror);
799 start = SDL_GetTicks();
802 end = SDL_GetTicks();
803 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));