xoreos  0.0.5
textureatlasfile.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 #include "src/common/ustring.h"
27 #include "src/common/readstream.h"
28 #include "src/common/xml.h"
30 #include "src/common/strutil.h"
31 
32 #include "src/aurora/resman.h"
34 
35 namespace Aurora {
36 
39  load(*stream);
40 }
41 
43  load(stream);
44 }
45 
47  return _atlasTextures.find(texture) != _atlasTextures.end();
48 }
49 
51  Common::UString &textureFile, float &x, float &y, float &w, float &h) const {
52  std::map<Common::UString, AtlasTexture>::const_iterator iter = _atlasTextures.find(texture);
53  if (iter == _atlasTextures.end()) {
54  throw Common::Exception("Atlas Texture %s not found", texture.c_str());
55  }
56 
57  const AtlasTexture &atlasTexture = iter->second;
58  textureFile = atlasTexture.textureFile;
59  x = atlasTexture.x;
60  y = atlasTexture.y;
61  w = atlasTexture.w;
62  h = atlasTexture.h;
63 }
64 
66  Common::XMLParser parser(stream);
67 
68  const Common::XMLNode &node = parser.getRoot();
69 
70  if(node.getName() != "AtlasData")
71  throw Common::Exception("Invalid tag, <AtlasData> expected, <%s> found", node.getName().c_str());
72 
73  const Common::XMLNode::Children &atlasTextures = node.getChildren();
74  for (Common::XMLNode::Children::const_iterator a = atlasTextures.begin(); a != atlasTextures.end(); ++a) {
75  if ((*a)->getName() != "AtlasTexture")
76  throw Common::Exception("Invalid tag, <AtlasTexture> expected, <%s> found", (*a)->getName().c_str());
77 
78  Common::UString atlasTexture = (*a)->getProperty("Name");
79 
80  const Common::XMLNode::Children &sourceTextures = (*a)->getChildren();
81  for (Common::XMLNode::Children::const_iterator s = sourceTextures.begin();
82  s != sourceTextures.end(); ++s) {
83  if ((*s)->getName() != "SourceTexture")
84  throw Common::Exception("Invalid tag, <SourceTexture> expected, <%s> found", (*s)->getName().c_str());
85 
86  const Common::UString sourceTexture = (*s)->getProperty("Name");
87  const Common::UString offsetAndScale = (*s)->getProperty("OffsetAndScale");
88  const Common::UString offsetAndScaleV2 = (*s)->getProperty("OffsetAndScale_V2");
89 
90  assert(offsetAndScale == offsetAndScaleV2);
91 
92  std::vector<Common::UString> values;
93  Common::UString::split(offsetAndScale, ' ', values);
94  if (values.size() != 4)
95  throw Common::Exception("Invalid OffsetAndScale attribute");
96 
97  atlasTexture.erase(atlasTexture.findFirst(".dds"), atlasTexture.end());
98 
99  AtlasTexture texture;
100  texture.textureFile = atlasTexture;
101  Common::parseString(values[0], texture.x);
102  Common::parseString(values[1], texture.y);
103  Common::parseString(values[2], texture.w);
104  Common::parseString(values[3], texture.h);
105 
106  _atlasTextures[sourceTexture] = texture;
107  }
108  }
109 }
110 
111 } // End of namespace Aurora
A loader class for texture atlas XML files for Dragon Age: Origins and Dragon Age 2...
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
std::map< Common::UString, AtlasTexture > _atlasTextures
void getAtlasTexture(const Common::UString &texture, Common::UString &textureFile, float &x, float &y, float &w, float &h) const
A class holding an UTF-8 string.
Definition: ustring.h:48
Class to parse a ReadStream into a simple XML tree.
Definition: xml.h:51
const UString & getName() const
Definition: xml.cpp:120
const Children & getChildren() const
Return a list of children.
Definition: xml.cpp:132
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
Utility templates and functions for working with strings and streams.
Exception that provides a stack of explanations.
Definition: error.h:36
Extensible Markup Language.
Definition: types.h:226
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
XML parsing helpers, using libxml2.
StackException Exception
Definition: error.h:59
Basic reading stream interfaces.
Unicode string handling.
bool isAtlasTexture(const Common::UString &texture) const
TextureAtlasFile(Common::SeekableReadStream &stream)
Parse tokens out of a stream.
const XMLNode & getRoot() const
Return the XML root node.
Definition: xml.cpp:108
void erase(iterator from, iterator to)
Erase the character within this range.
Definition: ustring.cpp:598
void load(Common::SeekableReadStream &stream)
void split(iterator splitPoint, UString &left, UString &right, bool remove=false) const
Definition: ustring.cpp:621
iterator end() const
Definition: ustring.cpp:257
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void parseString(const UString &str, T &value, bool allowEmpty)
Parse a string into any POD integer, float/double or bool type.
Definition: strutil.cpp:215
The global resource manager for Aurora resources.