diff options
| author | Nikita Kostovsky <nikita@kostovsky.me> | 2025-12-23 15:28:05 +0100 |
|---|---|---|
| committer | Nikita Kostovsky <nikita@kostovsky.me> | 2025-12-23 15:28:05 +0100 |
| commit | c055eeef6d41269d11b2ddf7f9aba6f8867da65d (patch) | |
| tree | dcd9baaec93c50a8ab49656be86ea248c17421fd /core/src/cpp/animation.cpp | |
Diffstat (limited to 'core/src/cpp/animation.cpp')
| -rw-r--r-- | core/src/cpp/animation.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
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<double>(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<double>(Clock::now() - timestamp).count(); + // FIXME: division by zero + int idx = static_cast<int>(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}; +} |
