-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedSprite.cpp
More file actions
153 lines (124 loc) · 5.02 KB
/
AnimatedSprite.cpp
File metadata and controls
153 lines (124 loc) · 5.02 KB
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
147
148
149
150
151
152
153
////////////////////////////////////////////////////////////
//
// Copyright (C) 2014 Maximilian Wagenbach (aka. Foaly) (foaly.f@web.de)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the
// use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#include <cstdlib>
#include <cmath>
#include "AnimatedSprite.hpp"
AnimatedSprite::AnimatedSprite(sf::Time frameTime, bool paused, bool looped)
: m_animation(NULL), m_frameTime(frameTime), m_currentFrame(0),
m_isPaused(paused), m_isLooped(looped), m_texture(NULL) {}
void AnimatedSprite::setAnimation(const Animation &animation) {
m_animation = &animation;
m_texture = m_animation->getSpriteSheet();
m_currentFrame = 0;
setFrame(m_currentFrame);
}
void AnimatedSprite::setFrameTime(sf::Time time) { m_frameTime = time; }
void AnimatedSprite::play() { m_isPaused = false; }
void AnimatedSprite::play(const Animation &animation) {
if (getAnimation() != &animation)
setAnimation(animation);
play();
}
void AnimatedSprite::pause() { m_isPaused = true; }
void AnimatedSprite::stop() {
m_isPaused = true;
m_currentFrame = 0;
setFrame(m_currentFrame);
}
void AnimatedSprite::setLooped(bool looped) { m_isLooped = looped; }
void AnimatedSprite::setColor(const sf::Color &color) {
// Update the vertices' color
m_vertices[0].color = color;
m_vertices[1].color = color;
m_vertices[2].color = color;
m_vertices[3].color = color;
}
const Animation *AnimatedSprite::getAnimation() const { return m_animation; }
sf::FloatRect AnimatedSprite::getLocalBounds() const {
sf::IntRect rect = m_animation->getFrame(m_currentFrame);
float width = static_cast<float>(std::abs(rect.width));
float height = static_cast<float>(std::abs(rect.height));
return sf::FloatRect(0.f, 0.f, width, height);
}
sf::FloatRect AnimatedSprite::getGlobalBounds() const {
return getTransform().transformRect(getLocalBounds());
}
bool AnimatedSprite::isLooped() const { return m_isLooped; }
bool AnimatedSprite::isPlaying() const { return !m_isPaused; }
sf::Time AnimatedSprite::getFrameTime() const { return m_frameTime; }
void AnimatedSprite::setFrame(std::size_t newFrame, bool resetTime) {
if (m_animation) {
// calculate new vertex positions and texture coordiantes
sf::IntRect rect = m_animation->getFrame(newFrame);
m_vertices[0].position = sf::Vector2f(0.f, 0.f);
m_vertices[1].position = sf::Vector2f(0.f, static_cast<float>(rect.height));
m_vertices[2].position = sf::Vector2f(static_cast<float>(rect.width),
static_cast<float>(rect.height));
m_vertices[3].position = sf::Vector2f(static_cast<float>(rect.width), 0.f);
float left = static_cast<float>(rect.left) + 0.0001f;
float right = left + static_cast<float>(rect.width);
float top = static_cast<float>(rect.top);
float bottom = top + static_cast<float>(rect.height);
m_vertices[0].texCoords = sf::Vector2f(left, top);
m_vertices[1].texCoords = sf::Vector2f(left, bottom);
m_vertices[2].texCoords = sf::Vector2f(right, bottom);
m_vertices[3].texCoords = sf::Vector2f(right, top);
}
if (resetTime)
m_currentTime = sf::Time::Zero;
}
void AnimatedSprite::update(sf::Time deltaTime) {
// if not paused and we have a valid animation
if (!m_isPaused && m_animation) {
// add delta time
m_currentTime += deltaTime;
// if current time is bigger then the frame time advance one frame
if (m_currentTime >= m_frameTime) {
// reset time, but keep the remainder
m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() %
m_frameTime.asMicroseconds());
// get next Frame index
if (m_currentFrame + 1 < m_animation->getSize())
m_currentFrame++;
else {
// animation has ended
m_currentFrame = 0; // reset to start
if (!m_isLooped) {
m_isPaused = true;
}
}
// set the current frame, not reseting the time
setFrame(m_currentFrame, false);
}
}
}
void AnimatedSprite::draw(sf::RenderTarget &target,
sf::RenderStates states) const {
if (m_animation && m_texture) {
states.transform *= getTransform();
states.texture = m_texture;
target.draw(m_vertices, 4, sf::Quads, states);
}
}