-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.cpp
59 lines (46 loc) · 1.06 KB
/
animation.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdbool>
#include "texture.h"
// This is a 2D animation [collection of textures]
//FIXME: varying playback speeds?!
class Animation {
private:
bool playing;
std::vector<std::shared_ptr<Texture>> frames;
unsigned int frame = 0;
public:
Animation(std::string path) {
unsigned int i = 0;
//FIXME: Load asynchronous
while(i < 10) { //FIXME: While true but figure out if texture creation fails
std::shared_ptr<Texture> texture = std::make_shared<Texture>(path + std::to_string(++i));
if (texture == nullptr) {
break;
}
frames.push_back(texture);
}
}
unsigned int length() {
return frames.size();
}
// Negative = relative to end
void setFrame(int frame) {
this->frame = (frame + length()) % length();
}
void play() {
playing = true;
}
void pause() {
playing = false;
}
bool isPlaying() {
return playing;
}
void step() {
if (playing) {
frame = (frame + 1) % length();
}
}
std::shared_ptr<Texture> texture() {
return frames[frame];
}
};