#include "animation.h" #include "game.h" #include AnimationAsset loadAnimationAssetFromFile(const char* fileName) { AnimationAsset animationAsset; animationAsset.image = LoadImageAnim(fileName, &animationAsset.frameCount); return animationAsset; } void freeAnimationAsset(AnimationAsset* animationAsset) { UnloadImage(animationAsset->image); } Animation createAnimation(AnimationAsset* asset, double delay) { Animation animation; animation.frameCount = asset->frameCount; animation.currentFrame = 0; animation.asset = asset; animation.texture = LoadTextureFromImage(asset->image); animation.width = asset->image.width; animation.height = asset->image.height; animation.delay = delay; animation.lastTime = -1; // -1 for no last time. return animation; } void closeAnimation(Animation* animation) { UnloadTexture(animation->texture); } void setAnimationFrame(Animation* animation, int frame) { animation->currentFrame = frame; unsigned int nextFrameDataOffset = animation->width * animation->height * 4 * frame; UpdateTexture(animation->texture, ((unsigned int*)animation->asset->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; } }