diff options
author | BoredGuy <osome3717@gmail.com> | 2025-08-21 20:07:04 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-08-21 20:07:04 +0300 |
commit | 8a8ed6d3fa7059dbb2a95072bbae4bf4618349a0 (patch) | |
tree | 5f1383eb3983b2bfc73680322a4bf0b68abbf770 | |
parent | 251be1ac2d808dfd0fca5c0eb37398357ca7bb20 (diff) |
Work on animation subsystem
- Also incomplete refactor of inline functions
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/constants.h | 3 | ||||
-rw-r--r-- | include/game.h | 31 | ||||
-rw-r--r-- | include/utils.h | 55 | ||||
-rw-r--r-- | src/background.c | 1 | ||||
-rw-r--r-- | src/game.c | 45 | ||||
-rw-r--r-- | src/physics.c | 13 | ||||
-rw-r--r-- | src/player.c | 1 |
8 files changed, 110 insertions, 40 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c5dd35..343273c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable(game "include/player.h" "include/assets.h" "include/background.h" + "include/utils.h" "src/game.c" "src/main.c" diff --git a/include/constants.h b/include/constants.h index 870c7fb..2fd6ace 100644 --- a/include/constants.h +++ b/include/constants.h @@ -9,3 +9,6 @@ #define MAX_SPRITE_COUNT 4 //Maximum number of sprites per entity #define NO_OFFSET 0 + +#define MAX_FRAMES 30 +#define MAX_ANIMATIONS 20 diff --git a/include/game.h b/include/game.h index e54afd3..530ae73 100644 --- a/include/game.h +++ b/include/game.h @@ -21,10 +21,25 @@ typedef enum DrawLayer { Foreground_Layer = 1 } DrawLayer; +typedef struct Animation { + int numFrames; + bool isLooping; + bool isComplete; + Rectangle srcRects[MAX_FRAMES]; + float frameTimes[MAX_FRAMES]; + + int currentFrame; + float currentFrameTimer; +} Animation; + typedef struct Sprite { Texture2D texture; DrawLayer layer; Rectangle destRect; //Destination rectangle relative to player position + + int numAnimations; //Zero for static spritesx + Animation animations[MAX_ANIMATIONS]; + int currentAnimation; } Sprite; typedef struct Entity { @@ -69,20 +84,8 @@ void InitGame(); void UpdateGame(float deltaTime); void DrawGame(); -static inline bool EntityAllocated(const Entity* e) { - return (e->flags & ENTITY_ALLOCATED); -} - -static inline bool SameEntity(const Entity* a, const Entity* b) { - return a->id == b->id; -} - -static inline void AddSpriteToEntity(Entity* e, Sprite s) { - if (e->numSprites < MAX_SPRITE_COUNT) { - e->sprites[e->numSprites++] = s; - } -} - +void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt); +void DrawEntitySprite(const Sprite* sprite, int spriteIndex); #ifdef BEATEMUP_DEBUG void DebugHighlights(const Entity* e); diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..59c1bc4 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,55 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include <stdbool.h> +#include "game.h" + +static inline bool EntityAllocated(const Entity* e) { + return (e->flags & ENTITY_ALLOCATED); +} + +static inline bool PhysicsEnabled(const Entity* e) { + return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); +} + +static inline bool SameEntity(const Entity* a, const Entity* b) { + return a->id == b->id; +} + +static inline void AddSpriteToEntity(Entity* e, Sprite s) { + if (e->numSprites < MAX_SPRITE_COUNT) { + e->sprites[e->numSprites++] = s; + } +} + +static inline bool IsAnimated(const Sprite* s) { + return s->numAnimations > 0; +} + +static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { + return EntityAllocated(e) + && (e->flags & ENTITY_VISIBLE); +} + +static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { + Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; + + destRectGlobal.x += e->position.x; + destRectGlobal.y += e->position.y; + + return destRectGlobal; +} + +static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) { + Rectangle physicsColliderGlobal = e->physicsCollider; + physicsColliderGlobal.x += e->position.x; + physicsColliderGlobal.y += e->position.y; + + return physicsColliderGlobal; +} + +static inline Animation* GetCurrentAnimation(Sprite* s) { + return &s->animations[s->currentAnimation]; +} + +#endif // UTILS_H_ diff --git a/src/background.c b/src/background.c index d3588a7..c9ed1fb 100644 --- a/src/background.c +++ b/src/background.c @@ -3,6 +3,7 @@ #include <raymath.h> #include <assert.h> #include <stdlib.h> +#include "utils.h" void AddBackground(const char* backgroundTextureName) { Entity e = {0}; @@ -8,23 +8,10 @@ #include "player.h" #include "background.h" #include <math.h> +#include "utils.h" Game game; -static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { - return EntityAllocated(e) - && (e->flags & ENTITY_VISIBLE); -} - -static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { - Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; - - destRectGlobal.x += e->position.x; - destRectGlobal.y += e->position.y; - - return destRectGlobal; -} - void AddEntity(Entity* e) { static int nextId = 0; @@ -158,3 +145,33 @@ void AddWall(float xpos, float ypos, float width, float height) { AddEntity(&wall); } + +static inline float GetCurrentFrameTime(const Animation* animation) { + return animation->frameTimes[animation->currentFrame]; +} + +void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) { + if (!IsAnimated(sprite)) + return; + + Animation* currentAnimation = GetCurrentAnimation(sprite); + + if (currentAnimation->isComplete) + return; + + currentAnimation->currentFrameTimer += dt; + + if (currentAnimation->currentFrameTimer >= GetCurrentFrameTime(currentAnimation)) { + currentAnimation->currentFrame++; + currentAnimation->currentFrameTimer = 0; + } + + if (currentAnimation->currentFrame >= currentAnimation->numFrames) { + if (currentAnimation->isLooping) + currentAnimation->currentFrame = 0; + else { + currentAnimation->currentFrame = currentAnimation->numFrames - 1; + currentAnimation->isComplete = true; + } + } +} diff --git a/src/physics.c b/src/physics.c index 51e80f5..bc93a29 100644 --- a/src/physics.c +++ b/src/physics.c @@ -1,20 +1,9 @@ #include "physics.h" #include "game.h" +#include "utils.h" extern Game game; -static inline bool PhysicsEnabled(const Entity* e) { - return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); -} - -static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) { - Rectangle physicsColliderGlobal = e->physicsCollider; - physicsColliderGlobal.x += e->position.x; - physicsColliderGlobal.y += e->position.y; - - return physicsColliderGlobal; -} - void MoveAndSlide(Entity* e, float deltaTime) { Vector2 velocity = e->velocity; Rectangle physicsCollider = GetPhysicsColliderGlobal(e); diff --git a/src/player.c b/src/player.c index 67f971f..62675f4 100644 --- a/src/player.c +++ b/src/player.c @@ -5,6 +5,7 @@ #include "player.h" #include "physics.h" #include "assets.h" +#include "utils.h" extern Game game; |