GameKit  0.0.1a
C++ gamedev tools
Tileset.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Tileset.cpp
5  *
6  * Description:
7  *
8  * Created: 15/02/2019 19:23:44
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include <sstream>
15 
16 #include "gk/core/Exception.hpp"
17 #include "gk/core/XMLFile.hpp"
18 #include "gk/graphics/Tileset.hpp"
19 
20 namespace gk {
21 
22 Tileset::Tileset(const std::string &filename, const std::string &configFile) {
23  load(filename, configFile);
24 }
25 
26 void Tileset::load(const std::string &filename, const std::string &configFile) {
27  gk::Texture::loadFromFile(filename);
28 
29  gk::XMLFile doc(configFile);
30 
31  tinyxml2::XMLElement *tilesetElement = doc.FirstChildElement("tileset").ToElement();
32  if (!tilesetElement)
33  throw EXCEPTION("Invalid tileset:", configFile);
34 
35  std::string name = tilesetElement->Attribute("name");
36 
37  m_tileWidth = tilesetElement->UnsignedAttribute("tilewidth");
38  m_tileHeight = tilesetElement->UnsignedAttribute("tileheight");
39 
40  u16 tileCount = tilesetElement->UnsignedAttribute("tilecount");
41  m_tiles.resize(tileCount, Tile{0});
42 
43  tinyxml2::XMLElement *tileElement = tilesetElement->FirstChildElement("tile");
44  while (tileElement) {
45  u16 tileID = tileElement->UnsignedAttribute("id");
46  u16 tileType = tileElement->UnsignedAttribute("type");
47 
48  Tile tile{tileType};
49 
50  tinyxml2::XMLElement *animationElement = tileElement->FirstChildElement("animation");
51  if (animationElement) {
52  tinyxml2::XMLElement *frameElement = animationElement->FirstChildElement("frame");
53  while (frameElement) {
54  u16 frameTileID = frameElement->UnsignedAttribute("tileid");
55  u16 frameDuration = frameElement->UnsignedAttribute("duration");
56 
57  tile.addAnimationFrame(frameTileID, frameDuration);
58 
59  frameElement = frameElement->NextSiblingElement("frame");
60  }
61  }
62 
63  m_info.push_back(tileType);
64  setTile(tileID, tile);
65 
66  tileElement = tileElement->NextSiblingElement("tile");
67  }
68 }
69 
70 } // namespace gk
71 
tinyxml2::XMLHandle FirstChildElement(const char *element)
Definition: XMLFile.hpp:30
void setTile(u16 id, Tile &tile)
Definition: Tileset.hpp:56
void loadFromFile(const std::string &filename)
Load the texture from a file on the disk.
Definition: Texture.cpp:54
unsigned short u16
Definition: IntTypes.hpp:22
#define EXCEPTION(args...)
Definition: Exception.hpp:22
void addAnimationFrame(u16 tileID, u16 duration)
Definition: Tileset.hpp:34
std::vector< u16 > m_info
Definition: Tileset.hpp:63
const std::string & filename() const
Return the filename of the texture.
Definition: Texture.hpp:108
u16 m_tileWidth
Definition: Tileset.hpp:65
Tileset()=default
u16 tileCount() const
Definition: Tileset.hpp:60
u16 m_tileHeight
Definition: Tileset.hpp:66
void load(const std::string &filename, const std::string &configFile)
Definition: Tileset.cpp:26
std::vector< Tile > m_tiles
Definition: Tileset.hpp:68