-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextureAtlas.cpp
103 lines (96 loc) · 3.19 KB
/
TextureAtlas.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
#include "textureatlas.h"
#include <tinyxml/tinyxml.h>
struct IconUVData {
float U1;
float U2;
float V1;
float V2;
};
typedef std::map<std::string, IconUVData> iconDataMap;
bool TextureAtlas::loadTextureAtlas(char *atlasBytes, int atlasSize, std::string& atlasDescriptor) {
iconDataMap iconData;
TiXmlDocument doc;
//doc.LoadFile(iconsLsxPath.c_str());
doc.Parse(atlasDescriptor.c_str());
TiXmlElement *element = doc.FirstChildElement("save");
element = element->FirstChildElement("region");
std::string iconAttrib = "IconUVList";
std::string mapKeyAttrib = "MapKey";
std::string U1Attrib = "U1";
std::string U2Attrib = "U2";
std::string V1Attrib = "V1";
std::string V2Attrib = "V2";
while (element != 0 && element->Attribute("id") != iconAttrib) {
element = element->NextSiblingElement();
}
if (element != 0) {
element = element->FirstChildElement("node");
element = element->FirstChildElement("children");
element = element->FirstChildElement("node");
while (element != 0) {
TiXmlElement *subelement = element->FirstChildElement("attribute");
std::string key;
IconUVData data;
while (subelement != 0) {
if (subelement->Attribute("id") == mapKeyAttrib) {
key = subelement->Attribute("value");
} else if (subelement->Attribute("id") == U1Attrib) {
data.U1 = boost::lexical_cast<float>(subelement->Attribute("value"));
} else if (subelement->Attribute("id") == U2Attrib) {
data.U2 = boost::lexical_cast<float>(subelement->Attribute("value"));
} else if (subelement->Attribute("id") == V1Attrib) {
data.V1 = boost::lexical_cast<float>(subelement->Attribute("value"));
} else if (subelement->Attribute("id") == V2Attrib) {
data.V2 = boost::lexical_cast<float>(subelement->Attribute("value"));
}
subelement = subelement->NextSiblingElement();
}
iconData[key] = data;
element = element->NextSiblingElement();
}
}
QImage qAtlas;
if (!qAtlas.loadFromData((const uchar *)atlasBytes, atlasSize, "DDS"))
return false;
int w = qAtlas.width();
int h = qAtlas.height();
for (iconDataMap::iterator it = iconData.begin(); it != iconData.end(); ++it) {
int imgX = w*iconData[it->first].U1;
int imgY = h*iconData[it->first].V1;
int imgW = w*iconData[it->first].U2 - imgX;
int imgH = h*iconData[it->first].V2 - imgY;
atlasMap[it->first] = qAtlas.copy(imgX, imgY, imgW, imgH);
}
return true;
}
std::vector<std::string> TextureAtlas::getTextureNames() {
std::vector<std::string> textureNames;
for (std::map<std::string, QImage>::iterator it = atlasMap.begin(); it != atlasMap.end(); ++it) {
textureNames.push_back(it->first);
}
return textureNames;
}
bool TextureAtlas::getNamedTexture(const char *textureName, QImage *img) {
std::string texture = textureName;
if (atlasMap.find(texture) != atlasMap.end()) {
QImage toReturn;
if (absoluteSize != 0) {
toReturn = atlasMap[texture].scaled(absoluteSize, absoluteSize);
}
else {
if (scale != 1.0f) {
int w = atlasMap[texture].width();
int h = atlasMap[texture].height();
toReturn = atlasMap[texture].scaled(w * scale, h * scale);
} else {
toReturn = atlasMap[texture];
}
}
*img = toReturn;
return true;
}
return false;
}
TextureAtlas::TextureAtlas()
{
}