From f842a445d7239ccc805ec988d13804f77c4bde62 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Wed, 26 Nov 2025 00:24:23 +0100 Subject: [PATCH] animation improvemenets --- examples/HelloAnimation/main.cpp | 5 ++--- interfaces/Crafter.Graphics-Animation.cppm | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/HelloAnimation/main.cpp b/examples/HelloAnimation/main.cpp index 35082ef..4b63b15 100644 --- a/examples/HelloAnimation/main.cpp +++ b/examples/HelloAnimation/main.cpp @@ -24,8 +24,7 @@ int main() { element.UpdatePosition(window); Animation> anim({ - {std::chrono::seconds(0), FractionalToMapped(-0.5)}, - {std::chrono::seconds(5), FractionalToMapped(1.5)} + {std::chrono::seconds(5), FractionalToMapped(-0.5), FractionalToMapped(1.5)}, }); anim.Start(std::chrono::high_resolution_clock::now()); @@ -34,7 +33,7 @@ int main() { std::tuple value = anim.Play(time.now); element.anchorX = std::get<0>(value); element.UpdatePosition(window); - if(anim.currentFrame == anim.keyframes.size()-1) { + if(anim.currentFrame == anim.keyframes.size()) { anim.Start(time.now); } window.LogTiming(); diff --git a/interfaces/Crafter.Graphics-Animation.cppm b/interfaces/Crafter.Graphics-Animation.cppm index 31d24c5..76f2f9c 100644 --- a/interfaces/Crafter.Graphics-Animation.cppm +++ b/interfaces/Crafter.Graphics-Animation.cppm @@ -53,7 +53,8 @@ namespace Crafter { export template struct Keyframe{ std::chrono::duration duration; - T values; + T startValues; + T endValues; }; export template @@ -67,27 +68,27 @@ namespace Crafter { startedAt = time; } T Play(std::chrono::time_point time) { - std::chrono::duration elapsed = time - startedAt; // elapsed time since animation started + std::chrono::duration elapsed = time - startedAt; std::chrono::duration accumulated(0); - for (std::uint_fast32_t i = currentFrame; i < keyframes.size() - 1; ++i) { - accumulated += keyframes[i+1].duration; + for (std::uint_fast32_t i = currentFrame; i < keyframes.size(); ++i) { + accumulated += keyframes[i].duration; if (elapsed < accumulated) { - std::chrono::duration frameStartTime = accumulated - keyframes[i+1].duration; - auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count(); + std::chrono::duration frameStartTime = accumulated - keyframes[i].duration; + auto t = (elapsed - frameStartTime) / keyframes[i].duration.count(); currentFrame = i; return LerpTuple( - keyframes[i].values, - keyframes[i + 1].values, + keyframes[i].startValues, + keyframes[i].endValues, t.count() ); } } // If we get here, we're past the last keyframe - currentFrame = keyframes.size() - 1; - return keyframes.back().values; + currentFrame = keyframes.size(); + return keyframes.back().endValues; } }; } \ No newline at end of file