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
25 #include <SDL/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_ship, // Spaceship element
55 *surf_life, // Indicator of number of ships remaining
56 *surf_rock[NROCKS], // THE ROCKS
57 *surf_font_big; // The big font
62 // Structure global variables
63 struct enginedots edot[MAXENGINEDOTS], *dotptr = edot;
64 struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot;
66 // Other global variables
70 struct shape shipshape;
71 float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE
72 float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick.
73 float screendx = SCREENDXMIN, screendy = 0.0;
74 float xscroll, yscroll;
77 // all movement is based on t_frame.
78 float t_frame; // length of this frame (in ticks = 1/20th second)
79 int ms_frame; // length of this frame (milliseconds)
80 int ms_end; // end of this frame (milliseconds)
82 float bangx, bangy, bangdx, bangdy;
87 float fadetimer = 0, faderate;
89 int pausedown = 0, paused = 0;
91 // bangdot start (bd1) and end (bd2) position:
102 enum states state = TITLE_PAGE;
103 float state_timeout = 600.0;
107 "Press SPACE to start",
108 "http://herkamire.com/jason/vor"
111 int bangdotlife, nbangdots;
112 Uint16 heatcolor[W*3];
116 extern int optind, opterr, optopt;
118 #define TO_TICKS(seconds) ((seconds)*20*opt_gamespeed)
120 // ************************************* FUNCS
125 for(i = 0; i<MAXENGINEDOTS; i++) {
131 new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s)
140 begin_generate = SDL_GetTicks();
142 row_inc = s->pitch/sizeof(uint16_t) - s->w;
143 colorkey = s->format->colorkey;
150 for(y=0; y<s->h; y++) {
151 for(x = 0; x<s->w; x++) {
153 if(c && c != colorkey) {
154 theta = frnd()*M_PI*2;
155 r = frnd(); r = 1 - r*r;
157 bdot[bd2].dx = 45*r*cos(theta) + dx;
158 bdot[bd2].dy = 45*r*sin(theta) + dy;
159 bdot[bd2].x = x + xbang;
160 bdot[bd2].y = y + ybang;
162 bdot[bd2].life = 100;
163 bdot[bd2].decay = frnd()*3 + 1;
164 bdot[bd2].active = 1;
166 // Replace the last few bang dots with the pixels from the exploding object
167 if(endcount>0) bdot[bd2].c = c;
169 bd2 = (bd2+1) % MAXBANGDOTS;
174 if(SDL_GetTicks() - begin_generate > 7) endcount++;
177 SDL_UnlockSurface(s);
181 draw_bang_dots(SDL_Surface *s) {
185 rawpixel = (Uint16 *) s->pixels;
190 for(i = bd1; (bd1 <= bd2)?(i<bd2):(i >= bd1 && i < bd2); last_i = ++i) {
194 if(bdot[i].x <= 0 || bdot[i].x >= XSIZE || bdot[i].y <= 0 || bdot[i].y >= YSIZE) {
195 // If the dot has drifted outside the perimeter, kill it
202 rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)] = bdot[i].c ? bdot[i].c : heatcolor[(int)(bdot[i].life*3)];
203 bdot[i].life -= bdot[i].decay;
204 bdot[i].x += bdot[i].dx*t_frame - xscroll;
205 bdot[i].y += bdot[i].dy*t_frame - yscroll;
207 if(bdot[i].life<0) bdot[i].active = 0;
224 draw_engine_dots(SDL_Surface *s) {
227 rawpixel = (Uint16 *) s->pixels;
229 for(i = 0; i<MAXENGINEDOTS; i++) {
231 edot[i].x += edot[i].dx*t_frame - xscroll;
232 edot[i].y += edot[i].dy*t_frame - yscroll;
233 if((edot[i].life -= t_frame*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
235 } else if(edot[i].x<0 || edot[i].x>XSIZE) {
239 heatindex = edot[i].life * 6;
240 //rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = lifecolor[(int)(edot[i].life)];
241 rawpixel[(int)(s->pitch/2*(int)(edot[i].y)) + (int)(edot[i].x)] = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex];
248 new_engine_dots(int n, int dir) {
250 float a, r; // angle, random length
252 float hx, hy; // half ship width/height.
253 static const int s[4] = { 2, 1, 0, 1 };
255 hx = surf_ship->w / 2;
256 hy = surf_ship->h / 2;
258 for(i = 0; i<n; i++) {
259 if(dotptr->active == 0) {
260 a = frnd()*M_PI + (dir-1)*M_PI_2;
261 r = sin(frnd()*M_PI);
263 dy = r * -sin(a); // screen y is "backwards".
266 dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3;
267 dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3;
269 dotptr->dx = shipdx + 2*dx;
270 dotptr->dy = shipdy + 20*dy;
271 dotptr->life = 60 * fabs(dy);
273 dotptr->dx = shipdx + 20*dx;
274 dotptr->dy = shipdy + 2*dy;
275 dotptr->life = 60 * fabs(dx);
278 if(dotptr - edot < MAXENGINEDOTS) dotptr++;
285 drawdots(SDL_Surface *s) {
288 // Create engine dots out the side we're moving from
289 for(m = 0; m<4; m++) {
290 if(jets & 1<<m) { // 'jets' is a bit field
291 new_engine_dots(80,m);
301 SDL_UnlockSurface(s);
311 // Where are our data files?
312 if(!find_files()) exit(1);
313 read_high_score_table();
316 // Initialize SDL with audio and video
317 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
319 printf ("Can't open sound, starting without it\n");
323 atexit(SDL_CloseAudio);
324 opt_sound = init_sound();
327 // Initialize with video only
328 CONDERROR(SDL_Init(SDL_INIT_VIDEO) != 0);
332 play_tune(TUNE_TITLE_PAGE);
334 // Attempt to get the required video size
335 flag = SDL_DOUBLEBUF | SDL_HWSURFACE;
336 if(opt_fullscreen) flag |= SDL_FULLSCREEN;
337 surf_screen = SDL_SetVideoMode(XSIZE,YSIZE,16,flag);
339 // Set the title bar text
340 SDL_WM_SetCaption("Variations on Rockdodger", "VoR");
342 NULLERROR(surf_screen);
344 // Set the heat color from the range 0 (cold) to 300 (blue-white)
345 for(i = 0; i<W*3; i++) {
346 heatcolor[i] = SDL_MapRGB(
348 (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?
353 NULLERROR(temp = IMG_Load(add_path("banners/variations.png")));
354 NULLERROR(surf_b_variations = SDL_DisplayFormat(temp));
356 NULLERROR(temp = IMG_Load(add_path("banners/on.png")));
357 NULLERROR(surf_b_on = SDL_DisplayFormat(temp));
359 NULLERROR(temp = IMG_Load(add_path("banners/rockdodger.png")));
360 NULLERROR(surf_b_rockdodger = SDL_DisplayFormat(temp));
362 NULLERROR(temp = IMG_Load(add_path("banners/game.png")));
363 NULLERROR(surf_b_game = SDL_DisplayFormat(temp));
365 NULLERROR(temp = IMG_Load(add_path("banners/over.png")));
366 NULLERROR(surf_b_over = SDL_DisplayFormat(temp));
368 surf_font_big = IMG_Load(add_path(BIG_FONT_FILE));
369 g_font = SFont_InitFont(surf_font_big);
371 // Load the spaceship graphic.
372 NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
373 NULLERROR(surf_ship = SDL_DisplayFormat(temp));
374 get_shape(surf_ship, &shipshape);
376 // Load the life indicator (small ship) graphic.
377 NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
378 NULLERROR(surf_life = SDL_DisplayFormat(temp));
385 // Remove the mouse cursor
387 SDL_ShowCursor(SDL_DISABLE);
399 float fadegame,fadeover;
403 // Draw a fully black background
404 SDL_FillRect(surf_screen,NULL,0);
406 // Draw the background dots
407 drawdots(surf_screen);
410 if(state == GAMEPLAY ) {
413 SDL_BlitSurface(surf_ship,NULL,surf_screen,&dest);
418 // Draw the life indicators.
419 if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER) {
420 for(i = 0; i<nships-1; i++) {
421 dest.x = (i + 1)*(surf_life->w + 10);
423 SDL_BlitSurface(surf_life, NULL, surf_screen, &dest);
428 snprintscore_line(topline, 50, score);
429 SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline);
431 // If it's game over, show the game over graphic in the dead centre
434 if(fadetimer<3.0/faderate) {
435 fadegame = fadetimer/(3.0/faderate);
440 if(fadetimer<3.0/faderate) {
442 } else if(fadetimer<6.0/faderate) {
443 fadeover = ((3.0/faderate)-fadetimer)/(6.0/faderate);
448 dest.x = (XSIZE-surf_b_game->w)/2;
449 dest.y = (YSIZE-surf_b_game->h)/2-40;
450 SDL_SetAlpha(surf_b_game, SDL_SRCALPHA, (int)(fadegame*(200 + 55*cos(fadetimer += t_frame/1.0))));
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_SetAlpha(surf_b_over, SDL_SRCALPHA, (int)(fadeover*(200 + 55*sin(fadetimer))));
456 SDL_BlitSurface(surf_b_over,NULL,surf_screen,&dest);
461 dest.x = (XSIZE-surf_b_variations->w)/2 + cos(fadetimer/6.5)*10;
462 dest.y = (YSIZE/2-surf_b_variations->h)/2 + sin(fadetimer/5.0)*10;
463 SDL_SetAlpha(surf_b_variations, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer += t_frame/2.0)));
464 SDL_BlitSurface(surf_b_variations,NULL,surf_screen,&dest);
466 dest.x = (XSIZE-surf_b_on->w)/2 + cos((fadetimer + 1.0)/6.5)*10;
467 dest.y = (YSIZE/2-surf_b_on->h)/2 + surf_b_variations->h + 20 + sin((fadetimer + 1.0)/5.0)*10;
468 SDL_SetAlpha(surf_b_on, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-1.0)));
469 SDL_BlitSurface(surf_b_on,NULL,surf_screen,&dest);
471 dest.x = (XSIZE-surf_b_rockdodger->w)/2 + cos((fadetimer + 2.0)/6.5)*10;
472 dest.y = (YSIZE/2-surf_b_rockdodger->h)/2 + surf_b_variations->h + surf_b_on->h + 40 + sin((fadetimer + 2.0)/5)*10;
473 SDL_SetAlpha(surf_b_rockdodger, SDL_SRCALPHA, (int)(200 + 55*sin(fadetimer-2.0)));
474 SDL_BlitSurface(surf_b_rockdodger,NULL,surf_screen,&dest);
476 text = "Version " VERSION;
477 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + sin(fadetimer/4.5)*10;
478 SFont_Write(surf_screen,g_font,x,YSIZE-50 + sin(fadetimer/2)*5,text);
480 text = sequence[(int)(fadetimer/40)%NSEQUENCE];
481 //text = "Press SPACE to start!";
482 x = (XSIZE-SFont_TextWidth(g_font,text))/2 + cos(fadetimer/4.5)*10;
483 SFont_Write(surf_screen,g_font,x,YSIZE-100 + cos(fadetimer/3)*5,text);
486 case HIGH_SCORE_ENTRY:
487 play_tune(TUNE_HIGH_SCORE_ENTRY);
488 if(!process_score_input()) { // done inputting name
490 // Change state to briefly show high scores page
491 state = HIGH_SCORE_DISPLAY;
494 // Write the high score table to the file
495 write_high_score_table();
497 play_tune(TUNE_TITLE_PAGE);
500 case HIGH_SCORE_DISPLAY:
501 // Display de list o high scores mon.
502 display_scores(surf_screen, 150,50);
506 ; // no action necessary
509 if(state == GAMEPLAY) {
510 bang = hit_rocks(shipx, shipy, &shipshape);
513 ms_frame = SDL_GetTicks() - ms_end;
515 if(ms_frame>200 || ms_frame<0) {
516 // We won't run at all below 5 frames per second.
517 // This also happens if we were paused, grr.
521 t_frame = opt_gamespeed * ms_frame / 50;
522 if(state == GAMEPLAY) score += ms_frame;
525 // Update the surface
526 SDL_Flip(surf_screen);
540 // Count down the game loop timer, and change state when it gets to zero or less;
542 if((state_timeout -= t_frame*3) < 0) {
545 // Create a new ship and start all over again
547 play_tune(TUNE_GAMEPLAY);
550 if(new_high_score(score)) {
552 state = HIGH_SCORE_ENTRY;
553 state_timeout = 5.0e6;
554 SDL_EnableUNICODE(1);
555 while(SDL_PollEvent(&e))
557 } else if(!keystate[SDLK_SPACE]) {
558 state = HIGH_SCORE_DISPLAY;
562 case HIGH_SCORE_DISPLAY:
564 state_timeout = 500.0;
566 case HIGH_SCORE_ENTRY:
569 state = HIGH_SCORE_DISPLAY;
570 state_timeout = 200.0;
573 ; // no action necessary
576 if(state == DEAD_PAUSE) {
580 if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) {
581 blast_radius = BLAST_RADIUS * 1.3;
584 blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
587 blast_rocks(bangx, bangy, blast_radius, fixonly);
589 if(bangx < 60) bangx = 60;
596 shipx += shipdx*t_frame;
597 shipy += shipdy*t_frame;
600 tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy);
601 screendy += tmp * t_frame/12;
602 tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx);
603 screendx += tmp * t_frame/12;
604 // taper off so we don't hit the barrier abruptly.
605 // (if we would hit in < 2 seconds, adjust to 2 seconds).
606 if(back_dist + (screendx - SCREENDXMIN)*TO_TICKS(2) < 0) {
607 screendx = SCREENDXMIN - (back_dist/TO_TICKS(2));
610 back_dist += (screendx - SCREENDXMIN)*t_frame;
611 if(opt_max_lead >= 0) back_dist = min(back_dist, opt_max_lead);
613 xscroll = screendx * t_frame;
614 yscroll = screendy * t_frame;
620 bangx += bangdx*t_frame - xscroll;
621 bangy += bangdy*t_frame - yscroll;
627 if(shipx<0 || shipx>XSIZE-surf_ship->w) {
628 // BOUNCE from left and right wall
629 shipx -= (shipdx-screendx)*t_frame;
630 shipdx = screendx - (shipdx-screendx)*opt_bounciness;
634 if(shipy<0 || shipy>YSIZE-surf_ship->h) {
635 // BOUNCE from top and bottom wall
636 shipy -= (shipdy-screendy)*t_frame;
637 shipdy = screendy - (shipdy-screendy)*opt_bounciness;
641 if(draw() && state == GAMEPLAY) {
643 play_sound(SOUND_BANG); // Play the explosion sound
644 bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
645 new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship);
646 shipdx *= 0.5; shipdy *= 0.5;
647 if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN;
650 shipdx = SCREENDXMIN; shipdy = 0;
651 state_timeout = 200.0;
657 state_timeout = DEAD_PAUSE_LENGTH;
662 keystate = SDL_GetKeyState(NULL);
665 if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) {
673 play_tune(TUNE_GAMEPLAY);
675 shipx = XSIZE/2.2; shipy = YSIZE/2;
676 shipdx = screendx; shipdy = screendy;
682 keystate = SDL_GetKeyState(NULL);
685 if(state == GAMEPLAY) {
687 if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;}
688 if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;}
689 if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;}
690 if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;}
691 if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); }
694 if(keystate[SDLK_p] | keystate[SDLK_s]) {
702 } else if(state == GAME_OVER) {
703 if(keystate[SDLK_SPACE]) {
708 if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) {
716 main(int argc, char **argv) {
718 argp_parse(&argp, argc, argv, 0, 0, 0);
721 printf ("ta: '%s'\n",initerror);