diff options
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}; +} |
