-
Notifications
You must be signed in to change notification settings - Fork 231
/
Animation.cpp
146 lines (119 loc) · 2.72 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <Book/Animation.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Texture.hpp>
Animation::Animation()
: mSprite()
, mFrameSize()
, mNumFrames(0)
, mCurrentFrame(0)
, mDuration(sf::Time::Zero)
, mElapsedTime(sf::Time::Zero)
, mRepeat(false)
{
}
Animation::Animation(const sf::Texture& texture)
: mSprite(texture)
, mFrameSize()
, mNumFrames(0)
, mCurrentFrame(0)
, mDuration(sf::Time::Zero)
, mElapsedTime(sf::Time::Zero)
, mRepeat(false)
{
}
void Animation::setTexture(const sf::Texture& texture)
{
mSprite.setTexture(texture);
}
const sf::Texture* Animation::getTexture() const
{
return mSprite.getTexture();
}
void Animation::setFrameSize(sf::Vector2i frameSize)
{
mFrameSize = frameSize;
}
sf::Vector2i Animation::getFrameSize() const
{
return mFrameSize;
}
void Animation::setNumFrames(std::size_t numFrames)
{
mNumFrames = numFrames;
}
std::size_t Animation::getNumFrames() const
{
return mNumFrames;
}
void Animation::setDuration(sf::Time duration)
{
mDuration = duration;
}
sf::Time Animation::getDuration() const
{
return mDuration;
}
void Animation::setRepeating(bool flag)
{
mRepeat = flag;
}
bool Animation::isRepeating() const
{
return mRepeat;
}
void Animation::restart()
{
mCurrentFrame = 0;
}
bool Animation::isFinished() const
{
return mCurrentFrame >= mNumFrames;
}
sf::FloatRect Animation::getLocalBounds() const
{
return sf::FloatRect(getOrigin(), static_cast<sf::Vector2f>(getFrameSize()));
}
sf::FloatRect Animation::getGlobalBounds() const
{
return getTransform().transformRect(getLocalBounds());
}
void Animation::update(sf::Time dt)
{
sf::Time timePerFrame = mDuration / static_cast<float>(mNumFrames);
mElapsedTime += dt;
sf::Vector2i textureBounds(mSprite.getTexture()->getSize());
sf::IntRect textureRect = mSprite.getTextureRect();
if (mCurrentFrame == 0)
textureRect = sf::IntRect(0, 0, mFrameSize.x, mFrameSize.y);
// While we have a frame to process
while (mElapsedTime >= timePerFrame && (mCurrentFrame <= mNumFrames || mRepeat))
{
// Move the texture rect left
textureRect.left += textureRect.width;
// If we reach the end of the texture
if (textureRect.left + textureRect.width > textureBounds.x)
{
// Move it down one line
textureRect.left = 0;
textureRect.top += textureRect.height;
}
// And progress to next frame
mElapsedTime -= timePerFrame;
if (mRepeat)
{
mCurrentFrame = (mCurrentFrame + 1) % mNumFrames;
if (mCurrentFrame == 0)
textureRect = sf::IntRect(0, 0, mFrameSize.x, mFrameSize.y);
}
else
{
mCurrentFrame++;
}
}
mSprite.setTextureRect(textureRect);
}
void Animation::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(mSprite, states);
}