diff --git a/src/actor.c b/src/actor.c index 21a9e85..142017b 100644 --- a/src/actor.c +++ b/src/actor.c @@ -130,10 +130,12 @@ void scroll_actor(Actor *actor) } } -void move_actor_with_joypad(Actor *actor) +// Move the actor with the joypad +void move_actor_with_joypad(Actor *actor, uint8_t framecount) { // Read buttons uint8_t buttons = joypad(); + uint8_t velocity = 1; bool is_moving_x = FALSE; bool is_moving_y = FALSE; @@ -145,14 +147,20 @@ void move_actor_with_joypad(Actor *actor) { actor->is_facing_right = FALSE; is_moving_x = TRUE; - actor->next_x -= 1; + if (framecount % 2 == 0) // move every 2nd frame keeps it smooth and without stutter + { + actor->next_x -= velocity; + } actor->dx = -1; } else if (buttons & J_RIGHT) { actor->is_facing_right = TRUE; is_moving_x = TRUE; - actor->next_x += 1; + if (framecount % 2 == 0) + { + actor->next_x += velocity; + } actor->dx = 1; } else @@ -163,13 +171,19 @@ void move_actor_with_joypad(Actor *actor) if (buttons & J_UP) { is_moving_y = TRUE; - actor->next_y -= 1; + if (framecount % 2 == 0) + { + actor->next_y -= velocity; + } actor->dy = -1; } else if (buttons & J_DOWN) { is_moving_y = TRUE; - actor->next_y += 1; + if (framecount % 2 == 0) + { + actor->next_y += velocity; + } actor->dy = 1; } else diff --git a/src/actor.h b/src/actor.h index 7d6497b..8bd2902 100644 --- a/src/actor.h +++ b/src/actor.h @@ -34,6 +34,6 @@ void move_actor(Actor *actor, uint8_t x, uint8_t y); void scroll_actor(Actor *actor); -void move_actor_with_joypad(Actor *actor); +void move_actor_with_joypad(Actor *actor, uint8_t framecount); void setup_actor(Actor *actor, uint8_t sprite_id, uint8_t tile_width, uint8_t tile_height, uint8_t tilemap_start, uint8_t total_frames, bool is_facing_right, const unsigned char *tilemap, const metasprite_t *metasprite); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6e399b5..8f46044 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include "collision.h" Actor submarine; +uint8_t framecount; uint8_t two_frame_counter = 0; uint8_t two_frame_real_value = 0; @@ -22,6 +23,20 @@ uint8_t two_frame_real_value = 0; #define TILEMAP_WIDTH_IN_TILES (tilemap_WIDTH >> 3) #define TILEMAP_HEIGHT_IN_TILES (tilemap_HEIGHT >> 3) +// animation counter and the directional controls update the player's location on every 2nd frame +// from https://gbdev.gg8.se/forums/viewtopic.php?id=850 +void animcounter(uint8_t numframes) +{ + if (framecount == numframes) + { + framecount = 0; //"framecount" declared at top of file. + } + framecount++; + + // Done processing, yield CPU and wait for start of next frame + vsync(); +} + // generic frame counter function // from https://laroldsjubilantjunkyard.com/tutorial/rpg-style-movement-in-gbdk/ void update_two_frame_counter(void) @@ -153,14 +168,9 @@ void main(void) update_two_frame_counter(); animate_actor(&submarine, two_frame_counter); - // Move the actor with the joypad - move_actor_with_joypad(&submarine); + move_actor_with_joypad(&submarine, framecount); // joypad_input(&submarine); - // Wait for next frame - wait_vbl_done(); - - // Done processing, yield CPU and wait for start of next frame - vsync(); + animcounter(96); // I picked 96 because of how many factors it has. Easy to have different spaced timings that don't stutter when the counter loops. } } \ No newline at end of file