guillaume 1.0.0
Guillaume is a component-based UI framework library built around a primitive rendering system. It's designed for building user interfaces across XR platforms and traditional platforms with custom rendering backends and input handling. The framework is lightweight, flexible, and easy to integrate into existing projects.
Loading...
Searching...
No Matches
application.hpp
1/*
2 Copyright (c) 2025 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 <iostream>
26#include <memory>
27#include <utility>
28
29#include "container.hpp"
30#include "renderer.hpp"
31
40template <typename RendererType> class Application {
41 static_assert(std::is_base_of<Renderer, RendererType>::value,
42 "RendererType must be derived from Renderer");
43
44private:
45 std::shared_ptr<RendererType> _renderer;
46 std::shared_ptr<Container> _root;
47 bool _running = false;
48
49protected:
58 void drawTree(const std::shared_ptr<Component> &component) {
59 if (!_renderer || !component)
60 return;
61
62 // Draw all primitives in this component
63 for (const auto &primitive : component->getPrimitives()) {
64 _renderer->draw(primitive);
65 }
66
67 // Then recursively draw all children
68 for (const auto &child : component->getChildren()) {
69 drawTree(child);
70 }
71 }
72
73public:
78 Application(void) : _renderer(nullptr), _root(std::make_shared<Container>()) {
79 try {
80 _renderer = std::make_shared<RendererType>();
81 } catch (std::exception &execption) {
82 std::cerr << "Failed to create renderer: " << execption.what()
83 << std::endl;
84 throw std::runtime_error("Failed to create renderer");
85 }
86 }
87
91 ~Application(void) = default;
92
98 std::shared_ptr<RendererType> getRenderer(void) const { return _renderer; }
99
105 std::shared_ptr<Container> getRoot(void) const { return _root; }
106
112 void setRoot(std::shared_ptr<Container> root) { _root = root; }
113
122 void run(void) {
123 _running = true;
124 if (_renderer) {
125 _renderer->clear();
126 }
127
128 if (_root) {
129 // Compute the virtual tree for this frame
130 _root->render();
131 // Draw the computed primitives/components
132 drawTree(_root);
133 }
134
135 if (_renderer) {
136 _renderer->present();
137 }
138 }
139
145 void update(void) {
146 if (_renderer) {
147 _renderer->clear();
148 }
149
150 if (_root) {
151 // Update the root component and trigger re-render
152 _root->render();
153 // Re-draw after the update
154 drawTree(_root);
155 }
156
157 if (_renderer) {
158 _renderer->present();
159 }
160 }
161
165 void stop(void) { _running = false; }
166};
Entry point of the application.
void setRoot(std::shared_ptr< Container > root)
Sets the root container.
void stop(void)
Stop the application run loop (if managed externally).
std::shared_ptr< RendererType > getRenderer(void) const
Gets the renderer.
void drawTree(const std::shared_ptr< Component > &component)
Recursively draw the component tree using the renderer.
Application(void)
Constructs an Application object.
std::shared_ptr< Container > getRoot(void) const
Gets the root container.
~Application(void)=default
Destroys the Application object.
void update(void)
Updates the application state.
void run(void)
Runs the application.
Specialized component to group other components.
Definition container.hpp:34