guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
render_handle_map.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <map>
26#include <memory>
27
28#include <utility/graphic/renderable.hpp>
29
30#include <utility/engine.hpp>
31
32#include "guillaume/ecs/entity.hpp"
33
34namespace guillaume::systems
35{
46 struct RenderHandle {
47 size_t objectId { 0 };
48 std::shared_ptr<utility::graphic::Renderable>
50 bool used { true };
51 };
52
89 {
90 private:
91 std::map<ecs::Entity::Identifier, RenderHandle>
92 _handles;
93
94 public:
98 RenderHandleMap(void) = default;
99
104 ~RenderHandleMap(void) = default;
105
110 void markAllUnused(void)
111 {
112 for (auto &[identifier, handle]: _handles) {
113 handle.used = false;
114 }
115 }
116
123 void removeUnused(utility::Engine &engine)
124 {
125 for (auto it = _handles.begin(); it != _handles.end();) {
126 if (!it->second.used) {
127 engine.removeObject(it->second.renderable,
128 it->second.objectId);
129 it = _handles.erase(it);
130 } else {
131 ++it;
132 }
133 }
134 }
135
147 {
148 auto it = _handles.find(identifier);
149 if (it == _handles.end()) {
150 return nullptr;
151 }
152 return &it->second;
153 }
154
165 void insert(const ecs::Entity::Identifier &identifier,
166 RenderHandle handle)
167 {
168 _handles.insert_or_assign(identifier, std::move(handle));
169 }
170
175 std::size_t size(void) const
176 {
177 return _handles.size();
178 }
179
185 void clear(void)
186 {
187 _handles.clear();
188 }
189 };
190
191} // namespace guillaume::systems
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
void insert(const ecs::Entity::Identifier &identifier, RenderHandle handle)
Store the handle of an entity, replacing the one it had.
void markAllUnused(void)
Clear the used flag of every handle. Call it from the system's prepare(), before any entity is drawn.
void clear(void)
Forget every handle without calling utility::Engine::removeObject. Use it only when the engine object...
RenderHandleMap(void)=default
Create an empty map.
~RenderHandleMap(void)=default
Destroy the map without removing the engine objects it refers to.
void removeUnused(utility::Engine &engine)
Remove from the engine, then from the map, every handle whose used flag is still false....
RenderHandle * find(const ecs::Entity::Identifier &identifier)
Look up the handle of an entity.
std::size_t size(void) const
Count the entities that have an engine object in this map.
Rules. Every class here runs each frame on the entities that have the components it declares.
What a render system remembers about the engine object it created for one entity.
bool used
Whether the entity was drawn this frame.
std::shared_ptr< utility::graphic::Renderable > renderable
Renderable uploaded last for this object.
size_t objectId
Identifier given by the engine.