From 0f25d9fb178e9fb32793331aa1ed724d9747702d Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Sat, 23 Aug 2025 08:58:57 +0300 Subject: Animation System Complete --- include/game.h | 2 +- include/utils.h | 4 ++++ src/background.c | 16 ++++++++++------ src/game.c | 22 +++++++++++++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/include/game.h b/include/game.h index 530ae73..ec44b3e 100644 --- a/include/game.h +++ b/include/game.h @@ -85,7 +85,7 @@ void UpdateGame(float deltaTime); void DrawGame(); void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt); -void DrawEntitySprite(const Sprite* sprite, int spriteIndex); +void DrawEntitySprite(const Entity* e, int spriteIndex); #ifdef BEATEMUP_DEBUG void DebugHighlights(const Entity* e); diff --git a/include/utils.h b/include/utils.h index 59c1bc4..0ba8c32 100644 --- a/include/utils.h +++ b/include/utils.h @@ -52,4 +52,8 @@ static inline Animation* GetCurrentAnimation(Sprite* s) { return &s->animations[s->currentAnimation]; } +static inline Rectangle GetCurrentSourceRectangle(const Animation* animation) { + return animation->srcRects[animation->currentFrame]; +} + #endif // UTILS_H_ diff --git a/src/background.c b/src/background.c index c9ed1fb..d3647cf 100644 --- a/src/background.c +++ b/src/background.c @@ -7,8 +7,8 @@ void AddBackground(const char* backgroundTextureName) { Entity e = {0}; - e.type = Background_Entity; + e.type = Background_Entity; e.flags |= ENTITY_VISIBLE; e.position = (Vector2) {0.0f, 0.0f}; @@ -32,11 +32,15 @@ void AddBackground(const char* backgroundTextureName) { Vector2 backgroundBounds = Vector2Scale((Vector2) {backgroundTexture.width, backgroundTexture.height}, backgroundSizeScale); - AddSpriteToEntity(&e, (Sprite){ - .texture = backgroundTexture, - .layer = Background_Layer, - .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y} - }); + Sprite backgroundSprite = (Sprite) { + .texture = backgroundTexture, + .layer = Background_Layer, + .numAnimations = 0, + + .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y} + }; + + AddSpriteToEntity(&e, backgroundSprite); AddEntity(&e); } diff --git a/src/game.c b/src/game.c index 8659939..b15c85f 100644 --- a/src/game.c +++ b/src/game.c @@ -68,8 +68,21 @@ void UpdateGame(float deltaTime) { void DrawEntitySprite(const Entity* e, int spriteIndex) { const Sprite* drawnSprite = &e->sprites[spriteIndex]; - Rectangle srcRect = - {0, 0, drawnSprite->texture.width, drawnSprite->texture.height}; + Rectangle srcRect; + if (!IsAnimated(drawnSprite)) { + srcRect = (Rectangle) { + .x = 0, + .y = 0, + .width = drawnSprite->texture.width, + .height = drawnSprite->texture.height + }; + } else { + const Animation* currentAnimation = + &drawnSprite->animations[drawnSprite->currentAnimation]; + + srcRect = GetCurrentSourceRectangle(currentAnimation); + } + Vector2 origin = {0.0f, 0.0f}; float rotation = 0.0f; Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex); @@ -77,6 +90,7 @@ void DrawEntitySprite(const Entity* e, int spriteIndex) { DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, WHITE); } + void DefaultDrawEntity(const Entity* e, DrawLayer toLayer) { for (int i = 0; i < e->numSprites; i++) { if (e->sprites[i].layer == toLayer) @@ -163,7 +177,9 @@ void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) { if (currentAnimation->currentFrameTimer >= GetCurrentFrameTime(currentAnimation)) { currentAnimation->currentFrame++; - currentAnimation->currentFrameTimer = 0; + + currentAnimation->currentFrameTimer = + currentAnimation->currentFrameTimer - GetCurrentFrameTime(currentAnimation); } if (currentAnimation->currentFrame >= currentAnimation->numFrames) { -- cgit v1.2.3