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, BARRIER_SPEED, 0.0 };
70 float screendx = BARRIER_SPEED, 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)
79 float gamespeed = 1.00;
85 int pausedown = 0, paused = 0;
87 // bangdot start (bd1) and end (bd2) position:
98 enum states state = TITLE_PAGE;
99 float state_timeout = 600.0;
104 "Press SPACE for normal game",
105 "Press '1' for easy game",
106 "http://jasonwoof.org/vor"
109 "Press SPACE for easy game",
110 "Press '2' for normal game",
111 "http://jasonwoof.org/vor"
115 int bangdotlife, nbangdots;
116 Uint16 heatcolor[W*3];
120 extern int optind, opterr, optopt;
122 #define TO_TICKS(seconds) ((seconds)*20*gamespeed)
124 // ************************************* FUNCS
129 for(i = 0; i<MAXENGINEDOTS; i++) {
135 new_bang_dots(struct sprite *s)
142 SDL_Surface *img = s->image;
146 row_inc = img->pitch/sizeof(uint16_t) - img->w;
147 colorkey = img->format->colorkey;
149 if(SDL_MUSTLOCK(img)) { SDL_LockSurface(img); }
153 for(y=0; y<img->h; y++) {
154 for(x = 0; x<img->w; x++) {
156 if(c && c != colorkey) {
157 theta = frnd()*M_PI*2;
158 r = frnd(); r = 1 - r*r;
160 bdot[bd2].dx = 45*r*cos(theta) + s->dx;
161 bdot[bd2].dy = 45*r*sin(theta) + s->dy;
162 bdot[bd2].x = x + s->x;
163 bdot[bd2].y = y + s->y;
164 //bdot[bd2].c = (i < n-3) ? 0 : c;
165 bdot[bd2].life = frnd() * 99;
166 bdot[bd2].decay = frnd()*3 + 1;
167 bdot[bd2].active = 1;
169 bd2 = (bd2+1) % MAXBANGDOTS;
176 if(SDL_MUSTLOCK(img)) { SDL_UnlockSurface(img); }
180 move_bang_dots(float ticks)
185 for(i=0; i<MAXBANGDOTS; i++) {
186 if(!bdot[i].active) continue;
188 // decrement life and maybe kill
189 bdot[i].life -= bdot[i].decay * ticks/2.0;
190 if(bdot[i].life < 0) { bdot[i].active = 0; continue; }
193 bdot[i].x += (bdot[i].dx - screendx)*ticks;
194 bdot[i].y += (bdot[i].dy - screendy)*ticks;
195 if(bdot[i].x < 0 || bdot[i].x >= (XSIZE - 0.000001) || bdot[i].y < 0 || bdot[i].y >= (YSIZE - 0.000001)) {
201 if((hit = pixel_collides(bdot[i].x, bdot[i].y))) {
202 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
204 hit->dx += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dx / sprite_mass(hit);
205 hit->dy += ENGINE_DOT_WEIGHT * bdot[i].life * bdot[i].dy / sprite_mass(hit);
214 draw_bang_dots(SDL_Surface *s)
217 uint16_t *pixels, *pixel, c;
218 int row_inc = s->pitch/sizeof(uint16_t);
220 pixels = (uint16_t *) s->pixels;
222 for(i=0; i<MAXBANGDOTS; i++) {
223 if(!bdot[i].active) continue;
225 pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x);
226 if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3];
233 new_engine_dots(float time_span) {
235 int n = time_span * ENGINE_DOTS_PER_TIC;
236 float a, r; // angle, random length
238 float hx, hy; // half ship width/height.
239 static const int s[4] = { 2, 1, 0, 1 };
241 float accelh, accelv, past_ship_dx, past_ship_dy;
243 hx = ship.image->w / 2;
244 hy = ship.image->h / 2;
246 for(dir=0; dir<4; dir++) {
247 if(!(ship.jets & 1<<dir)) continue;
249 for(i = 0; i<n; i++) {
250 if(dotptr->active == 0) {
251 a = frnd()*M_PI + (dir-1)*M_PI_2;
252 r = sin(frnd()*M_PI);
254 dy = r * -sin(a); // screen y is "backwards".
258 // dot was created at a random time during the time span
259 time = frnd() * time_span; // this is how long ago
261 // calculate how fast the ship was going when this engine dot was
262 // created (as if it had a smooth acceleration). This is used in
263 // determining the velocity of the dots, but not their starting
265 accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1);
266 accelh *= THRUSTER_STRENGTH * time;
267 past_ship_dx = ship.dx - accelh;
268 accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1);
269 accelv *= THRUSTER_STRENGTH * time;
270 past_ship_dy = ship.dy - accelv;
272 // the starting position (not speed) of the dot is calculated as
273 // though the ship were traveling at a constant speed for this
275 dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx;
276 dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy;
278 dotptr->dx = past_ship_dx + 2*dx;
279 dotptr->dy = past_ship_dy + 20*dy;
280 dotptr->life = 60 * fabs(dy);
282 dotptr->dx = past_ship_dx + 20*dx;
283 dotptr->dy = past_ship_dy + 2*dy;
284 dotptr->life = 60 * fabs(dx);
287 // move the dot as though it were created in the past
288 dotptr->x += (dotptr->dx - screendx) * time;
289 dotptr->y += (dotptr->dy - screendy) * time;
291 if(dotptr - edot < MAXENGINEDOTS-1) dotptr++;
299 move_engine_dots(float ticks) {
303 for(i = 0; i<MAXENGINEDOTS; i++) {
304 if(!edot[i].active) continue;
306 edot[i].x += (edot[i].dx - screendx)*ticks;
307 edot[i].y += (edot[i].dy - screendy)*ticks;
308 edot[i].life -= t_frame*3;
309 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)) {
315 if((hit = pixel_collides(edot[i].x, edot[i].y))) {
316 if(hit->type != SHIP) { // they shouldn't hit the ship, but they do
318 hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit);
319 hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit);
327 draw_engine_dots(SDL_Surface *s) {
330 uint16_t *pixels = (uint16_t *) s->pixels;
331 int row_inc = s->pitch/sizeof(uint16_t);
334 for(i = 0; i<MAXENGINEDOTS; i++) {
335 if(!edot[i].active) continue;
337 heatindex = edot[i].life * 6;
338 c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
339 pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c;
344 draw_dots(SDL_Surface *s) {
345 if(SDL_MUSTLOCK(s)) { SDL_LockSurface(s); }
349 if(SDL_MUSTLOCK(s)) { SDL_UnlockSurface(s); }
353 load_image(char *filename)
355 SDL_Surface *tmp, *img = NULL;
356 char *s = add_data_path(filename);
361 img = SDL_DisplayFormat(tmp);
362 SDL_FreeSurface(tmp);
371 load_sprite(SPRITE(&ship), "ship.png");
374 void font_cleanup() {
385 // Where are our data files?
386 if(!find_files()) exit(1);
387 read_high_score_table();
390 // Initialize SDL with audio and video
391 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
393 fputs("Can't open sound, starting without it\n", stderr);
397 atexit(SDL_CloseAudio);
398 opt_sound = init_sound();
401 // Initialize with video only
402 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
406 play_tune(TUNE_TITLE_PAGE);
408 // Attempt to get the required video size
409 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
410 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
411 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
413 // Set the title bar text
414 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
416 NULLERROR(surf_screen);
418 // Set the heat color from the range 0 (cold) to 300 (blue-white)
419 for(i = 0; i<W*3; i++) {
420 heatcolor[i] = SDL_MapRGB(
422 (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?
427 NULLERROR(surf_b_variations = load_image("b_variations.png"));
428 NULLERROR(surf_b_on = load_image("b_on.png"));
429 NULLERROR(surf_b_rockdodger = load_image("b_rockdodger.png"));
431 NULLERROR(surf_b_game = load_image("b_game.png"));
432 NULLERROR(surf_b_over = load_image("b_over.png"));
434 // Load the life indicator (small ship) graphic.
435 NULLERROR(surf_life = load_image("life.png"));
437 // Load the font image
438 s = add_data_path("font.png");
440 g_font = font_load(s);
441 atexit(&font_cleanup);
443 fprintf(stderr, "could create path to font\n");
451 add_sprite(SPRITE(&ship));
453 // Remove the mouse cursor
455 SDL_ShowCursor(SDL_DISABLE);
467 for(i=0; i<ship.lives-1; i++) {
468 dest.x = (i + 1)*(surf_life->w + 10);
470 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
481 fadetimer += t_frame;
483 dest.x = (XSIZE-surf_b_game->w)/2;
484 dest.y = (YSIZE-surf_b_game->h)/2-40;
485 SDL_BlitSurface(surf_b_game,NULL,surf_screen,&dest);
487 dest.x = (XSIZE-surf_b_over->w)/2;
488 dest.y = (YSIZE-surf_b_over->h)/2 + 40;
489 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
491 if(new_high_score(score)) {
492 text0 = "New High Score!";
493 text1 = "Press SPACE to continue";
495 text0 = msgs[g_easy][0];
496 text1 = msgs[g_easy][1];
499 x = (XSIZE-font_width(text0))/2 + cos(fadetimer/9)*10;
500 font_write(x,YSIZE-100 + cos(fadetimer/6)*5,text0);
502 x = (XSIZE-font_width(text1))/2 + sin(fadetimer/9)*10;
503 font_write(x,YSIZE-50 + sin(fadetimer/4)*5,text1);
507 draw_title_page(void)
513 fadetimer += t_frame/2.0;
515 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
516 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
517 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
519 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
520 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
521 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
523 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
524 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
525 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
527 text = msgs[g_easy][(int)(fadetimer/35)%NSEQUENCE];
528 x = (XSIZE-font_width(text))/2 + cos(fadetimer/4.5)*10;
529 font_write(x,YSIZE-100 + cos(fadetimer/3)*5,text);
531 text = "Version " VERSION;
532 x = (XSIZE-font_width(text))/2 + sin(fadetimer/4.5)*10;
533 font_write(x,YSIZE-50 + sin(fadetimer/2)*5,text);
539 SDL_FillRect(surf_screen,NULL,0); // black background
540 draw_dots(surf_screen); // background dots
541 draw_sprite(SPRITE(&ship));
548 case GAME_OVER: draw_game_over(); break;
550 case TITLE_PAGE: draw_title_page(); break;
552 case HIGH_SCORE_ENTRY: play_tune(TUNE_HIGH_SCORE_ENTRY);
553 // and fall through to
554 case HIGH_SCORE_DISPLAY:
555 // Display de list o high scores mon.
556 display_scores(surf_screen, 150,50);
560 ; // no action necessary
563 // Update the surface
564 SDL_Flip(surf_screen);
568 kill_ship(struct ship *ship)
570 play_sound(SOUND_BANG);
571 new_bang_dots(SPRITE(ship));
574 state_timeout = DEAD_PAUSE_LENGTH;
575 // want ship to be invisible, but keep drifting at sqrt(speed)
576 // to leave it in the middle of the space from the explosion.
578 ship->dx = (ship->dx < 0) ? -sqrt(-ship->dx) : sqrt(ship->dx);
579 ship->dy = (ship->dy < 0) ? -sqrt(-ship->dy) : sqrt(ship->dy);
580 if(ship->dx < BARRIER_SPEED) ship->dx = BARRIER_SPEED;
583 state_timeout = 200.0;
586 // scrolling is based on the ship speed, so we need to reset it.
587 ship->dx = BARRIER_SPEED; ship->dy = 0;
592 do_collision(Sprite *a, Sprite *b)
594 if(a->type == SHIP) kill_ship((struct ship *)a);
595 else if(b->type == SHIP) kill_ship((struct ship *)b);
600 init_score_entry(void)
603 state = HIGH_SCORE_ENTRY;
604 state_timeout = 5.0e6;
605 SDL_EnableUNICODE(1);
606 while(SDL_PollEvent(&e))
611 // Count down the state timer, and change state when it gets to zero or less;
613 update_state(float ticks)
615 state_timeout -= ticks*3;
616 if(state_timeout > 0) return;
619 case GAMEPLAY: break; // no action necessary
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;
654 ms_frame = SDL_GetTicks() - ms_end;
656 if(ms_frame > 1000) {
659 t_frame = gamespeed * ms_frame / 50;
662 while(SDL_PollEvent(&e)) {
664 case SDL_QUIT: return;
666 if(state == HIGH_SCORE_ENTRY) {
667 if(!process_score_input(&e.key.keysym)) {
668 // Write the high score table to the file
669 write_high_score_table();
670 // continue to display the scores briefly
671 state = HIGH_SCORE_DISPLAY;
673 play_tune(TUNE_TITLE_PAGE);
675 } else if(e.key.keysym.sym == SDLK_q) {
679 if(e.key.keysym.sym == SDLK_ESCAPE) {
685 keystate = SDL_GetKeyState(NULL);
687 if(state == GAMEPLAY) {
691 if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;}
692 if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;}
693 if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;}
694 if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;}
695 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
698 if(keystate[SDLK_p] | keystate[SDLK_s]) {
702 if(!paused) ms_end = SDL_GetTicks();
710 update_state(t_frame);
713 tmp = (ship.y+ship.h/2 + ship.dy*t_frame - YSCROLLTO)/25 + (ship.dy-screendy);
714 screendy += tmp * t_frame/12;
715 tmp = (ship.x+ship.w/2 + ship.dx*t_frame - XSCROLLTO)/25 + (ship.dx-screendx);
716 screendx += tmp * t_frame/12;
717 // taper off so we don't hit the barrier abruptly.
718 // (if we would hit in < 2 seconds, adjust to 2 seconds).
719 if(dist_ahead + (screendx - BARRIER_SPEED)*TO_TICKS(2) < 0)
720 screendx = BARRIER_SPEED - (dist_ahead/TO_TICKS(2));
721 dist_ahead += (screendx - BARRIER_SPEED)*t_frame;
722 if(MAX_DIST_AHEAD >= 0) dist_ahead = min(dist_ahead, MAX_DIST_AHEAD);
724 move_sprites(t_frame); new_rocks(t_frame);
725 move_engine_dots(t_frame); new_engine_dots(t_frame);
726 move_bang_dots(t_frame);
731 // BOUNCE off left or right edge of screen
732 if(ship.x < 0 || ship.x+ship.w > XSIZE) {
733 ship.x -= (ship.dx-screendx)*t_frame;
734 ship.dx = screendx - (ship.dx-screendx)*BOUNCINESS;
737 // BOUNCE off top or bottom of screen
738 if(ship.y < 0 || ship.y+ship.h > YSIZE) {
739 ship.y -= (ship.dy-screendy)*t_frame;
740 ship.dy = screendy - (ship.dy-screendy)*BOUNCINESS;
746 if((keystate[SDLK_SPACE] || keystate[SDLK_1] || keystate[SDLK_2])
747 && (state == HIGH_SCORE_DISPLAY
748 || state == TITLE_PAGE
749 || state == GAME_OVER)) {
750 if(state == GAME_OVER && new_high_score(score))
753 if((keystate[SDLK_SPACE] && !initial_rocks) || keystate[SDLK_2]) {
755 initial_rocks = NORMAL_I_ROCKS;
756 final_rocks = NORMAL_F_ROCKS;
757 if(gamespeed == EASY_GAMESPEED)
758 gamespeed = NORMAL_GAMESPEED;
759 } else if(keystate[SDLK_1]) {
761 initial_rocks = EASY_I_ROCKS;
762 final_rocks = EASY_F_ROCKS;
763 gamespeed = EASY_GAMESPEED;
767 screendx = BARRIER_SPEED; screendy = 0;
769 ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2;
770 ship.dx = screendx; ship.dy = screendy;
772 ship.flags = MOVE|DRAW|COLLIDE;
773 add_sprite(SPRITE(&ship));
778 play_tune(TUNE_GAMEPLAY);
785 if(state == TITLE_PAGE && keystate[SDLK_h]) {
786 state = HIGH_SCORE_DISPLAY;
793 main(int argc, char **argv) {
794 if(!parse_opts(argc, argv)) return 1;
797 printf ("vor: SDL error: '%s'\n",initerror);
801 start = SDL_GetTicks();
804 end = SDL_GetTicks();
805 // printf("%ld frames in %ld ms, %.2f fps.\n", frames, end-start, frames * 1000.0 / (end-start));