From 3631e9c1888d2d2e43e85af654fc0544594795f2 Mon Sep 17 00:00:00 2001 From: nathansmith117 Date: Fri, 16 Feb 2024 10:38:55 -0700 Subject: Animation play and pause working --- src/animation.c | 19 +++++++++++++++++++ src/animation.h | 4 ++++ src/mainMenu.c | 10 +++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/animation.c b/src/animation.c index 52210ae..1a2f612 100644 --- a/src/animation.c +++ b/src/animation.c @@ -31,6 +31,8 @@ Animation createAnimation(AnimationAsset* asset, double delay) animation.delay = delay; animation.lastTime = -1.0; // -1.0 for no last time. + animation.playing = false; + return animation; } @@ -49,6 +51,11 @@ void setAnimationFrame(Animation* animation, int frame) void runAnimation(Animation* animation) { + if (!animation->playing) + { + return; + } + double currentTime = GetTime(); if (animation->lastTime == -1.0 || currentTime - animation->lastTime >= animation->delay) @@ -68,7 +75,19 @@ void runAnimation(Animation* animation) } } +void playAnimation(Animation* animation) +{ + animation->playing = true; + animation->lastTime = -1.0; +} + void pauseAnimation(Animation* animation) { + animation->playing = false; +} + +void toggleAnimation(Animation* animation) +{ + animation->playing = !animation->playing; animation->lastTime = -1.0; } diff --git a/src/animation.h b/src/animation.h index 6a632eb..693383b 100644 --- a/src/animation.h +++ b/src/animation.h @@ -28,6 +28,8 @@ typedef struct Animation { // Timing the frames. double delay; double lastTime; + + bool playing; } Animation; AnimationAsset loadAnimationAssetFromFile(const char* fileName); @@ -40,6 +42,8 @@ void closeAnimation(Animation* animation); void setAnimationFrame(Animation* animation, int frame); void runAnimation(Animation* animation); +void playAnimation(Animation* animation); void pauseAnimation(Animation* animation); +void toggleAnimation(Animation* animation); #endif diff --git a/src/mainMenu.c b/src/mainMenu.c index e5d3b60..370b71f 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -1,12 +1,11 @@ #include "mainMenu.h" #include "game.h" #include "assets.h" -#include void initMainMenu(MainMenu* mainMenu, Game* game) { mainMenu->startButtonAnimation = createAnimation( - &game->assets.animations[BUTTON_BOX_ANIMATION], + &game->assets.animations[PENGUIN_LOL_ANIMATION], ANIMATION_DEFAULT_DELAY ); } @@ -15,9 +14,14 @@ void updateMainMenu(MainMenu* mainMenu, Game* game) { ClearBackground(RAYWHITE); - DrawTextureEx(mainMenu->startButtonAnimation.texture, (Vector2){100.0, 100.0}, 0.0, 1.0, WHITE); + DrawTextureEx(mainMenu->startButtonAnimation.texture, (Vector2){100.0, 100.0}, 0.0, 0.1, WHITE); runAnimation(&mainMenu->startButtonAnimation); + if (IsKeyPressed(KEY_SPACE)) + { + toggleAnimation(&mainMenu->startButtonAnimation); + } + DrawFPS(0, 0); } -- cgit v1.2.3