animations

This commit is contained in:
Jorijn van der Graaf 2025-11-24 03:38:20 +01:00
commit e4dda0861c
8 changed files with 161 additions and 4 deletions

View file

@ -0,0 +1,80 @@
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Graphics:Animation;
import std;
namespace Crafter {
template <typename T>
constexpr T Lerp(T a, T b, double elapsed) {
return a + static_cast<T>(elapsed * (b - a));
}
template <typename Tuple, std::size_t... Is>
constexpr auto LerpTupleImpl(const Tuple& a, const Tuple& b, double elapsed, std::index_sequence<Is...>) {
return std::make_tuple(Lerp(std::get<Is>(a), std::get<Is>(b), elapsed)...);
}
template <typename Tuple>
constexpr auto LerpTuple(const Tuple& a, const Tuple& b, double elapsed) {
return LerpTupleImpl(a, b, elapsed, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
}
export template <typename T>
struct Keyframe{
std::chrono::duration<double> duration;
T values;
};
export template <typename T>
struct Animation {
std::vector<Keyframe<T>> keyframes;
std::uint_fast32_t currentFrame;
std::chrono::time_point<std::chrono::high_resolution_clock> startedAt;
Animation(std::vector<Keyframe<T>>&& keyframes) : keyframes(std::move(keyframes)) {}
void Start(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
currentFrame = 0;
startedAt = time;
}
T Play(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
std::chrono::duration<double> elapsed = time - startedAt; // elapsed time since animation started
std::chrono::duration<double> accumulated(0);
for (std::uint_fast32_t i = currentFrame; i < keyframes.size() - 1; ++i) {
accumulated += keyframes[i+1].duration;
if (elapsed < accumulated) {
std::chrono::duration<double> frameStartTime = accumulated - keyframes[i+1].duration;
auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count();
currentFrame = i;
return LerpTuple(
keyframes[i].values,
keyframes[i + 1].values,
t.count()
);
}
}
// If we get here, we're past the last keyframe
currentFrame = keyframes.size() - 1;
return keyframes.back().values;
}
};
}

View file

@ -95,6 +95,10 @@ namespace Crafter {
float a;
};
export struct FrameTime {
std::chrono::time_point<std::chrono::high_resolution_clock> now;
std::chrono::duration<double> delta;
};
export constexpr std::int_fast32_t BOUND = 9;

View file

@ -47,8 +47,9 @@ export namespace Crafter {
class UiElement;
class Window {
public:
std::chrono::time_point<std::chrono::high_resolution_clock> lastFrameEnd;
Event<void> onClose;
Event<std::chrono::time_point<std::chrono::high_resolution_clock>> onUpdate;
Event<FrameTime> onUpdate;
bool open = true;
bool updating = false;
Window() = default;

View file

@ -25,6 +25,7 @@ export import :UiElement;
export import :Types;
export import :Font;
export import :Shm;
export import :Animation;
// export import :WindowWaylandVulkan;
// export import :VulkanBuffer;