Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
TextureAtlas.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 "GraphicsApi.h"
28#include "Path.h"
29#include "Rect.h"
30
31namespace gf {
32#ifndef DOXYGEN_SHOULD_SKIP_THIS
33inline namespace v1 {
34#endif
35
36 class Texture;
37 class ResourceManager;
38
65 class GF_GRAPHICS_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
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 void loadXml(const Path& filename);
199
200 private:
201 Path m_texturePath;
202 const Texture *m_texture;
203 std::map<std::string, RectI> m_rects;
204 };
205
206#ifndef DOXYGEN_SHOULD_SKIP_THIS
207}
208#endif
209}
210
211#endif // GF_TEXTURE_ATLAS_H
A resource manager.
Definition: ResourceManager.h:144
A collection of sub-texture.
Definition: TextureAtlas.h:65
const Texture & getTexture() const
Get the source texture of the atlas.
Definition: TextureAtlas.h:145
void setTexture(const Texture &texture)
Change the source texture of the atlas.
Definition: TextureAtlas.h:131
RectI getSubTexture(const std::string &name) const
Get the sub-texture rectangle.
const Path & getTexturePath() const
Get the texture path.
Definition: TextureAtlas.h:114
void addSubTexture(std::string name, const RectI &rect)
Add a sub-texture to the atlas.
TextureAtlas(const Path &filename, ResourceManager &resources)
Load an atlas from a XML file.
void setTexturePath(const Path &path)
Set the texture path.
Definition: TextureAtlas.h:104
bool hasTexture() const
Check if a texture is set.
Definition: TextureAtlas.h:156
TextureAtlas(const Path &filename)
Load an atlas from a XML file.
TextureAtlas()
Default constructor.
Definition: TextureAtlas.h:70
RectF getTextureRect(const std::string &name) const
Get the texture rectangle in normalized coordinates.
void unsetTexture()
Unset the source texture of the atlas.
Definition: TextureAtlas.h:167
A texture for colored images.
Definition: Texture.h:313
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
@ Texture
A GPU texture.
The namespace for gf classes.