X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=main.c;h=5ac2b5e7b6af905ccfbffcce6aa8a0c4c1fe8665;hp=b4972a22dc89749f122e87f19de7015f76fa783d;hb=45925ff114a17b9c1c0a720ff668e2cb795cd761;hpb=a77978e0c9bc91ea3d7b192ce136621e78d4ec5b diff --git a/main.c b/main.c index b4972a2..5ac2b5e 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,8 @@ #include #include -#include -#include +#include +#include #include #include #include @@ -39,7 +39,7 @@ #include "mt.h" #include "rocks.h" #include "score.h" -#include "shape.h" +#include "sprite.h" #include "sound.h" // ************************************* VARS @@ -51,7 +51,6 @@ SDL_Surface *surf_b_rockdodger, // "rockdodger" banner *surf_b_game, // Title element "game" *surf_b_over, // Title element "over" - *surf_ship, // Spaceship element *surf_life, // Indicator of number of ships remaining *surf_rock[NROCKS], // THE ROCKS *surf_font_big; // The big font @@ -67,9 +66,8 @@ struct bangdots bdot[MAXBANGDOTS], *bdotptr = bdot; char topline[1024]; char *initerror = ""; -struct shape shipshape; -float shipx = XSIZE/2, shipy = YSIZE/2; // X position, 0..XSIZE -float shipdx = SCREENDXMIN, shipdy = 0.0; // Change in X position per tick. +struct ship ship = { SHIP, 0, NULL, XSIZE/2, YSIZE/2, SCREENDXMIN, 0.0 }; + float screendx = SCREENDXMIN, screendy = 0.0; float xscroll, yscroll; float back_dist; @@ -79,10 +77,10 @@ float t_frame; // length of this frame (in ticks = 1/20th second) int ms_frame; // length of this frame (milliseconds) int ms_end; // end of this frame (milliseconds) +int bang = false; float bangx, bangy, bangdx, bangdy; -int nships,score; -int jets = 0; +int score; float fadetimer = 0, faderate; @@ -105,7 +103,7 @@ float state_timeout = 600.0; #define NSEQUENCE 2 char *sequence[] = { "Press SPACE to start", - "http://herkamire.com/jason/vor" + "http://jasonwoof.org/vor" }; int bangdotlife, nbangdots; @@ -206,7 +204,7 @@ draw_bang_dots(SDL_Surface *s) } // check collisions - if(pixel_hit_rocks(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; } + if(pixel_collides(bdot[i].x, bdot[i].y)) { bdot[i].active = 0; continue; } pixel = pixels + row_inc*(int)(bdot[i].y) + (int)(bdot[i].x); if(bdot[i].c) c = bdot[i].c; else c = heatcolor[(int)(bdot[i].life)*3]; @@ -223,8 +221,8 @@ new_engine_dots(int n, int dir) { float hx, hy; // half ship width/height. static const int s[4] = { 2, 1, 0, 1 }; - hx = surf_ship->w / 2; - hy = surf_ship->h / 2; + hx = ship.image->w / 2; + hy = ship.image->h / 2; for(i = 0; iactive == 0) { @@ -234,15 +232,15 @@ new_engine_dots(int n, int dir) { dy = r * -sin(a); // screen y is "backwards". dotptr->active = 1; - dotptr->x = shipx + s[dir]*hx + (frnd()-0.5)*3; - dotptr->y = shipy + s[(dir+1)&3]*hy + (frnd()-0.5)*3; + dotptr->x = ship.x + s[dir]*hx + (frnd()-0.5)*3; + dotptr->y = ship.y + s[(dir+1)&3]*hy + (frnd()-0.5)*3; if(dir&1) { - dotptr->dx = shipdx + 2*dx; - dotptr->dy = shipdy + 20*dy; + dotptr->dx = ship.dx + 2*dx; + dotptr->dy = ship.dy + 20*dy; dotptr->life = 60 * fabs(dy); } else { - dotptr->dx = shipdx + 20*dx; - dotptr->dy = shipdy + 2*dy; + dotptr->dx = ship.dx + 20*dx; + dotptr->dy = ship.dy + 2*dy; dotptr->life = 60 * fabs(dx); } @@ -271,7 +269,7 @@ draw_engine_dots(SDL_Surface *s) { edot[i].active = 0; continue; } - if(pixel_hit_rocks(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; } + if(pixel_collides(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; } heatindex = edot[i].life * 6; c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex]; pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c; @@ -284,7 +282,7 @@ drawdots(SDL_Surface *s) { // Create engine dots out the side we're moving from for(m = 0; m<4; m++) { - if(jets & 1<w + 10); + dest.y = 20; + SDL_BlitSurface(surf_life, NULL, surf_screen, &dest); } +} - draw_rocks(); +void +draw(void) { + SDL_Rect dest; + int x; + char *text; + float fadegame,fadeover; - // Draw the life indicators. - if(state == GAMEPLAY || state == DEAD_PAUSE || state == GAME_OVER) { - for(i = 0; iw + 10); - dest.y = 20; - SDL_BlitSurface(surf_life, NULL, surf_screen, &dest); - } - } + SDL_FillRect(surf_screen,NULL,0); // black background + drawdots(surf_screen); // background dots + draw_sprite(SPRITE(&ship)); + draw_rocks(); - // Draw the score - snprintscore_line(topline, 50, score); - SFont_Write(surf_screen, g_font, XSIZE-250, 0, topline); + show_lives(); + show_score(); // If it's game over, show the game over graphic in the dead centre switch (state) { @@ -503,43 +505,51 @@ draw() { ; // no action necessary } - if(state == GAMEPLAY) { - bang = hit_rocks(shipx, shipy, &shipshape); - } + collisions(); ms_frame = SDL_GetTicks() - ms_end; ms_end += ms_frame; - if(ms_frame>200 || ms_frame<0) { - // We won't run at all below 5 frames per second. - // This also happens if we were paused, grr. - t_frame = 0; - ms_frame = 0; - } else { - t_frame = opt_gamespeed * ms_frame / 50; - if(state == GAMEPLAY) score += ms_frame; - } + t_frame = opt_gamespeed * ms_frame / 50; + if(state == GAMEPLAY) score += ms_frame; // Update the surface SDL_Flip(surf_screen); +} +static inline void +kill_ship(Sprite *ship) +{ + ship->flags = MOVE|DRAW; + SDL_SetAlpha(ship->image, SDL_SRCALPHA, 0); + bang = true; +} - return bang; +void +do_collision(Sprite *a, Sprite *b) +{ + if(a->type == SHIP) kill_ship(a); + else if (b->type == SHIP) kill_ship(b); + else bounce(a, b); } -int +void gameloop() { - Uint8 *keystate; + Uint8 *keystate = SDL_GetKeyState(NULL); float tmp; for(;;) { + SDL_PumpEvents(); + keystate = SDL_GetKeyState(NULL); + if(!paused) { // Count down the game loop timer, and change state when it gets to zero or less; if((state_timeout -= t_frame*3) < 0) { switch(state) { case DEAD_PAUSE: - // Create a new ship and start all over again + // Restore the ship and continue playing + ship.flags = DRAW|MOVE|COLLIDE; state = GAMEPLAY; play_tune(TUNE_GAMEPLAY); break; @@ -551,7 +561,7 @@ gameloop() { SDL_EnableUNICODE(1); while(SDL_PollEvent(&e)) ; - } else if(!keystate[SDLK_SPACE]) { + } else { state = HIGH_SCORE_DISPLAY; state_timeout = 400; } @@ -571,32 +581,25 @@ gameloop() { } } else { if(state == DEAD_PAUSE) { - float blast_radius; - int fixonly; - - if(state_timeout < DEAD_PAUSE_LENGTH - 20.0) { - blast_radius = BLAST_RADIUS * 1.3; - fixonly = 1; - } else { + float blast_radius, alpha; + if(state_timeout >= DEAD_PAUSE_LENGTH - 20.0) { blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0; - fixonly = 0; + blast_rocks(bangx, bangy, blast_radius); } - blast_rocks(bangx, bangy, blast_radius, fixonly); if(bangx < 60) bangx = 60; + + alpha = 255.0 * (DEAD_PAUSE_LENGTH - state_timeout) / DEAD_PAUSE_LENGTH; + SDL_SetAlpha(ship.image, SDL_SRCALPHA, (uint8_t)alpha); } } new_rocks(); - // INERTIA - shipx += shipdx*t_frame; - shipy += shipdy*t_frame; - // SCROLLING - tmp = (shipy-YSCROLLTO)/25 + (shipdy-screendy); + tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy); screendy += tmp * t_frame/12; - tmp = (shipx-XSCROLLTO)/25 + (shipdx-screendx); + tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx); screendx += tmp * t_frame/12; // taper off so we don't hit the barrier abruptly. // (if we would hit in < 2 seconds, adjust to 2 seconds). @@ -610,81 +613,77 @@ gameloop() { xscroll = screendx * t_frame; yscroll = screendy * t_frame; - shipx -= xscroll; - shipy -= yscroll; - // move bang center bangx += bangdx*t_frame - xscroll; bangy += bangdy*t_frame - yscroll; - move_rocks(); + move_sprites(); - // BOUNCE X - if(shipx<0 || shipx>XSIZE-surf_ship->w) { - // BOUNCE from left and right wall - shipx -= (shipdx-screendx)*t_frame; - shipdx = screendx - (shipdx-screendx)*opt_bounciness; + // BOUNCE off left or right edge of screen + if(ship.x < 0 || ship.x+ship.w > XSIZE) { + ship.x -= (ship.dx-screendx)*t_frame; + ship.dx = screendx - (ship.dx-screendx)*opt_bounciness; } - // BOUNCE Y - if(shipy<0 || shipy>YSIZE-surf_ship->h) { - // BOUNCE from top and bottom wall - shipy -= (shipdy-screendy)*t_frame; - shipdy = screendy - (shipdy-screendy)*opt_bounciness; + // BOUNCE off top or bottom of screen + if(ship.y < 0 || ship.y+ship.h > YSIZE) { + ship.y -= (ship.dy-screendy)*t_frame; + ship.dy = screendy - (ship.dy-screendy)*opt_bounciness; } + draw(); - if(draw() && state == GAMEPLAY) { + if(state == GAMEPLAY && bang) { // Died + bang = false; play_sound(SOUND_BANG); // Play the explosion sound - bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy; - new_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship); - shipdx *= 0.5; shipdy *= 0.5; - if(shipdx < SCREENDXMIN) shipdx = SCREENDXMIN; - if(--nships <= 0) { + bangx = ship.x; bangy = ship.y; bangdx = ship.dx; bangdy = ship.dy; + new_bang_dots(ship.x,ship.y,ship.dx,ship.dy,ship.image); + ship.dx *= 0.5; ship.dy *= 0.5; + if(ship.dx < SCREENDXMIN) ship.dx = SCREENDXMIN; + if(--ship.lives <= 0) { state = GAME_OVER; - shipdx = SCREENDXMIN; shipdy = 0; + ship.dx = SCREENDXMIN; ship.dy = 0; state_timeout = 200.0; fadetimer = 0.0; faderate = t_frame; - } - else { + } else { state = DEAD_PAUSE; state_timeout = DEAD_PAUSE_LENGTH; } } - SDL_PumpEvents(); - keystate = SDL_GetKeyState(NULL); - // new game - if(keystate[SDLK_SPACE] && (state == HIGH_SCORE_DISPLAY || state == TITLE_PAGE)) { - + if(keystate[SDLK_SPACE] + && (state == HIGH_SCORE_DISPLAY + || state == TITLE_PAGE + || state == GAME_OVER)) { + reset_sprites(); reset_rocks(); - nships = 4; + ship.x = XSIZE/2.2; ship.y = YSIZE/2; + ship.dx = screendx; ship.dy = screendy; + ship.lives = 4; + ship.flags = MOVE|DRAW|COLLIDE; + SDL_SetAlpha(ship.image, SDL_SRCALPHA, SDL_ALPHA_OPAQUE); + add_sprite(SPRITE(&ship)); + score = 0; state = GAMEPLAY; play_tune(TUNE_GAMEPLAY); - - shipx = XSIZE/2.2; shipy = YSIZE/2; - shipdx = screendx; shipdy = screendy; } - jets = 0; - } else { - SDL_PumpEvents(); - keystate = SDL_GetKeyState(NULL); + ship.jets = 0; } - if(state == GAMEPLAY) { + if(state == GAMEPLAY || state == DEAD_PAUSE) { if(!paused) { - if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { shipdx -= 1.5*t_frame; jets |= 1<<0;} - if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { shipdy += 1.5*t_frame; jets |= 1<<1;} - if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { shipdx += 1.5*t_frame; jets |= 1<<2;} - if(keystate[SDLK_UP] | keystate[SDLK_c]) { shipdy -= 1.5*t_frame; jets |= 1<<3;} + if(keystate[SDLK_LEFT] | keystate[SDLK_h]) { ship.dx -= 1.5*t_frame; ship.jets |= 1<<0;} + if(keystate[SDLK_DOWN] | keystate[SDLK_t]) { ship.dy += 1.5*t_frame; ship.jets |= 1<<1;} + if(keystate[SDLK_RIGHT] | keystate[SDLK_n]) { ship.dx += 1.5*t_frame; ship.jets |= 1<<2;} + if(keystate[SDLK_UP] | keystate[SDLK_c]) { ship.dy -= 1.5*t_frame; ship.jets |= 1<<3;} if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); } } @@ -692,19 +691,15 @@ gameloop() { if(!pausedown) { paused = !paused; pausedown = 1; + if(!paused) ms_end = SDL_GetTicks(); } } else { pausedown = 0; } - } else if(state == GAME_OVER) { - if(keystate[SDLK_SPACE]) { - state_timeout = -1; - } } - if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) { - return 0; - } + if(state != HIGH_SCORE_ENTRY && (keystate[SDLK_q] || keystate[SDLK_ESCAPE])) + return; } }