summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
index 4a10830..d6e8ce3 100644
--- a/src/game.c
+++ b/src/game.c
@@ -87,6 +87,12 @@ void DrawEntitySprite(const Entity* e, int spriteIndex) {
float rotation = 0.0f;
Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex);
+ if (drawnSprite->flipX)
+ srcRect.width = -srcRect.width;
+
+ if (drawnSprite->flipY)
+ srcRect.height = -srcRect.width;
+
DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, WHITE);
}
@@ -193,3 +199,39 @@ void UpdateCurrentSpriteAnimation(Sprite* sprite, float dt) {
}
}
}
+
+Rectangle GetSrcRectFromIndex(Texture texture, int framesX, int framesY, int index) {
+ const float frameWidth = (float)texture.width / framesX;
+ const float frameHeight = (float)texture.height / framesY;
+
+ return (Rectangle) {
+ .x = (index % framesY) * frameWidth,
+ .y = (index / framesY) * frameHeight,
+ .width = frameWidth,
+ .height = frameHeight
+ };
+}
+
+Animation AnimationFromIndices(AnimationFromIndicesParams params) {
+ int numFrames = params.numFrames;
+ bool isLooping = params.isLooping;
+ Texture texture = params.texture;
+ int framesX = params.framesX;
+ int framesY = params.framesY;
+ int* indices = params.indices;
+ float* frameTimes = params.frameTimes;
+
+ Animation animation = (Animation) {
+ .numFrames = numFrames,
+ .isLooping = isLooping,
+
+ .currentFrame = 0
+ };
+
+ for (int i = 0; i < numFrames; i++) {
+ animation.srcRects[i] = GetSrcRectFromIndex(texture, framesX, framesY, indices[i]);
+ animation.frameTimes[i] = frameTimes[i];
+ }
+
+ return animation;
+}