aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathansmith117 <thenathansmithsmith@gmail.com>2024-02-15 00:39:24 +0000
committernathansmith117 <thenathansmithsmith@gmail.com>2024-02-15 00:39:24 +0000
commit9211212e939561c92e9a29baf0b61408d0b58c7a (patch)
treed1dc8a7b2d86c6c17b44badf9057985f7350e52a
parent64bb931e18825b285dd93d0b9987ef72ece15ab4 (diff)
downloadPenguinYippies-9211212e939561c92e9a29baf0b61408d0b58c7a.tar.gz
PenguinYippies-9211212e939561c92e9a29baf0b61408d0b58c7a.tar.bz2
PenguinYippies-9211212e939561c92e9a29baf0b61408d0b58c7a.zip
More animation stuff
-rw-r--r--src/animation.c29
-rw-r--r--src/animation.h5
2 files changed, 34 insertions, 0 deletions
diff --git a/src/animation.c b/src/animation.c
index f173401..2fc20fb 100644
--- a/src/animation.c
+++ b/src/animation.c
@@ -23,3 +23,32 @@ void freeAnimation(Animation* animation)
UnloadImage(animation->image);
UnloadTexture(animation->texture);
}
+
+void setAnimationFrame(Animation * animation, int frame)
+{
+ animation->currentFrame = frame;
+ unsigned int nextFrameDataOffset = animation->image.width * animation->image.height * 4 * frame;
+
+ UpdateTexture(animation->texture, ((unsigned int*)animation->image.data) + nextFrameDataOffset);
+}
+
+void runAnimation(Animation * animation)
+{
+ double currentTime = GetTime();
+
+ if (animation->lastTime == -1 || currentTime - animation->lastTime >= animation->delay)
+ {
+ // Count the frames up.
+ int newFrame = animation->currentFrame + 1;
+
+ if (newFrame >= animation->frameCount)
+ {
+ newFrame = 0;
+ }
+
+ // Set the frame
+ setAnimationFrame(animation, newFrame);
+
+ animation->lastTime = currentTime;
+ }
+}
diff --git a/src/animation.h b/src/animation.h
index 8ec918f..089e6e1 100644
--- a/src/animation.h
+++ b/src/animation.h
@@ -22,4 +22,9 @@ typedef struct Animation {
Animation loadAnimationFromFile(const char* fileName);
void freeAnimation(Animation* animation);
+// Set the frame and update the texture.
+void setAnimationFrame(Animation * animation, int frame);
+
+void runAnimation(Animation * animation);
+
#endif