add framecounter

add framecounter to be able to slow down movement and possibly other stuff
This commit is contained in:
Norman Bos
2024-04-10 00:14:59 +02:00
parent 72ff83fde4
commit 874177fc94
3 changed files with 37 additions and 13 deletions
+19 -5
View File
@@ -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 // Read buttons
uint8_t buttons = joypad(); uint8_t buttons = joypad();
uint8_t velocity = 1;
bool is_moving_x = FALSE; bool is_moving_x = FALSE;
bool is_moving_y = FALSE; bool is_moving_y = FALSE;
@@ -145,14 +147,20 @@ void move_actor_with_joypad(Actor *actor)
{ {
actor->is_facing_right = FALSE; actor->is_facing_right = FALSE;
is_moving_x = TRUE; 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; actor->dx = -1;
} }
else if (buttons & J_RIGHT) else if (buttons & J_RIGHT)
{ {
actor->is_facing_right = TRUE; actor->is_facing_right = TRUE;
is_moving_x = TRUE; is_moving_x = TRUE;
actor->next_x += 1; if (framecount % 2 == 0)
{
actor->next_x += velocity;
}
actor->dx = 1; actor->dx = 1;
} }
else else
@@ -163,13 +171,19 @@ void move_actor_with_joypad(Actor *actor)
if (buttons & J_UP) if (buttons & J_UP)
{ {
is_moving_y = TRUE; is_moving_y = TRUE;
actor->next_y -= 1; if (framecount % 2 == 0)
{
actor->next_y -= velocity;
}
actor->dy = -1; actor->dy = -1;
} }
else if (buttons & J_DOWN) else if (buttons & J_DOWN)
{ {
is_moving_y = TRUE; is_moving_y = TRUE;
actor->next_y += 1; if (framecount % 2 == 0)
{
actor->next_y += velocity;
}
actor->dy = 1; actor->dy = 1;
} }
else else
+1 -1
View File
@@ -34,6 +34,6 @@ void move_actor(Actor *actor, uint8_t x, uint8_t y);
void scroll_actor(Actor *actor); 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); 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);
+17 -7
View File
@@ -15,6 +15,7 @@
#include "collision.h" #include "collision.h"
Actor submarine; Actor submarine;
uint8_t framecount;
uint8_t two_frame_counter = 0; uint8_t two_frame_counter = 0;
uint8_t two_frame_real_value = 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_WIDTH_IN_TILES (tilemap_WIDTH >> 3)
#define TILEMAP_HEIGHT_IN_TILES (tilemap_HEIGHT >> 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 // generic frame counter function
// from https://laroldsjubilantjunkyard.com/tutorial/rpg-style-movement-in-gbdk/ // from https://laroldsjubilantjunkyard.com/tutorial/rpg-style-movement-in-gbdk/
void update_two_frame_counter(void) void update_two_frame_counter(void)
@@ -153,14 +168,9 @@ void main(void)
update_two_frame_counter(); update_two_frame_counter();
animate_actor(&submarine, two_frame_counter); animate_actor(&submarine, two_frame_counter);
// Move the actor with the joypad move_actor_with_joypad(&submarine, framecount);
move_actor_with_joypad(&submarine);
// joypad_input(&submarine); // joypad_input(&submarine);
// Wait for next frame 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.
wait_vbl_done();
// Done processing, yield CPU and wait for start of next frame
vsync();
} }
} }