summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/game.h2
-rw-r--r--include/utils.h4
-rw-r--r--src/background.c16
-rw-r--r--src/game.c22
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) {