#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}; }