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]] bool contains(const std::string& key);
126
134 [[nodiscard]] meta::optional_ref<Info> query(const std::string& key);
135
146 template<meta::is_arithmetic Type>
147 [[nodiscard]] static float map_x_texel(const Type x, const Type width);
148
159 template<meta::is_arithmetic Type>
160 [[nodiscard]] static float map_y_texel(const Type y, const Type height);
161
167 [[nodiscard]] meta::vector<std::string> keys();
168
169 private:
174
179
183 TextureAtlas(const TextureAtlas&) = delete;
184
189
193 void init();
194
195 private:
200
205
209 meta::vector<std::unique_ptr<Sheet>> m_sheets;
210
214 robin_hood::unordered_flat_map<std::string, Info> m_data;
215
220
225 };
226
227 template<meta::is_arithmetic Type>
228 inline float TextureAtlas::map_x_texel(const Type x, const Type width)
229 {
230 if constexpr (std::is_floating_point<Type>::value)
231 {
232 return x / width;
233 }
234 else
235 {
236 return static_cast<float>(x) / static_cast<float>(width);
237 }
238 }
239
240 template<meta::is_arithmetic Type>
241 inline float TextureAtlas::map_y_texel(const Type y, const Type height)
242 {
243 if constexpr (std::is_floating_point<Type>::value)
244 {
245 return y / height;
246 }
247 else
248 {
249 return static_cast<float>(y) / static_cast<float>(height);
250 }
251 }
252 } // namespace resource
253} // namespace galaxy
254
255#endif
Defines the 2D transformation of an entity.
Definition Transform.hpp:22
Draw to an opengl texture instead of the screen.
Abstraction for OpenGL vertex array objects.
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.