add basic image loading
[exterlulz-musk.git] / musk / animation.cpp
blob85b992526a4e490edc18c6b6e73d5c9e08b12ab3
1 #include "animation.h"
3 namespace musk {
5 using std::string;
7 Animation::Animation(const string& path, size_t frames, uint32_t durations) :
8 Image(path),
10 frames_(frames),
11 durations_(),
12 currentFrame_(0),
13 currentTicks_(0),
14 width_(0) {
16 if (frames_ > 1) {
17 width_ = Image::width() / frames_;
19 else {
20 frames_ = 1;
21 width_ = Image::width();
24 clipMask_.w = width_;
25 durations_.assign(frames_, durations);
28 int Animation::width() const {
29 return width_;
32 unsigned int Animation::durationForFrame(size_t n) const {
33 return durations_.at(n);
36 void Animation::reset() {
37 clipMask_.x = 0;
40 void Animation::step(uint32_t ticks)
42 currentTicks_ += ticks;
43 if (currentTicks_ < durationForFrame(currentFrame_)) {
44 return;
46 currentTicks_ -= durationForFrame(currentFrame_);
47 currentFrame_ = (currentFrame_ + 1) % frames_;
49 clipMask_.x += width();
50 if (clipMask_.x >= Image::width()) {
51 reset();
55 } // namespace musk