X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=main.c;h=f35829273499c3af52139d8a734045d3639f9a01;hp=e3a0f8351da7158bb9e2fa5a7ae400a4288f6144;hb=da1b718e14900f7db4d94501a8fa22b24be097e7;hpb=a726e74233c877dab3aca50e5b31e7652035ae30 diff --git a/main.c b/main.c index e3a0f83..f358292 100644 --- a/main.c +++ b/main.c @@ -180,28 +180,21 @@ new_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s) } void -draw_bang_dots(SDL_Surface *s) +move_bang_dots(float ticks) { int i; - int first_i, last_i; - uint16_t *pixels, *pixel, c; - int row_inc = s->pitch/sizeof(uint16_t); Sprite *hit; - pixels = (uint16_t *) s->pixels; - first_i = -1; - last_i = 0; - for(i=0; i= XSIZE || bdot[i].y < 0 || bdot[i].y >= YSIZE) { bdot[i].active = 0; continue; @@ -216,6 +209,21 @@ draw_bang_dots(SDL_Surface *s) continue; } } + } +} + + +void +draw_bang_dots(SDL_Surface *s) +{ + int i; + uint16_t *pixels, *pixel, c; + int row_inc = s->pitch/sizeof(uint16_t); + + pixels = (uint16_t *) s->pixels; + + for(i=0; iw / 2; hy = ship.image->h / 2; - for(i = 0; iactive == 0) { - a = frnd()*M_PI + (dir-1)*M_PI_2; - r = sin(frnd()*M_PI); - dx = r * cos(a); - dy = r * -sin(a); // screen y is "backwards". - - dotptr->active = 1; - - // dot was created at a random time during the time span - time = frnd() * time_span; // this is how long ago - - // calculate how fast the ship was going when this engine dot was - // created (as if it had a smoothe accelleration). This is used in - // determining the velocity of the dots, but not their starting - // location. - accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1); - accelh *= THRUSTER_STRENGTH * time; - past_ship_dx = ship.dx - accelh; - accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1); - accelv *= THRUSTER_STRENGTH * time; - past_ship_dy = ship.dy - accelv; - - // the starting position (not speed) of the dot is calculated as - // though the ship were traveling at a constant speed for this - // time_span. - dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx; - dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy; - if(dir&1) { - dotptr->dx = past_ship_dx + 2*dx; - dotptr->dy = past_ship_dy + 20*dy; - dotptr->life = 60 * fabs(dy); - } else { - dotptr->dx = past_ship_dx + 20*dx; - dotptr->dy = past_ship_dy + 2*dy; - dotptr->life = 60 * fabs(dx); - } + for(dir=0; dir<4; dir++) { + if(!(ship.jets & 1<active == 0) { + a = frnd()*M_PI + (dir-1)*M_PI_2; + r = sin(frnd()*M_PI); + dx = r * cos(a); + dy = r * -sin(a); // screen y is "backwards". + + dotptr->active = 1; + + // dot was created at a random time during the time span + time = frnd() * time_span; // this is how long ago + + // calculate how fast the ship was going when this engine dot was + // created (as if it had a smooth acceleration). This is used in + // determining the velocity of the dots, but not their starting + // location. + accelh = ((ship.jets >> 2) & 1) - (ship.jets & 1); + accelh *= THRUSTER_STRENGTH * time; + past_ship_dx = ship.dx - accelh; + accelv = ((ship.jets >> 1) & 1) - ((ship.jets >> 3) & 1); + accelv *= THRUSTER_STRENGTH * time; + past_ship_dy = ship.dy - accelv; + + // the starting position (not speed) of the dot is calculated as + // though the ship were traveling at a constant speed for this + // time_span. + dotptr->x = (ship.x - (ship.dx - screendx) * time) + s[dir]*hx; + dotptr->y = (ship.y - (ship.dy - screendy) * time) + s[(dir+1)&3]*hy; + if(dir&1) { + dotptr->dx = past_ship_dx + 2*dx; + dotptr->dy = past_ship_dy + 20*dy; + dotptr->life = 60 * fabs(dy); + } else { + dotptr->dx = past_ship_dx + 20*dx; + dotptr->dy = past_ship_dy + 2*dy; + dotptr->life = 60 * fabs(dx); + } - // move the dot as though it were created in the past - dotptr->x += (dotptr->dx - screendx) * time; - dotptr->y += (dotptr->dy - screendy) * time; + // move the dot as though it were created in the past + dotptr->x += (dotptr->dx - screendx) * time; + dotptr->y += (dotptr->dy - screendy) * time; - if(dotptr - edot < MAXENGINEDOTS-1) dotptr++; - else dotptr = edot; + if(dotptr - edot < MAXENGINEDOTS-1) dotptr++; + else dotptr = edot; + } } } } void -move_engine_dots() { +move_engine_dots(float ticks) { int i; + Sprite *hit; + for(i = 0; i= XSIZE || edot[i].y < 0 || edot[i].y >= YSIZE) { edot[i].active = 0; } + + // check collisions + if((hit = pixel_collides(edot[i].x, edot[i].y))) { + if(hit->type != SHIP) { // they shouldn't hit the ship, but they do + edot[i].active = 0; + hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit); + hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit); + continue; + } + } } } @@ -309,21 +332,10 @@ draw_engine_dots(SDL_Surface *s) { uint16_t *pixels = (uint16_t *) s->pixels; int row_inc = s->pitch/sizeof(uint16_t); int heatindex; - Sprite *hit; for(i = 0; itype != SHIP) { // they shouldn't hit the ship, but they do - edot[i].active = 0; - hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit); - hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit); - continue; - } - } + if(!edot[i].active) 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; @@ -331,20 +343,7 @@ draw_engine_dots(SDL_Surface *s) { } void -drawdots(SDL_Surface *s) { - int m; - - move_engine_dots(); - - // Create engine dots - for(m = 0; m<4; m++) { - if(ship.jets & 1< 0) return; + + switch(state) { + case DEAD_PAUSE: + // Restore the ship and continue playing + ship.flags = DRAW|MOVE|COLLIDE; + state = GAMEPLAY; + play_tune(TUNE_GAMEPLAY); + break; + case GAME_OVER: + if(new_high_score(score)) init_score_entry(); + else { + state = HIGH_SCORE_DISPLAY; + state_timeout = 400; + } + break; + case HIGH_SCORE_DISPLAY: + state = TITLE_PAGE; + state_timeout = 600.0; + fadetimer = 0.0; + break; + case HIGH_SCORE_ENTRY: + break; + case TITLE_PAGE: + state = HIGH_SCORE_DISPLAY; + state_timeout = 200.0; + break; + case GAMEPLAY: + ; // no action necessary + } +} + void gameloop() { SDL_Event e; Uint8 *keystate; float tmp; - for(;;) { + ms_frame = SDL_GetTicks() - ms_end; + ms_end += ms_frame; + t_frame = opt_gamespeed * ms_frame / 50; + while(SDL_PollEvent(&e)) { switch(e.type) { case SDL_QUIT: return; @@ -638,50 +672,37 @@ gameloop() { } 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: - // Restore the ship and continue playing - ship.flags = DRAW|MOVE|COLLIDE; - state = GAMEPLAY; - play_tune(TUNE_GAMEPLAY); - break; - case GAME_OVER: - if(new_high_score(score)) init_score_entry(); - else { - state = HIGH_SCORE_DISPLAY; - state_timeout = 400; - } - break; - case HIGH_SCORE_DISPLAY: - state = TITLE_PAGE; - state_timeout = 600.0; - fadetimer = 0.0; - break; - case HIGH_SCORE_ENTRY: - break; - case TITLE_PAGE: - state = HIGH_SCORE_DISPLAY; - state_timeout = 200.0; - break; - case GAMEPLAY: - ; // no action necessary + if(state == GAMEPLAY) { + if(!paused) { + score += ms_frame; + + if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;} + if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;} + if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;} + if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;} + if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); } + } + + if(keystate[SDLK_p] | keystate[SDLK_s]) { + if(!pausedown) { + paused = !paused; + pausedown = 1; + if(!paused) ms_end = SDL_GetTicks(); } } else { - if(state == DEAD_PAUSE) { - if(bangx < 60) bangx = 60; - } + pausedown = 0; } + } + + if(!paused) { + update_state(t_frame); - new_rocks(); + if(state == DEAD_PAUSE && bangx < 60) bangx = 60; // SCROLLING - tmp = (ship.y+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy); + tmp = (ship.y+ship.h/2+ship.dy*t_frame-YSCROLLTO)/25 + (ship.dy-screendy); screendy += tmp * t_frame/12; - tmp = (ship.x+ship.dx*t_frame-XSCROLLTO)/25 + (ship.dx-screendx); + tmp = (ship.x+ship.w/2+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). @@ -694,8 +715,10 @@ gameloop() { bangx += (bangdx - screendx)*t_frame; bangy += (bangdy - screendy)*t_frame; - move_sprites(); - + move_sprites(t_frame); + move_engine_dots(t_frame); + move_bang_dots(t_frame); + move_dust(t_frame); // BOUNCE off left or right edge of screen if(ship.x < 0 || ship.x+ship.w > XSIZE) { @@ -709,6 +732,9 @@ gameloop() { ship.dy = screendy - (ship.dy-screendy)*opt_bounciness; } + new_rocks(t_frame); + new_engine_dots(t_frame); + draw(); if(state == GAMEPLAY && bang) { @@ -757,7 +783,7 @@ gameloop() { reset_rocks(); screendx = SCREENDXMIN; screendy = 0; - ship.x = XSIZE/2.2; ship.y = YSIZE/2; + ship.x = XSIZE/2.2; ship.y = YSIZE/2 - ship.w/2; ship.dx = screendx; ship.dy = screendy; ship.lives = 4; ship.flags = MOVE|DRAW|COLLIDE; @@ -773,27 +799,6 @@ gameloop() { ship.jets = 0; } - if(state == GAMEPLAY) { - if(!paused) { - // FIXME why is this at the bottom? Shouldn't it be up before the ship movement? - if(keystate[SDLK_LEFT] || keystate[SDLK_h]) { ship.dx -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<0;} - if(keystate[SDLK_DOWN] || keystate[SDLK_t]) { ship.dy += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<1;} - if(keystate[SDLK_RIGHT] || keystate[SDLK_n]) { ship.dx += THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<2;} - if(keystate[SDLK_UP] || keystate[SDLK_c]) { ship.dy -= THRUSTER_STRENGTH*t_frame; ship.jets |= 1<<3;} - if(keystate[SDLK_3]) { SDL_SaveBMP(surf_screen, "snapshot.bmp"); } - } - - if(keystate[SDLK_p] | keystate[SDLK_s]) { - if(!pausedown) { - paused = !paused; - pausedown = 1; - if(!paused) ms_end = SDL_GetTicks(); - } - } else { - pausedown = 0; - } - } - if(state == TITLE_PAGE && keystate[SDLK_h]) { state = HIGH_SCORE_DISPLAY; state_timeout = 400;