galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
TextureAtlas.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_GRAPHICS_TEXTUREATLAS_HPP_
9#define GALAXY_GRAPHICS_TEXTUREATLAS_HPP_
10
11#include <span>
12#include <string_view>
13
14#include <robin_hood.h>
15
18#include "galaxy/graphics/TexelRegion.hpp"
19#include "galaxy/graphics/VertexArray.hpp"
21
22namespace galaxy
23{
24 namespace resource
25 {
29 class TextureAtlas final
30 {
31 public:
35 struct Sheet final
36 {
40 math::RectPack m_packer;
41
46 };
47
51 struct Info final
52 {
56 math::iRect m_region;
57
61 graphics::TexelRegion m_texel_region;
62
66 int m_index = 0;
67
71 int m_handle = 0;
72
77
82 };
83
88
92 virtual ~TextureAtlas();
93
99 void add(const std::string& file);
100
104 void add_from_vfs();
105
111 void save();
112
116 void clear();
117
125 [[nodiscard]]
126 bool contains(const std::string& key);
127
135 [[nodiscard]]
136 meta::optional_ref<Info> query(const std::string& key);
137
148 template<meta::is_arithmetic Type>
149 [[nodiscard]]
150 static float map_x_texel(const Type x, const Type width);
151
162 template<meta::is_arithmetic Type>
163 [[nodiscard]]
164 static float map_y_texel(const Type y, const Type height);
165
171 [[nodiscard]]
172 meta::vector<std::string> keys();
173
174 private:
179
184
188 TextureAtlas(const TextureAtlas&) = delete;
189
194
198 void init();
199
200 private:
205
210
214 meta::vector<std::unique_ptr<Sheet>> m_sheets;
215
219 robin_hood::unordered_flat_map<std::string, Info> m_data;
220
224 graphics::VertexArray m_vao;
225
230 };
231
232 template<meta::is_arithmetic Type>
233 inline float TextureAtlas::map_x_texel(const Type x, const Type width)
234 {
235 if constexpr (std::is_floating_point<Type>::value)
236 {
237 return x / width;
238 }
239 else
240 {
241 return static_cast<float>(x) / static_cast<float>(width);
242 }
243 }
244
245 template<meta::is_arithmetic Type>
246 inline float TextureAtlas::map_y_texel(const Type y, const Type height)
247 {
248 if constexpr (std::is_floating_point<Type>::value)
249 {
250 return y / height;
251 }
252 else
253 {
254 return static_cast<float>(y) / static_cast<float>(height);
255 }
256 }
257 } // namespace resource
258} // namespace galaxy
259
260#endif
Defines the 2D transformation of an entity.
Definition Transform.hpp:22
Draw to an opengl texture instead of the screen.
Parses raw texture files and stiches them into a large altas.
virtual ~TextureAtlas()
Destructor.
robin_hood::unordered_flat_map< std::string, Info > m_data
Index'd list of textures on a sheet.
void init()
Initialize atlas.
TextureAtlas(const TextureAtlas &)=delete
Copy constructor.
int m_size
Maxinum size of an atlas texture.
TextureAtlas(TextureAtlas &&)=delete
Move constructor.
components::Transform m_transform
Default transform to use when building an atlas.
graphics::VertexArray m_vao
Default vertex array to use when building an atlas.
meta::vector< std::string > keys()
Get a list of keys in the cache.
void add_from_vfs()
Loads all atlas textures in the vfs.
void clear()
Clear all data.
int m_max_bindings
Max number of active textures allowed.
void save()
Save all created atlas' to disk.
meta::vector< std::unique_ptr< Sheet > > m_sheets
Texture atlas sheets.
meta::optional_ref< Info > query(const std::string &key)
Get data about texture in atlas.
static float map_y_texel(const Type y, const Type height)
Takes in a y positon texture coord and maps it to a texel.
void add(const std::string &file)
Add a single file.
TextureAtlas & operator=(const TextureAtlas &)=delete
Copy assignment operator.
static float map_x_texel(const Type x, const Type width)
Takes in a x positon texture coord and maps it to a texel.
bool contains(const std::string &key)
Check if atlas contains a texture.
TextureAtlas & operator=(TextureAtlas &&)=delete
Move assignment operator.
Animated.cpp galaxy.
Definition Animated.cpp:16
Information about textures stored in atlas.
int m_sheet_width
Texture sheet width.
math::iRect m_region
Region of the texture in the atlas.
int m_handle
Texture handle this texture belongs to.
int m_sheet_height
Texture sheet height.
graphics::TexelRegion m_texel_region
Texture region in OpenGL texels.
int m_index
Index of which atlas this sprite belongs to.
A sheet of textures in the atlas.
graphics::RenderTexture m_render_texture
Texture to combine to.
math::RectPack m_packer
Rectangle packing algorithm structure.