Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
TextureAtlas.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_TEXTURE_ATLAS_H
22 #define GF_TEXTURE_ATLAS_H
23 
24 #include <map>
25 #include <string>
26 
27 #include "Path.h"
28 #include "Portability.h"
29 #include "Rect.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  class Texture;
37  class ResourceManager;
38 
39  /**
40  * @ingroup game
41  * @brief A collection of sub-texture
42  *
43  * A texture atlas is a collection of sub-texture that have been packed
44  * together in a single texture. The description of the sub-textures is done
45  * in a XML file. This description is a name and the texture coordinates in
46  * the texture.
47  *
48  * Here is an example of a XML file containing an atlas:
49  *
50  * ~~~{.xml}
51  * <TextureAtlas imagePath="bricks.png">
52  * <SubTexture name="brickBlack01" x="718" y="500" width="32" height="32"/>
53  * <SubTexture name="brickBlack02" x="716" y="66" width="64" height="32"/>
54  * ...
55  * </TextureAtlas>
56  * ~~~
57  *
58  * gf::TextureAtlas is able to read the XML file and give the coordinates
59  * thanks to the name of the sub-texture, either in pixels or in texture
60  * normalized coordinates.
61  *
62  * @sa gf::Texture
63  * @sa [Texture Atlas (wikipedia)](https://en.wikipedia.org/wiki/Texture_atlas)
64  */
66  public:
67  /**
68  * @brief Default constructor
69  */
71  : m_texture(nullptr)
72  {
73 
74  }
75 
76  /**
77  * @brief Load an atlas from a XML file
78  *
79  * Load the data about the sub-textures from a XML file.
80  *
81  * @param filename The filename of the XML file
82  * @return True if the file has been loaded
83  */
84  bool loadFromFile(const Path& filename);
85 
86  /**
87  * @brief Load an atlas from a XML file
88  *
89  * Load the data about the sub-textures from a XML file. In addition, load
90  * the texture thanks to a resource manager.
91  *
92  * @param filename The filename of the XML file
93  * @param resources A resource manager
94  * @return True if the file has been loaded
95  */
96  bool loadFromFile(const Path& filename, ResourceManager& resources);
97 
98  /**
99  * @brief Set the texture path
100  *
101  * The texture path comes from the XML file when it is loaded.
102  *
103  * @param path The path to the texture
104  * @sa getTexturePath()
105  */
106  void setTexturePath(const Path& path) {
107  m_texturePath = path;
108  }
109 
110  /**
111  * @brief Get the texture path
112  *
113  * @return The path to the texture
114  * @sa setTexturePath()
115  */
116  const Path& getTexturePath() const {
117  return m_texturePath;
118  }
119 
120  /**
121  * @brief Change the source texture of the atlas
122  *
123  * The texture must exist as long as the atlas uses it. Indeed, the
124  * atlas doesn't store its own copy of the texture, but rather keeps
125  * a pointer to the one that you passed to this function.
126  * If the source texture is destroyed and the atlas tries to
127  * use it, the behavior is undefined.
128  *
129  * @param texture New texture
130  *
131  * @sa getTexture(), getTexturePath()
132  */
133  void setTexture(const Texture& texture) {
134  m_texture = &texture;
135  }
136 
137  /**
138  * @brief Get the source texture of the atlas
139  *
140  * The returned reference is const, which means that you can't
141  * modify the texture when you retrieve it with this function.
142  *
143  * @return A reference to the atlas' texture
144  *
145  * @sa setTexture()
146  */
147  const Texture& getTexture() const {
148  return *m_texture;
149  }
150 
151  /**
152  * @brief Check if a texture is set
153  *
154  * @returns True if a texture is already set
155  *
156  * @sa setTexture(), getTexture()
157  */
158  bool hasTexture() const {
159  return m_texture != nullptr;
160  }
161 
162  /**
163  * @brief Unset the source texture of the atlas
164  *
165  * After a call to this function, the atlas has no source texture.
166  *
167  * @sa setTexture()
168  */
169  void unsetTexture() {
170  m_texture = nullptr;
171  }
172 
173  /**
174  * @brief Add a sub-texture to the atlas
175  *
176  * @param name The name of the sub-texture
177  * @param rect The rectangle inside the texture
178  */
179  void addSubTexture(std::string name, const RectU& rect);
180 
181  /**
182  * @brief Get the sub-texture rectangle
183  *
184  * @param name The name of the sub-texture
185  * @return The rectangle corresponding to the name
186  * @sa getTextureRect()
187  */
188  RectU getSubTexture(const std::string& name) const;
189 
190  /**
191  * @brief Get the texture rectangle in normalized coordinates
192  *
193  * @param name The name of the sub-texture
194  * @return The texture rectangle in normalized coordinates
195  * @sa getSubTexture()
196  */
197  RectF getTextureRect(const std::string& name) const;
198 
199  private:
200  Path m_texturePath;
201  const Texture *m_texture;
202  std::map<std::string, RectU> m_rects;
203  };
204 
205 #ifndef DOXYGEN_SHOULD_SKIP_THIS
206 }
207 #endif
208 }
209 
210 #endif // GF_TEXTURE_ATLAS_H
A collection of sub-texture.
Definition: TextureAtlas.h:65
bool loadFromFile(const Path &filename, ResourceManager &resources)
Load an atlas from a XML file.
void setTexture(const Texture &texture)
Change the source texture of the atlas.
Definition: TextureAtlas.h:133
const Path & getTexturePath() const
Get the texture path.
Definition: TextureAtlas.h:116
A texture for colored images.
Definition: Texture.h:339
TextureAtlas()
Default constructor.
Definition: TextureAtlas.h:70
The namespace for gf classes.
Definition: Action.h:34
void addSubTexture(std::string name, const RectU &rect)
Add a sub-texture to the atlas.
A resource manager.
Definition: ResourceManager.h:131
const Texture & getTexture() const
Get the source texture of the atlas.
Definition: TextureAtlas.h:147
bool hasTexture() const
Check if a texture is set.
Definition: TextureAtlas.h:158
RectF getTextureRect(const std::string &name) const
Get the texture rectangle in normalized coordinates.
#define GF_API
Definition: Portability.h:35
RectU getSubTexture(const std::string &name) const
Get the sub-texture rectangle.
void setTexturePath(const Path &path)
Set the texture path.
Definition: TextureAtlas.h:106
bool loadFromFile(const Path &filename)
Load an atlas from a XML file.
void unsetTexture()
Unset the source texture of the atlas.
Definition: TextureAtlas.h:169