Skip to main content

GLFWApplication Class

An Application implementation for glfw-based environments. More...

Declaration

class helios::ext::glfw::app::GLFWApplication { ... }

Base class

classApplication

Class providing the entry point for a helios application. More...

Public Constructors Index

GLFWApplication (std::unique_ptr< helios::rendering::RenderingDevice > renderingDevice, std::unique_ptr< helios::input::InputManager > inputManager, std::unique_ptr< helios::event::EventManager > eventManager)

Constructs a new `GLFWApplication` instance. More...

Public Member Functions Index

voidinit () override

Initializes the application. More...

helios::ext::glfw::window::GLFWWindow &createWindow (std::unique_ptr< helios::rendering::RenderTarget > renderTarget, const helios::ext::glfw::window::GLFWWindowConfig &cfg)

Creates a new GLFWWindow and add it to this Application's windows collection. The Application takes ownership of the window. More...

helios::ext::glfw::window::GLFWWindow &createWindow (std::unique_ptr< helios::rendering::RenderTarget > renderTarget, const helios::window::WindowConfig &cfg) override

Creates the container for the native window and performs all necessary steps to properly initialize it. More...

voidsetCurrent (helios::window::Window &win) override

Sets the current window for this application. More...

helios::window::Window *current () const noexcept override

Returns the currently focused window, or nullptr if none exists or no window is being treated as current. More...

Private Member Attributes Index

helios::window::Window *current_ = nullptr

A pointer to the currently active window of the Application, or `nullptr` if there is no currently active window. More...

GLFWRAIIGuardglfwRaiiGuard_

RAII Guard for glfw initialization and termination. More...

Description

An Application implementation for glfw-based environments.

This application owns a `GLFWRAIIGuard`.

Definition at line 36 of file GLFWApplication.ixx.

Public Constructors

GLFWApplication()

helios::ext::glfw::app::GLFWApplication::GLFWApplication (std::unique_ptr< helios::rendering::RenderingDevice > renderingDevice, std::unique_ptr< helios::input::InputManager > inputManager, std::unique_ptr< helios::event::EventManager > eventManager)
inline explicit

Constructs a new `GLFWApplication` instance.

Parameters
renderingDevice

Unique rendering device owned by the application.

inputManager

Unique input manager instance.

eventManager

Unique event manager instance.

Definition at line 58 of file GLFWApplication.ixx.

59 std::unique_ptr<helios::rendering::RenderingDevice> renderingDevice,
60 std::unique_ptr<helios::input::InputManager> inputManager,
61 std::unique_ptr<helios::event::EventManager> eventManager)
63 std::move(renderingDevice),
64 std::move(inputManager),
65 std::move(eventManager)),
66 glfwRaiiGuard_() {}

References helios::app::Application::Application, helios::app::Application::eventManager, helios::app::Application::inputManager and helios::app::Application::renderingDevice.

Public Member Functions

createWindow()

helios::ext::glfw::window::GLFWWindow & helios::ext::glfw::app::GLFWApplication::createWindow (std::unique_ptr< helios::rendering::RenderTarget > renderTarget, const helios::ext::glfw::window::GLFWWindowConfig & cfg)
inline

Creates a new GLFWWindow and add it to this Application's windows collection. The Application takes ownership of the window.

Delegates to Window::show() for showing the window and initializes the associated rendering device if necessary, e.g. for glfw an additional `glfwMakeContextCurrent()` is called so the rendering device can be safely initialized, then immediately passes a `nullptr` to the `glfwMakeContextCurrent()` to force calling APIs to explicitly call setCurrent() on the constructed window. The render target size is synced to the current framebuffer dimensions reported by `glfwGetFramebufferSize()`.

Returns

Reference to the created window instance.

See Also

https://www.glfw.org/docs/latest/group__window.html#ga0e2637a4161afb283f5300c7f94785c9

Returns

A ref to the window created.

Definition at line 98 of file GLFWApplication.ixx.

99 std::unique_ptr<helios::rendering::RenderTarget> renderTarget,
101 ) {
102 auto window = std::make_unique<helios::ext::glfw::window::GLFWWindow>(std::move(renderTarget), cfg);
103
104 if (const auto glfw_window = window.get()) {
105 if (!glfw_window->show()) {
106 const std::string msg = "Cannot show window.";
107 logger_.error(msg);
108 throw std::runtime_error(msg);
109 }
110
111 // the first window inits the rendering device in this case, since glad requires
112 // context created by glfw window
113 if (!renderingDevice_->initialized()) {
114 glfwMakeContextCurrent(glfw_window->nativeHandle());
115 renderingDevice_->init();
116 // we intentionally set the previous context to NULL
117 // to enforce call to setCurrent()
118 glfwMakeContextCurrent(nullptr);
119 }
120 int width, height;
121 glfwGetFramebufferSize(glfw_window->nativeHandle(), &width, &height);
122 glfw_window->renderTarget().setSize(width, height);
123
124 } else {
125 throw std::runtime_error("Cannot create: Missing GLFWWindow.");
126 }
127
128 windowList_.emplace_back(std::move(window));
129
130 return *dynamic_cast<helios::ext::glfw::window::GLFWWindow*>(windowList_.back().get());
131 }

