From c055eeef6d41269d11b2ddf7f9aba6f8867da65d Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Tue, 23 Dec 2025 15:28:05 +0100 Subject: initial commit --- core/src/cpp/animation.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 core/src/cpp/animation.cpp (limited to 'core/src/cpp/animation.cpp') diff --git a/core/src/cpp/animation.cpp b/core/src/cpp/animation.cpp new file mode 100644 index 0000000..fbda115 --- /dev/null +++ b/core/src/cpp/animation.cpp @@ -0,0 +1,37 @@ +#include "animation.h" + +Animation::Animation( + SDL_Renderer* renderer, + const std::string& filename, + const int width, + const double durationS, + const bool repeat +) + : Sprite{renderer, filename, width} + , durationS{durationS} + , repeat{repeat} +{ +} + +bool Animation::animation_ended(const TimeStamp timestamp) const +{ + double elapsed = std::chrono::duration(Clock::now() - timestamp) + .count(); // seconds from timestamp to now + return !repeat && elapsed >= durationS; +} + +int Animation::frame(const TimeStamp timestamp) const +{ + // seconds from timestamp to now + double elapsed = + std::chrono::duration(Clock::now() - timestamp).count(); + // FIXME: division by zero + int idx = static_cast(nframes * elapsed / durationS); + + return repeat ? idx % nframes : std::min(idx, nframes - 1); +} + +SDL_Rect Animation::rect(const TimeStamp timestamp) const +{ + return {frame(timestamp) * width, 0, width, height}; +} -- cgit v1.2.3-70-g09d2