optimization

This commit is contained in:
Jorijn van der Graaf 2025-11-26 00:00:50 +01:00
commit bf6793e41d
5 changed files with 51 additions and 43 deletions

View file

@ -61,27 +61,12 @@ namespace Crafter {
std::vector<Keyframe<T>> keyframes;
std::uint_fast32_t currentFrame;
std::chrono::time_point<std::chrono::high_resolution_clock> startedAt;
mutable T cachedValue; // Cache the last computed value
mutable std::chrono::time_point<std::chrono::high_resolution_clock> lastTime; // Track when cached value was computed
Animation(std::vector<Keyframe<T>>&& keyframes) : keyframes(std::move(keyframes)), currentFrame(0) {}
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;
// Invalidate cache
lastTime = std::chrono::time_point<std::chrono::high_resolution_clock>();
}
T Play(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
// Check if we can reuse cached value
if (lastTime != std::chrono::time_point<std::chrono::high_resolution_clock>()) {
// Only use cached value if we haven't moved forward in time
if (time <= lastTime) {
return cachedValue;
}
}
std::chrono::duration<double> elapsed = time - startedAt; // elapsed time since animation started
std::chrono::duration<double> accumulated(0);
@ -92,21 +77,17 @@ namespace Crafter {
auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count();
currentFrame = i;
cachedValue = LerpTuple(
return LerpTuple(
keyframes[i].values,
keyframes[i + 1].values,
t.count()
);
lastTime = time;
return cachedValue;
}
}
// If we get here, we're past the last keyframe
currentFrame = keyframes.size() - 1;
cachedValue = keyframes.back().values;
lastTime = time;
return cachedValue;
return keyframes.back().values;
}
};
}

View file

@ -31,10 +31,11 @@ export namespace Crafter {
std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled;
std::uint_fast32_t bufferWidth;
std::uint_fast32_t bufferHeight;
bool opaque = false;
RenderingElement();
RenderingElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
RenderingElement(bool opague = false);
RenderingElement(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElement(bool opague, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
RenderingElement(RenderingElement&) = delete;
RenderingElement& operator=(RenderingElement&) = delete;