Gamedev Framework (gf)  0.5.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 
65  class GF_API TextureAtlas {
66  public:
71  : m_texture(nullptr)
72  {
73 
74  }
75 
84  bool loadFromFile(const Path& filename);
85 
96  bool loadFromFile(const Path& filename, ResourceManager& resources);
97 
106  void setTexturePath(const Path& path) {
107  m_texturePath = path;
108  }
109 
116  const Path& getTexturePath() const {
117  return m_texturePath;
118  }
119 
133  void setTexture(const Texture& texture) {
134  m_texture = &texture;
135  }
136 
147  const Texture& getTexture() const {
148  return *m_texture;
149  }
150 
158  bool hasTexture() const {
159  return m_texture != nullptr;
160  }
161 
169  void unsetTexture() {
170  m_texture = nullptr;
171  }
172 
179  void addSubTexture(std::string name, const RectU& rect);
180 
188  RectU getSubTexture(const std::string& name) const;
189 
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
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
A resource manager.
Definition: ResourceManager.h:131
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41
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
void setTexturePath(const Path &path)
Set the texture path.
Definition: TextureAtlas.h:106
void unsetTexture()
Unset the source texture of the atlas.
Definition: TextureAtlas.h:169