From 629a90dea9abfe18f96db0808d012157d1059ac9 Mon Sep 17 00:00:00 2001 From: Six Jonathan Date: Sat, 19 Nov 2022 01:43:31 +0100 Subject: [PATCH] Update frame only when necessary and fix idle animation speed --- content/setting/animation.yaml | 2 +- content/setting/setting.yaml | 2 +- src/main.cpp | 51 ++++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/content/setting/animation.yaml b/content/setting/animation.yaml index 4c46f14..3a3af78 100644 --- a/content/setting/animation.yaml +++ b/content/setting/animation.yaml @@ -3,7 +3,7 @@ Nodes: AnimationNode: name: idle sprite: idle.png - framerate: 10 + framerate: 5 loop: true GrabNode: name: grab diff --git a/content/setting/setting.yaml b/content/setting/setting.yaml index cadfc95..9644659 100644 --- a/content/setting/setting.yaml +++ b/content/setting/setting.yaml @@ -1,5 +1,5 @@ Game: - FPS: 10 # min 1 + FPS: 60 # min 1 Scale: 4 # min 1 RandomSeed: -1 # -1: use random seed Physic: diff --git a/src/main.cpp b/src/main.cpp index a350a5a..fe1f2da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -459,6 +459,8 @@ struct GameData Vec2i windowPos = {0.f, 0.f}; Vec2i petPosLimit = {0, 0}; + bool shouldUpdateFrame = true; + // Resources std::unique_ptr pFramebuffer = nullptr; @@ -1182,6 +1184,7 @@ class SpriteAnimator protected: SpriteSheet* pSheet = nullptr; float timer; + float maxTimer; bool loop; bool isEnd; int frameRate; @@ -1189,37 +1192,42 @@ class SpriteAnimator int indexCurrentAnimSprite; public: - void play(SpriteSheet& inSheet, bool inLoop, int inFrameRate) + void play(GameData& data, SpriteSheet& inSheet, bool inLoop, int inFrameRate) { pSheet = &inSheet; loop = inLoop; frameRate = inFrameRate; indexCurrentAnimSprite = 0; timer = 0.f; + maxTimer = pSheet->getTileCount() / (float)frameRate; isEnd = false; + data.shouldUpdateFrame = true; } - void update(double deltaTime) + void update(GameData& data, double deltaTime) { if (!isDone()) { timer += deltaTime; - indexCurrentAnimSprite = timer * frameRate; - if (indexCurrentAnimSprite >= pSheet->getTileCount()) + while (timer >= maxTimer) { if (loop) { - - indexCurrentAnimSprite = 0; - timer -= pSheet->getTileCount() / (float)frameRate; + timer -= maxTimer; } else { - indexCurrentAnimSprite = pSheet->getTileCount() - 1; + timer = 0.f; isEnd = true; } } + + if (indexCurrentAnimSprite != (int)(timer * frameRate)) + { + data.shouldUpdateFrame = true; + indexCurrentAnimSprite = timer * frameRate; + } } } @@ -1343,13 +1351,13 @@ class AnimationNode : public StateMachine::Node void onEnter(GameData& blackBoard) override { StateMachine::Node::onEnter(blackBoard); - spriteAnimator.play(spriteSheets, loop, frameRate); + spriteAnimator.play(blackBoard, spriteSheets, loop, frameRate); } void onUpdate(GameData& blackBoard, double dt) override { StateMachine::Node::onUpdate(blackBoard, dt); - spriteAnimator.update(dt); + spriteAnimator.update(blackBoard, dt); } void onExit(GameData& blackBoard) override @@ -1979,19 +1987,23 @@ class Game const std::function unlimitedUpdateDebugCollision{[&](double deltaTime) { processInput(datas.window); - physicSystem.update(deltaTime); - // poll for and process events glfwPollEvents(); }}; const std::function limitedUpdate{[&](double deltaTime) { - // render - initDrawContext(); + pet.update(deltaTime); - pet.draw(); + if (datas.shouldUpdateFrame) + { + // render + initDrawContext(); - // swap front and back buffers - glfwSwapBuffers(datas.window); + pet.draw(); + + // swap front and back buffers + glfwSwapBuffers(datas.window); + datas.shouldUpdateFrame = false; + } }}; const std::function limitedUpdateDebugCollision{[&](double deltaTime) { @@ -2021,16 +2033,13 @@ class Game mainLoop.emplaceTimer( [&]() { physicSystem.update(1.f / datas.physicFrameRate); - - pet.update(1.f / datas.physicFrameRate); }, 1.f / datas.physicFrameRate, true); mainLoop.start(); while (!glfwWindowShouldClose(datas.window)) { - mainLoop.update(datas.debugEdgeDetection ? unlimitedUpdateDebugCollision : unlimitedUpdate, - datas.debugEdgeDetection ? limitedUpdateDebugCollision : limitedUpdate); + mainLoop.update(unlimitedUpdate, datas.debugEdgeDetection ? limitedUpdateDebugCollision : limitedUpdate); } } };