-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteSheet.cpp
More file actions
226 lines (199 loc) · 5.04 KB
/
SpriteSheet.cpp
File metadata and controls
226 lines (199 loc) · 5.04 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "SpriteSheet.h"
SpriteSheet::SpriteSheet(TextureManager* textureManager, bool sharedMemory = true) :
m_textureManager(textureManager),
m_spriteScale(1.0f, 1.0f),
m_totalFrames(0),
m_frame(0),
m_texture(nullptr),
m_sharedMemory(sharedMemory),
m_origin(OriginType::Medium)
{
}
SpriteSheet::~SpriteSheet()
{
releaseSheet();
}
void SpriteSheet::draw(sf::RenderWindow* window)
{
window->draw(m_sprite);
}
void SpriteSheet::frameStep()
{
m_frame = ++m_frame % m_totalFrames;
}
void SpriteSheet::resetFrame()
{
m_frame = 0;
}
void SpriteSheet::cropSprite()
{
m_cropRect = sf::IntRect(
(m_spriteSize.x * m_frame) + m_sheetPadding.x + (m_spriteSpacing.x * m_frame),
m_sheetPadding.y,
m_spriteSize.x,
m_spriteSize.y
);
m_sprite.setTextureRect(m_cropRect);
}
int SpriteSheet::getTotalFrames() const
{
return m_totalFrames;
}
const sf::Sprite* SpriteSheet::getSprite() const
{
return &m_sprite;
}
const sf::Texture* SpriteSheet::getTexture() const
{
return m_texture;
}
const sf::Vector2i& SpriteSheet::getSpriteSize() const { return m_spriteSize; }
const sf::Vector2f& SpriteSheet::getSpriteScale() const
{
return m_spriteScale;
}
const sf::Vector2f& SpriteSheet::getSpritePosition() const { return m_sprite.getPosition(); }
void SpriteSheet::setSpriteSize(const sf::Vector2i& size)
{
m_spriteSize = size;
switch (m_origin)
{
case OriginType::Medium:
m_sprite.setOrigin(static_cast<float>(m_spriteSize.x) / 2.f, static_cast<float>(m_spriteSize.y) / 2.f);
break;
case OriginType::Bottom:
m_sprite.setOrigin(static_cast<float>(m_spriteSize.x) / 2.f, static_cast<float>(m_spriteSize.y));
break;
case OriginType::Top:
m_sprite.setOrigin(static_cast<float>(m_spriteSize.x) / 2.f, 0.f);
break;
}
}
void SpriteSheet::setSpritePosition(const sf::Vector2f& position)
{
m_sprite.setPosition(position);
}
void SpriteSheet::setSpriteRotation(const float& rotation)
{
m_sprite.setRotation(rotation);
}
void SpriteSheet::setSpriteScale(const sf::Vector2f& scale)
{
m_spriteScale = scale;
m_sprite.setScale(m_spriteScale);
}
void SpriteSheet::setSheetPadding(const sf::Vector2i& padding)
{
m_sheetPadding = padding;
}
void SpriteSheet::setSpriteSpacing(const sf::Vector2i& spacing)
{
m_spriteSpacing = spacing;
}
void SpriteSheet::setSpriteOrigin(OriginType origin)
{
m_origin = origin;
}
void SpriteSheet::setSpriteColor(const sf::Color& color)
{
m_sprite.setColor(color);
}
void SpriteSheet::setSmooth(const bool& smooth)
{
m_texture->setSmooth(smooth);
}
const sf::Vector2i& SpriteSheet::getSheetPadding() const { return m_sheetPadding; }
const sf::Vector2i& SpriteSheet::getSpriteSpacing() const { return m_spriteSpacing; }
const sf::IntRect& SpriteSheet::getCropRect() const { return m_cropRect; }
sf::FloatRect SpriteSheet::globalRectToPixelRect(const sf::FloatRect& rect) const
{
sf::FloatRect pixelRect;
pixelRect.left = (rect.left - m_sprite.getGlobalBounds().left) / m_spriteScale.x;
pixelRect.top = (rect.top - m_sprite.getGlobalBounds().top) / m_spriteScale.y;
pixelRect.width = rect.width / m_spriteScale.x;
pixelRect.height = rect.height / m_spriteScale.y;
return pixelRect;
}
bool SpriteSheet::loadSheet(const std::string& filePath)
{
std::ifstream file;
file.open(Utils::getWorkingDirectory() + filePath);
if (!file.is_open())
{
std::cerr << "SpriteSheet::loadSheet() failed to load file: " << filePath << std::endl;
return false;
}
std::string line;
while (std::getline(file, line))
{
if (line[0] == '#') continue;
std::stringstream ss(line);
std::string type;
ss >> type;
if (type == "Texture")
{
if (m_textureId != "")
{
std::cerr << "Duplicate textures in: " << filePath << std::endl;
continue;
}
std::string textureId;
ss >> textureId;
m_textureId = textureId;
if (m_sharedMemory)
{
if (!m_textureManager->requireResource(textureId))
{
std::cerr << "SpriteSheet could not set up the texture: " << textureId << std::endl;
continue;
}
m_texture = new sf::Texture();
m_texture = m_textureManager->getResource(m_textureId);
}
else
{
// load texture directly into separate memory
std::string texturePath = Utils::getWorkingDirectory() + m_textureManager->getPath(m_textureId);
m_texture = new sf::Texture();
if (!m_texture->loadFromFile(texturePath))
{
std::cerr << "SpriteSheet failed to load texture: " << texturePath << std::endl;
continue;
}
}
m_sprite.setTexture(*m_texture);
}
else if (type == "Frames")
{
ss >> m_totalFrames;
}
else if (type == "Size")
{
ss >> m_spriteSize.x >> m_spriteSize.y;
setSpriteSize(m_spriteSize);
}
else if (type == "Scale")
{
ss >> m_spriteScale.x >> m_spriteScale.y;
m_sprite.setScale(m_spriteScale);
}
else if (type == "Padding")
{
ss >> m_sheetPadding.x >> m_sheetPadding.y;
}
else if (type == "Spacing")
{
ss >> m_spriteSpacing.x >> m_spriteSpacing.y;
}
}
file.close();
return true;
}
void SpriteSheet::releaseSheet()
{
if (m_sharedMemory)
m_textureManager->releaseResource(m_textureId);
else
delete m_texture;
m_texture = nullptr;
}