Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
TextureAtlas.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 
83  TextureAtlas(const Path& filename);
84 
94  TextureAtlas(const Path& filename, ResourceManager& resources);
95 
104  void setTexturePath(const Path& path) {
105  m_texturePath = path;
106  }
107 
114  const Path& getTexturePath() const {
115  return m_texturePath;
116  }
117 
131  void setTexture(const Texture& texture) {
132  m_texture = &texture;
133  }
134 
145  const Texture& getTexture() const {
146  return *m_texture;
147  }
148 
156  bool hasTexture() const {
157  return m_texture != nullptr;
158  }
159 
167  void unsetTexture() {
168  m_texture = nullptr;
169  }
170 
177  void addSubTexture(std::string name, const RectI& rect);
178 
186  RectI getSubTexture(const std::string& name) const;
187 
195  RectF getTextureRect(const std::string& name) const;
196 
197  private:
198  Path m_texturePath;
199  const Texture *m_texture;
200  std::map<std::string, RectI> m_rects;
201  };
202 
203 #ifndef DOXYGEN_SHOULD_SKIP_THIS
204 }
205 #endif
206 }
207 
208 #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:131
const Path & getTexturePath() const
Get the texture path.
Definition: TextureAtlas.h:114
A texture for colored images.
Definition: Texture.h:309
TextureAtlas()
Default constructor.
Definition: TextureAtlas.h:70
The namespace for gf classes.
Definition: Action.h:35
A resource manager.
Definition: ResourceManager.h:144
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
const Texture & getTexture() const
Get the source texture of the atlas.
Definition: TextureAtlas.h:145
bool hasTexture() const
Check if a texture is set.
Definition: TextureAtlas.h:156
void setTexturePath(const Path &path)
Set the texture path.
Definition: TextureAtlas.h:104
void unsetTexture()
Unset the source texture of the atlas.
Definition: TextureAtlas.h:167