blob: fbda11557197740fcb9025bffc9155fabd7c5596 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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};
}
|