lxgui
gui_atlas.hpp
1 #ifndef LXGUI_GUI_ATLAS_HPP
2 #define LXGUI_GUI_ATLAS_HPP
3 
4 #include "lxgui/gui_bounds2.hpp"
5 #include "lxgui/gui_material.hpp"
6 #include "lxgui/lxgui.hpp"
7 #include "lxgui/utils.hpp"
8 
9 #include <memory>
10 #include <optional>
11 #include <string>
12 #include <unordered_map>
13 #include <vector>
14 
15 namespace lxgui::gui {
16 
17 class renderer;
18 class font;
19 
25 class atlas_page {
26 public:
31  explicit atlas_page(material::filter filt);
32 
34  virtual ~atlas_page() = default;
35 
37  atlas_page(const atlas_page&) = delete;
38 
40  atlas_page(atlas_page&&) = delete;
41 
43  atlas_page& operator=(const atlas_page&) = delete;
44 
47 
53  std::shared_ptr<material> fetch_material(const std::string& file_name) const;
54 
61  std::shared_ptr<material> add_material(const std::string& file_name, const material& mat);
62 
68  std::shared_ptr<font> fetch_font(const std::string& font_name) const;
69 
76  bool add_font(const std::string& font_name, std::shared_ptr<gui::font> fnt);
77 
82  bool empty() const;
83 
84 protected:
91  virtual std::shared_ptr<material>
92  add_material_(const material& mat, const bounds2f& location) = 0;
93 
98  virtual float get_width_() const = 0;
99 
104  virtual float get_height_() const = 0;
105 
107 
108 private:
115  std::optional<bounds2f> find_location_(float width, float height) const;
116 
117  std::unordered_map<std::string, std::weak_ptr<gui::material>> texture_list_;
118  std::unordered_map<std::string, std::weak_ptr<gui::font>> font_list_;
119 };
120 
126 class atlas {
127 public:
133  explicit atlas(renderer& rdr, material::filter filt);
134 
136  virtual ~atlas() = default;
137 
139  atlas(const atlas&) = delete;
140 
142  atlas(atlas&&) = delete;
143 
145  atlas& operator=(const atlas&) = delete;
146 
148  atlas& operator=(atlas&&) = delete;
149 
155  std::shared_ptr<material> fetch_material(const std::string& file_name) const;
156 
163  std::shared_ptr<material> add_material(const std::string& file_name, const material& mat);
164 
170  std::shared_ptr<font> fetch_font(const std::string& font_name) const;
171 
178  bool add_font(const std::string& font_name, std::shared_ptr<gui::font> fnt);
179 
184  std::size_t get_page_count() const;
185 
186 protected:
191  virtual std::unique_ptr<atlas_page> create_page_() = 0;
192 
195 
196 private:
201  void add_page_();
202 
203  struct page_item {
204  std::unique_ptr<atlas_page> page;
205  std::shared_ptr<material> no_texture_mat;
206  };
207 
208  std::vector<page_item> page_list_;
209 };
210 
211 } // namespace lxgui::gui
212 
213 #endif
A single texture holding multiple materials for efficient rendering.
Definition: gui_atlas.hpp:25
material::filter filter_
Definition: gui_atlas.hpp:106
virtual std::shared_ptr< material > add_material_(const material &mat, const bounds2f &location)=0
Adds a new material to this page, at the provided location.
atlas_page & operator=(const atlas_page &)=delete
Non-copiable.
atlas_page(material::filter filt)
Constructor.
Definition: gui_atlas.cpp:12
std::shared_ptr< material > add_material(const std::string &file_name, const material &mat)
Creates a new material from a texture file.
Definition: gui_atlas.cpp:25
std::shared_ptr< font > fetch_font(const std::string &font_name) const
Find a font in this page (nullptr if not found).
Definition: gui_atlas.cpp:41
virtual ~atlas_page()=default
Destructor.
bool add_font(const std::string &font_name, std::shared_ptr< gui::font > fnt)
Creates a new font from a texture file.
Definition: gui_atlas.cpp:51
virtual float get_height_() const =0
Return the height of this page (in pixels).
atlas_page & operator=(atlas_page &&)=delete
Non-movable.
atlas_page(atlas_page &&)=delete
Non-movable.
std::shared_ptr< material > fetch_material(const std::string &file_name) const
Find a material in this page (nullptr if not found).
Definition: gui_atlas.cpp:14
virtual float get_width_() const =0
Return the width of this page (in pixels).
atlas_page(const atlas_page &)=delete
Non-copiable.
bool empty() const
Checks if this page is empty (contains no materials).
Definition: gui_atlas.cpp:72
A class that holds multiple materials for efficient rendering.
Definition: gui_atlas.hpp:126
virtual ~atlas()=default
Destructor.
std::size_t get_page_count() const
Return the number of pages in this atlas.
Definition: gui_atlas.cpp:230
atlas & operator=(const atlas &)=delete
Non-copiable.
atlas(const atlas &)=delete
Non-copiable.
std::shared_ptr< material > add_material(const std::string &file_name, const material &mat)
Add a new material to the atlas.
Definition: gui_atlas.cpp:172
virtual std::unique_ptr< atlas_page > create_page_()=0
Create a new page in this atlas.
bool add_font(const std::string &font_name, std::shared_ptr< gui::font > fnt)
Add a new font to the atlas.
Definition: gui_atlas.cpp:208
std::shared_ptr< material > fetch_material(const std::string &file_name) const
Find a material in this atlas (nullptr if not found).
Definition: gui_atlas.cpp:161
atlas & operator=(atlas &&)=delete
Non-movable.
material::filter filter_
Definition: gui_atlas.hpp:194
atlas(atlas &&)=delete
Non-movable.
atlas(renderer &rdr, material::filter filt)
Constructor.
Definition: gui_atlas.cpp:159
renderer & renderer_
Definition: gui_atlas.hpp:193
std::shared_ptr< font > fetch_font(const std::string &font_name) const
Find a font in this atlas (nullptr if not found).
Definition: gui_atlas.cpp:198
A class that holds rendering data.
Abstract type for implementation specific management.