-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsprite.cpp
More file actions
294 lines (244 loc) · 7.1 KB
/
sprite.cpp
File metadata and controls
294 lines (244 loc) · 7.1 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "sprite.h"
#include "engine.h"
#include "texture.h"
#include "utils/atlas.h"
#include <cstring>
namespace {
const char *gPages("Pages");
const char *gShapes("Shapes");
}
/*!
\class Sprite
\brief Represents 2D sprite.
\inmodule Resources
Sprites usually used in games to display environment and characters in 2D games.
This class also supports sprite sheets to contain several images in one container to simplify animation or handle tile maps.
*/
Sprite::Sprite() {
}
Sprite::~Sprite() {
PROFILE_FUNCTION();
Sprite::clear();
}
/*!
Adds new sub \a texture as element to current sprite sheet.
All elements will be packed to a single sprite sheet texture using Sprite::pack() method.
Returns the id of the new element.
\sa packSheets()
*/
int Sprite::addElement(Texture *texture) {
PROFILE_FUNCTION();
m_sources.push_back(texture);
Mesh *mesh = Engine::objectCreate<Mesh>();
mesh->makeDynamic();
mesh->setVertices({Vector3(0.0f, 0.0f, 0.0f),
Vector3(0.0f, texture->height(), 0.0f),
Vector3(texture->width(), texture->height(), 0.0f),
Vector3(texture->width(), 0.0f, 0.0f) });
mesh->setIndices({0, 1, 2, 0, 2, 3});
int index = (m_sources.size() - 1);
m_shapes[index].first = mesh;
return index;
}
/*!
Packs all added elements int to a sprite sheets.
Parameter \a padding can be used to delimit elements.
\sa addElement()
*/
void Sprite::packSheets(int padding) {
PROFILE_FUNCTION();
uint32_t atlasWidth = 1;
uint32_t atlasHeight = 1;
std::vector<AtlasNode *> nodes;
nodes.resize(m_sources.size());
AtlasNode root;
if(m_pages.empty()) {
Texture *texture = Engine::objectCreate<Texture>();
texture->setFiltering(Texture::Bilinear);
addPage(texture);
}
while(true) {
root.w = atlasWidth;
root.h = atlasHeight;
uint32_t i;
for(i = 0; i < m_sources.size(); i++) {
Texture *texture = m_sources[i];
int32_t width = (texture->width() + padding * 2);
int32_t height = (texture->height() + padding * 2);
AtlasNode *node = root.insert(width, height);
if(node) {
node->fill = true;
nodes[i] = node;
} else {
atlasWidth *= 2;
atlasHeight *= 2;
if(root.left) {
delete root.left;
root.left = nullptr;
}
if(root.right) {
delete root.right;
root.right = nullptr;
}
root.fill = false;
break;
}
}
if(i == m_sources.size()) {
break;
}
}
for(auto it : m_pages) {
it->resize(atlasWidth, atlasHeight);
for(uint32_t i = 0; i < nodes.size(); i++) {
AtlasNode *node = nodes[i];
int32_t w = node->w - padding * 2;
int32_t h = node->h - padding * 2;
uint8_t *src = m_sources[i]->surface(0).front().data();
uint8_t *dst = it->surface(0).front().data();
for(int32_t y = 0; y < h; y++) {
memcpy(&dst[(y + node->y + padding) * atlasWidth + node->x], &src[y * w], w);
}
Mesh *mesh = shape(i);
if(mesh) {
Vector4 uvFrame;
uvFrame.x = node->x / static_cast<float>(atlasWidth);
uvFrame.y = (node->y + padding) / static_cast<float>(atlasHeight);
uvFrame.z = uvFrame.x + w / static_cast<float>(atlasWidth);
uvFrame.w = uvFrame.y + h / static_cast<float>(atlasHeight);
mesh->setUv0({Vector2(uvFrame.x, uvFrame.y),
Vector2(uvFrame.z, uvFrame.y),
Vector2(uvFrame.z, uvFrame.w),
Vector2(uvFrame.x, uvFrame.w)});
mesh->recalcBounds();
}
}
it->setDirty();
}
}
/*!
\internal
*/
void Sprite::loadUserData(const VariantMap &data) {
clear();
{
auto it = data.find(gPages);
if(it != data.end()) {
for(auto &page : it->second.toList()) {
TString ref = page.toString();
addPage(Engine::loadResource<Texture>(ref));
}
}
}
{
auto it = data.find(gShapes);
if(it != data.end()) {
for(auto &mesh : it->second.toList()) {
VariantList array = mesh.toList();
auto arrayIt = array.begin();
int32_t key = arrayIt->toInt();
++arrayIt;
int32_t pageId = arrayIt->toInt();
++arrayIt;
Mesh *m = Engine::loadResource<Mesh>(arrayIt->toString());
if(m) {
m->incRef();
m_shapes[key] = std::make_pair(m, pageId);
}
}
}
}
}
/*!
\internal
*/
VariantMap Sprite::saveUserData() const {
VariantMap result;
VariantList pages;
for(auto &it : m_pages) {
TString ref = Engine::reference(it);
if(!ref.isEmpty()) {
pages.push_back(ref);
}
}
if(!pages.empty()) {
result[gPages] = pages;
}
VariantList shapes;
for(auto &it : m_shapes) {
TString ref = Engine::reference(it.second.first);
if(!ref.isEmpty()) {
VariantList fields;
fields.push_back(it.first);
fields.push_back(it.second.second);
fields.push_back(ref);
shapes.push_back(fields);
}
}
if(!shapes.empty()) {
result[gShapes] = shapes;
}
return result;
}
/*!
Returns a mesh which represents the sprite with \a key.
*/
Mesh *Sprite::shape(int key) const {
PROFILE_FUNCTION();
auto it = m_shapes.find(key);
if(it != m_shapes.end()) {
return it->second.first;
}
return nullptr;
}
/*!
Sets a new \a mesh for the sprite with \a key.
The old mesh will be deleted and no longer available.
*/
void Sprite::setShape(int key, Mesh *mesh) {
PROFILE_FUNCTION();
if(mesh) {
mesh->incRef();
m_shapes[key] = std::make_pair(mesh, 0);
}
}
/*!
Returns a sprite sheet texture with \a key.
*/
Texture *Sprite::page(int key) {
PROFILE_FUNCTION();
int index = 0;
auto it = m_shapes.find(key);
if(it != m_shapes.end()) {
index = it->second.second;
}
return (index < m_pages.size()) ? m_pages[index] : nullptr;
}
/*!
Adds a new sprite sheet \a texture.
*/
void Sprite::addPage(Texture *texture) {
PROFILE_FUNCTION();
if(texture) {
texture->incRef();
m_pages.push_back(texture);
}
}
/*!
\internal
*/
void Sprite::clear() {
PROFILE_FUNCTION();
for(auto it : m_sources) {
it->decRef();
}
m_sources.clear();
for(auto it : m_pages) {
it->decRef();
}
m_pages.clear();
for(auto it : m_shapes) {
it.second.first->decRef();
}
m_shapes.clear();
}