aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathansmith117 <thenathansmithsmith@gmail.com>2024-02-15 23:32:41 +0000
committernathansmith117 <thenathansmithsmith@gmail.com>2024-02-15 23:32:41 +0000
commit12b80da7f61df440a6e3252012436fb0baebe073 (patch)
tree9bae4b3df40534df2c015754d6fb9ed75e03f87f
parente05dbe42fd8ffc6601887d19afcba545a6401733 (diff)
downloadPenguinYippies-12b80da7f61df440a6e3252012436fb0baebe073.tar.gz
PenguinYippies-12b80da7f61df440a6e3252012436fb0baebe073.tar.bz2
PenguinYippies-12b80da7f61df440a6e3252012436fb0baebe073.zip
Fixed up animation system
-rw-r--r--src/animation.c43
-rw-r--r--src/animation.h26
-rw-r--r--src/assets.c4
-rw-r--r--src/assets.h2
-rw-r--r--src/game.c4
-rw-r--r--src/game.h4
6 files changed, 60 insertions, 23 deletions
diff --git a/src/animation.c b/src/animation.c
index 2fc20fb..deb764c 100644
--- a/src/animation.c
+++ b/src/animation.c
@@ -2,37 +2,52 @@
#include "game.h"
#include <raylib.h>
-Animation loadAnimationFromFile(const char* fileName)
+AnimationAsset loadAnimationAssetFromFile(const char* fileName)
{
- Animation animation;
+ AnimationAsset animationAsset;
+ animationAsset.image = LoadImageAnim(fileName, &animationAsset.frameCount);
+
+ return animationAsset;
+}
- // Load image in.
- animation.image = LoadImageAnim(fileName, &animation.frameCount);
- animation.texture = LoadTextureFromImage(animation.image);
+void freeAnimationAsset(AnimationAsset* animationAsset)
+{
+ UnloadImage(animationAsset->image);
+}
- // Set options.
+Animation createAnimation(AnimationAsset* asset, double delay)
+{
+ Animation animation;
+
+ animation.frameCount = asset->frameCount;
animation.currentFrame = 0;
- animation.delay = ANIMATION_DEFAULT_DELAY;
- animation.lastTime = -1.0; // -1.0 means there wasn't a last time.
+
+ 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 freeAnimation(Animation* animation)
+void closeAnimation(Animation* animation)
{
- UnloadImage(animation->image);
UnloadTexture(animation->texture);
}
-void setAnimationFrame(Animation * animation, int frame)
+void setAnimationFrame(Animation* animation, int frame)
{
animation->currentFrame = frame;
- unsigned int nextFrameDataOffset = animation->image.width * animation->image.height * 4 * frame;
+ unsigned int nextFrameDataOffset = animation->width * animation->height * 4 * frame;
- UpdateTexture(animation->texture, ((unsigned int*)animation->image.data) + nextFrameDataOffset);
+ UpdateTexture(animation->texture, ((unsigned int*)animation->asset->image.data) + nextFrameDataOffset);
}
-void runAnimation(Animation * animation)
+void runAnimation(Animation* animation)
{
double currentTime = GetTime();
diff --git a/src/animation.h b/src/animation.h
index 089e6e1..963af74 100644
--- a/src/animation.h
+++ b/src/animation.h
@@ -1,30 +1,44 @@
#include "gameCommon.h"
+#include <raylib.h>
#define ANIMATION_DEFAULT_DELAY 0.1
#ifndef ANIMATION_H
#define ANIMATION_H
+// The asset is what is loaded from a file and the animation is like a instance of it.
+
+typedef struct AnimationAsset {
+ // The animation is stored in a image and the texture is used for fast rendering.
+ int frameCount;
+ Image image;
+} AnimationAsset;
+
typedef struct Animation {
// Frame stuff.
int frameCount;
int currentFrame;
- // The animation is stored in a image and the texture is used for fast rendering.
- Image image;
+ AnimationAsset* asset;
Texture texture;
+ int width;
+ int height;
+
// Timing the frames.
double delay;
double lastTime;
} Animation;
-Animation loadAnimationFromFile(const char* fileName);
-void freeAnimation(Animation* animation);
+AnimationAsset loadAnimationAssetFromFile(const char* fileName);
+void freeAnimationAsset(AnimationAsset* animationAsset);
+
+Animation createAnimation(AnimationAsset* asset, double delay);
+void closeAnimation(Animation* animation);
// Set the frame and update the texture.
-void setAnimationFrame(Animation * animation, int frame);
+void setAnimationFrame(Animation* animation, int frame);
-void runAnimation(Animation * animation);
+void runAnimation(Animation* animation);
#endif
diff --git a/src/assets.c b/src/assets.c
index 05e2b04..e0aa0a8 100644
--- a/src/assets.c
+++ b/src/assets.c
@@ -29,7 +29,7 @@ void loadAnimations(Assets* assets)
{
char filePath[ASSETS_NAME_MAX];
snprintf(filePath, ASSETS_NAME_MAX, "assets/%s", animationAssetsNames[i]);
- assets->animations[i] = loadAnimationFromFile(filePath);
+ assets->animations[i] = loadAnimationAssetFromFile(filePath);
}
}
@@ -62,7 +62,7 @@ void closeAssets(Assets* assets)
// Animations.
for (int i = 0; i < ANIMATION_ASSET_COUNT; ++i)
{
- freeAnimation(&assets->animations[i]);
+ freeAnimationAsset(&assets->animations[i]);
}
TraceLog(LOG_INFO, "Assets closed");
diff --git a/src/assets.h b/src/assets.h
index eed002b..8dd9e51 100644
--- a/src/assets.h
+++ b/src/assets.h
@@ -14,7 +14,7 @@ extern const char animationAssetsNames[ANIMATION_ASSET_COUNT][ASSETS_NAME_MAX];
typedef struct Assets {
Texture textures[TEXTURE_ASSET_COUNT];
- Animation animations[ANIMATION_ASSET_COUNT];
+ AnimationAsset animations[ANIMATION_ASSET_COUNT];
} Assets;
void initAssets(Assets* assets);
diff --git a/src/game.c b/src/game.c
index 899b3f2..cecca65 100644
--- a/src/game.c
+++ b/src/game.c
@@ -4,6 +4,9 @@ void initGame(Game* game)
{
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Penguin Yippies!");
+ // Assets.
+ initAssets(&game->assets);
+
// Screens.
game->currentScreen = MAIN_MENU_SCREEN;
initMainMenu(&game->mainMenu, game);
@@ -31,6 +34,7 @@ void updateGame(Game* game)
void closeGame(Game* game)
{
+ closeAssets(&game->assets);
closeMainMenu(&game->mainMenu);
closeGameScreen(&game->gameScreen);
diff --git a/src/game.h b/src/game.h
index a5b8cb6..81cfa67 100644
--- a/src/game.h
+++ b/src/game.h
@@ -1,6 +1,7 @@
#include "gameCommon.h"
#include "mainMenu.h"
#include "gameScreen.h"
+#include "assets.h"
#ifndef GAME_H
#define GAME_H
@@ -11,6 +12,9 @@ typedef enum ScreenId {
} ScreenId;
typedef struct Game {
+ Assets assets;
+
+ // Screens.
ScreenId currentScreen;
MainMenu mainMenu;
GameScreen gameScreen;