References helios::app::Application::logger_, helios::app::Application::renderingDevice_ and helios::app::Application::windowList_.

Referenced by createWindow.

createWindow()

helios::ext::glfw::window::GLFWWindow & helios::ext::glfw::app::GLFWApplication::createWindow (std::unique_ptr< helios::rendering::RenderTarget > renderTarget, const helios::window::WindowConfig & cfg)
inline virtual

Creates the container for the native window and performs all necessary steps to properly initialize it.

Parameters
renderTarget

Render target that wraps the framebuffer used by the window.

cfg

Window configuration defining properties such as title, dimensions, and hints.

Returns

Reference to the created window instance.

Exceptions
std::invalid_argument

If the configuration was invalid.

Definition at line 137 of file GLFWApplication.ixx.

138 std::unique_ptr<helios::rendering::RenderTarget> renderTarget,
140 ) override {
141 auto const* tmp_cfg = dynamic_cast<helios::ext::glfw::window::GLFWWindowConfig const*>(&cfg);
142 if (!tmp_cfg) {
143 std::string msg = "GLFWApplication requires GLFWWindowConfig";
144 logger_.error(msg);
145 throw std::invalid_argument(msg);
146 }
147
148 return createWindow(std::move(renderTarget), *tmp_cfg);
149 }

References createWindow and helios::app::Application::logger_.

current()

helios::window::Window * helios::ext::glfw::app::GLFWApplication::current ()
inline nodiscard noexcept virtual

Returns the currently focused window, or nullptr if none exists or no window is being treated as current.

Returns

Pointer to the current window, or nullptr if none is active.

Definition at line 195 of file GLFWApplication.ixx.

195 [[nodiscard]] helios::window::Window* current() const noexcept override {
196 return current_;
197 }

init()

void helios::ext::glfw::app::GLFWApplication::init ()
inline virtual

Initializes the application.

Performs any bootstrapping necessary for this application and provides an idiomatic entry point for APIs using this application. This method ensures that all controllers are initialized and subscribe to this application's event manager dispatcher.

Exceptions
std::runtime_error

If the application was already initialized.

Definition at line 72 of file GLFWApplication.ixx.

72 void init() override {
73 Application::init();
74
75 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
76 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
77 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
78 }

setCurrent()

void helios::ext::glfw::app::GLFWApplication::setCurrent (helios::window::Window & win)
inline virtual

Sets the current window for this application.

Ensures `glfwMakeContextCurrent()` is called to bind the correct context for subsequent glfw operations and re-registers the framebuffer size callback on the targeted window.

Parameters
win

Reference to the window that becomes the current context owner.

Exceptions
std::invalid_argument

If `win` is not a `GLFWWindow` or not owned by this application.

Definition at line 163 of file GLFWApplication.ixx.

163 void setCurrent(helios::window::Window& win) override {
164 if (!hasWindow(win)) {
165 const std::string msg = "Window is not owned by this Application.";
166 logger_.error(msg);
167 throw std::runtime_error(msg);
168 }
169
170 if (const auto glfw_window = dynamic_cast<helios::ext::glfw::window::GLFWWindow*>(&win)) {
171 logger_.info(std::format("Setting Window {0} as current", win.guid().value()));
172 // 1 makes the context of the specified window current
173 // for the calling thread (@todo extract?)
174 glfwMakeContextCurrent(glfw_window->nativeHandle());
175
176 // 2 set the framebuffersize callback
177 glfwSetFramebufferSizeCallback(
178 glfw_window->nativeHandle(),
179 glfw_window->frameBufferSizeCallback()
180 );
181
182 } else {
183 const std::string msg = "Cannot init: Missing GLFWWindow.";
184 logger_.error(msg);
185 throw std::runtime_error(msg);
186 }
187
188 inputManager_->observe(win);
189 current_ = &win;
190 }

References helios::window::Window::guid, helios::app::Application::hasWindow, helios::app::Application::inputManager_, helios::app::Application::logger_ and helios::util::Guid::value.

Private Member Attributes

current_

helios::window::Window* helios::ext::glfw::app::GLFWApplication::current_ = nullptr

A pointer to the currently active window of the Application, or `nullptr` if there is no currently active window.

Definition at line 43 of file GLFWApplication.ixx.

43 helios::window::Window* current_ = nullptr;

glfwRaiiGuard_

GLFWRAIIGuard helios::ext::glfw::app::GLFWApplication::glfwRaiiGuard_

RAII Guard for glfw initialization and termination.

Definition at line 48 of file GLFWApplication.ixx.

48 GLFWRAIIGuard glfwRaiiGuard_;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.