Move frame counter out of sprite component

This allows individual animation
scene_man
En Yi 2023-11-04 20:32:29 +08:00
parent 97f7afc401
commit 0c540d5053
10 changed files with 18 additions and 18 deletions

View File

@ -31,6 +31,7 @@ int main(void)
add_font(&assets, "testfont", "res/test_font.ttf");
Font* fnt = get_font(&assets, "testfont");
int current_frame = 0;
while(!WindowShouldClose())
{
if (IsKeyReleased(KEY_C))
@ -43,16 +44,16 @@ int main(void)
DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED);
// Draw the static Sprite and animated Sprite
draw_sprite(spr, (Vector2){64,128}, 0.0f, false);
draw_sprite(spr2, (Vector2){64,180}, 0.0f, true);
draw_sprite(spr, 0, (Vector2){64,128}, 0.0f, false);
draw_sprite(spr2, current_frame, (Vector2){64,180}, 0.0f, true);
EndDrawing();
// Update the animated Sprite
spr2->elapsed++;
if (spr2->elapsed == spr2->speed)
{
spr2->current_frame++;
spr2->current_frame %= spr2->frame_count;
current_frame++;
current_frame %= spr2->frame_count;
spr2->elapsed = 0;
}
}

View File

@ -185,7 +185,6 @@ typedef struct Sprite {
Vector2 origin;
Vector2 anchor;
int frame_count;
int current_frame;
int elapsed;
int speed;
char* name;
@ -205,6 +204,7 @@ typedef struct _CSprite_t {
bool flip_x;
bool flip_y;
bool pause;
int current_frame;
} CSprite_t;
typedef struct _CMoveable_t {

View File

@ -461,10 +461,10 @@ LevelPack_t* get_level_pack(Assets_t* assets, const char* name)
return NULL;
}
void draw_sprite(Sprite_t* spr, Vector2 pos, float rotation, bool flip_x)
void draw_sprite(Sprite_t* spr, int frame_num, Vector2 pos, float rotation, bool flip_x)
{
Rectangle rec = {
spr->origin.x + spr->frame_size.x * spr->current_frame,
spr->origin.x + spr->frame_size.x * frame_num,
spr->origin.y,
spr->frame_size.x * (flip_x ? -1:1),
spr->frame_size.y

View File

@ -68,7 +68,7 @@ Sound* get_sound(Assets_t* assets, const char* name);
Font* get_font(Assets_t* assets, const char* name);
LevelPack_t* get_level_pack(Assets_t* assets, const char* name);
void draw_sprite(Sprite_t* spr, Vector2 pos, float rotation, bool flip_x);
void draw_sprite(Sprite_t* spr, int frame_num, Vector2 pos, float rotation, bool flip_x);
typedef struct SFX
{

View File

@ -137,7 +137,7 @@ void draw_particle_system(ParticleSystem_t* system)
}
else
{
draw_sprite(emitter->spr, part->position, part->rotation, false);
draw_sprite(emitter->spr, 0, part->position, part->rotation, false);
}
}
}

View File

@ -58,7 +58,6 @@ int main(void)
.origin = (Vector2){0, 0},
.anchor = (Vector2){tex.width / 2, tex.height / 2},
.frame_count = 0,
.current_frame = 0,
.elapsed = 0,
.speed = 0,
.name = "test_spr"

View File

@ -225,7 +225,7 @@ static void render_editor_game_scene(Scene_t* scene)
uint8_t tile_sprite_idx = tilemap.tiles[i].tile_type + tilemap.tiles[i].rotation;
if (data->tile_sprites[tile_sprite_idx] != NULL)
{
draw_sprite(data->tile_sprites[tile_sprite_idx], (Vector2){x,y}, 0.0f, false);
draw_sprite(data->tile_sprites[tile_sprite_idx], 0, (Vector2){x,y}, 0.0f, false);
}
else if (tilemap.tiles[i].tile_type == SOLID_TILE)
{
@ -429,7 +429,7 @@ static void render_editor_game_scene(Scene_t* scene)
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, spr.offset);
draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x);
draw_sprite(spr.sprite, p_cspr->current_frame, pos, 0.0f, p_cspr->flip_x);
}
}
}

View File

@ -131,7 +131,7 @@ static void render_regular_game_scene(Scene_t* scene)
if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL)
{
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, 0.0f, false);
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], 0, (Vector2){x,y}, 0.0f, false);
}
else if (tilemap.tiles[i].tile_type == SOLID_TILE)
{
@ -223,7 +223,7 @@ static void render_regular_game_scene(Scene_t* scene)
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, spr.offset);
draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x);
draw_sprite(spr.sprite, p_cspr->current_frame, pos, 0.0f, p_cspr->flip_x);
}
continue;
}

View File

@ -1898,14 +1898,14 @@ void sprite_animation_system(Scene_t* scene)
SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
if (spr.sprite == NULL) continue;
if (reset) spr.sprite->current_frame = 0;
if (reset) p_cspr->current_frame = 0;
// Animate it (handle frame count)
spr.sprite->elapsed++;
if (spr.sprite->elapsed == spr.sprite->speed)
{
spr.sprite->current_frame++;
spr.sprite->current_frame %= spr.sprite->frame_count;
p_cspr->current_frame++;
p_cspr->current_frame %= spr.sprite->frame_count;
spr.sprite->elapsed = 0;
}
}

View File

@ -109,7 +109,7 @@ static void level_scene_render_func(Scene_t* scene)
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, spr.offset);
draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x);
draw_sprite(spr.sprite, p_cspr->current_frame, pos, 0.0f, p_cspr->flip_x);
}
}
}