diff options
Diffstat (limited to 'src/game.c')
-rw-r--r-- | src/game.c | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -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; +} |