Skip to main content

GLFWWindow.ixx File

GLFW-based Window implementation. More...

Included Headers

#include <format> #include <GLFW/glfw3.h> #include <string> #include <helios.rendering.RenderTarget> #include <helios.ext.glfw.window.GLFWWindowConfig> #include <helios.ext.glfw.window.GLFWWindowUserPointer> #include <helios.window.Window>

Namespaces Index

namespacehelios
namespaceext

Platform-specific extensions and backend implementations. More...

namespaceglfw

GLFW-specific implementations. More...

namespacewindow

GLFW window management. More...

Classes Index

classGLFWWindow

An OpenGL focused window implementation using GLFW. More...

Description

GLFW-based Window implementation.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file GLFWWindow.ixx
3 * @brief GLFW-based Window implementation.
4 */
5module;
6
7#include <format>
8#include <GLFW/glfw3.h>
9#include <string>
10
11export module helios.ext.glfw.window.GLFWWindow;
12
13import helios.window.Window;
14
15import helios.ext.glfw.window.GLFWWindowUserPointer;
16import helios.ext.glfw.window.GLFWWindowConfig;
17import helios.rendering.RenderTarget;
18
19export namespace helios::ext::glfw::window {
20
21 /**
22 * @brief An OpenGL focused window implementation using GLFW.
23 *
24 * This class manages a `GLFWwindow` handle, providing methods for
25 * creation, display, event polling and resource cleanup.
26 *
27 * @todo add glfwSetWindowRefreshCallback
28 */
30
31 private:
32 /**
33 * @brief The native GLFWwindow handle.
34 */
35 GLFWwindow* nativeHandle_ = nullptr;
36
37
38 /**
39 * @brief C-style callback function for framebuffer resize events.
40 * This function pointer is passed to `glfwSetFramebufferSizeCallback`.
41 */
42 GLFWframebuffersizefun frameBufferSizeCallback_;
43
44 /**
45 * @brief A unique_ptr to the to an object holding user defined data for the native GLFWwindow.
46 * This information gets forwarded using `glfwSetWindowUserPointer`
47 */
48 std::unique_ptr<GLFWWindowUserPointer> windowUserPointer_;
49
50 public:
51 /**
52 * @brief Do not copy instances of GLFWWindow.
53 */
54 GLFWWindow(const GLFWWindow&) = delete;
55 GLFWWindow& operator=(const GLFWWindow&) = delete;
56
57 /**
58 * @brief Destructor. Delegates to #destroy()
59 */
60 ~GLFWWindow() override {
61 destroy();
62 }
63
64
65 /**
66 * @brief Constructs a new GLFWWindow based in the provided configuration.
67 * The underlying native window is not created until #show() is called.
68 *
69 * @param cfg A const ref to this window's configuration-
70 */
71 explicit GLFWWindow(
72 std::unique_ptr<helios::rendering::RenderTarget> renderTarget,
73 const GLFWWindowConfig& cfg
74 ) :
75 Window(std::move(renderTarget), cfg),
76 frameBufferSizeCallback_(cfg.frameBufferSizeCallback) {
77 }
78
79 /********************
80 * Overrides
81 *******************/
82
83 /**
84 * @brief Creates and displays the underlying native `GlFWwindow`.
85 *
86 * This method initializes the native window. It does not set the current context,
87 * which is actually done by GLFWApplication::setCurrent().
88 *
89 * @see https://www.glfw.org/docs/latest/group__window.html#ga3555a418df92ad53f917597fe2f64aeb
90 */
91 bool show() noexcept override {
92 if (nativeHandle_ != nullptr) {
93 logger_.warn("Window already shown.");
94 return true;
95 }
96
97 logger_.info("Calling glfwCreateWindow().");
98 nativeHandle_ = glfwCreateWindow(
99 width_, height_, title_.c_str(), nullptr, nullptr);
100
101 if (aspectRatioDenom_ > 0 && aspectRatioNumer_ > 0) {
102 glfwSetWindowAspectRatio(nativeHandle_, aspectRatioNumer_, aspectRatioDenom_);
103 }
104
105 if (nativeHandle_ == nullptr) {
106 logger_.error("Failed to create GLFW window");
107 return false;
108 }
109
110 return true;
111 }
112
113
114 /**
115 * @brief Swaps the front and back buffers of the window.
116 *
117 * @see https://www.glfw.org/docs/latest/group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14
118 */
119 void swapBuffers() const noexcept override {
120 glfwSwapBuffers(nativeHandle_);
121 }
122
123
124 /**
125 * @brief Processes all events in the underlying Window's EventQueue.
126 *
127 * This method should be called regularly in the main loop.
128 *
129 * @see https://www.glfw.org/docs/latest/group__window.html#ga37bd57223967b4211d60ca1a0bf3c832
130 */
131 void pollEvents() const noexcept override {
132 glfwPollEvents();
133 }
134
135
136 /**
137 * @copydoc helios::window::Window::setShouldClose()
138 *
139 * @see https://www.glfw.org/docs/latest/group__window.html#ga49c449dde2a6f87d996f4daaa09d6708
140 */
141 void setShouldClose(bool close) override {
142 glfwSetWindowShouldClose(nativeHandle_, close);
143 }
144
145
146 /**
147 * @copydoc helios::window::Window::shouldClose()
148 *
149 * @see https://www.glfw.org/docs/latest/group__window.html#ga24e02fbfefbb81fc45320989f8140ab5
150 */
151 [[nodiscard]] bool shouldClose() const override {
152 if (nativeHandle_ == nullptr) {
153 return true;
154 }
155 return glfwWindowShouldClose(nativeHandle_);
156 }
157
158
159 /********************
160 * Specifics
161 *******************/
162 /**
163 * @brief Returns the framebuffer resize callback function.
164 *
165 * @return the `GLFWframebuffersizefun` currently registered with this Window.
166 */
167 [[nodiscard]] GLFWframebuffersizefun frameBufferSizeCallback() const noexcept {
168 return frameBufferSizeCallback_;
169 }
170
171
172 /**
173 * @brief Sets the callback function for framebuffer resize events.
174 *
175 * Implementing APIs should consider this method as "package protected": It's purpose is
176 * to register a function that delegates the native event into the Application's
177 * EventQueue.
178 *
179 * @param framebufferSizeCallback The `GLFWframebuffersizefun` to set.
180 */
181 void setFrameBufferSizeCallback(GLFWframebuffersizefun framebufferSizeCallback) noexcept {
182 frameBufferSizeCallback_ = framebufferSizeCallback;
183 }
184
185
186 /**
187 * @brief Returns the raw native GLFWwindow handle.
188 *
189 * This method should only be used in GLFW-related API parts, where underlying
190 * glfw-functions require the native window handle.
191 *
192 * @return The `GLFWwindow*` handle, or a `nullptr` if not available.
193 */
194 [[nodiscard]] GLFWwindow* nativeHandle() const noexcept {
195 return nativeHandle_;
196 }
197
198
199 /**
200 * @brief Removed the underlying native handle of the GLFWwindow.
201 *
202 * @see https://www.glfw.org/docs/latest/group__window.html#gacdf43e51376051d2c091662e9fe3d7b2
203 */
204 void destroy() const {
205 if (nativeHandle_) {
206 glfwDestroyWindow(nativeHandle_);
207 }
208 }
209
210
211 /**
212 * @brief Sets the user-defined pointer for his GLFWWindow.
213 *
214 * This transfers ownership of the `GLFWWindowUserPointer`-object to this
215 * Window and additionally associates its raw pointer with this Window's
216 * native handle.
217 *
218 * @param windowUserPointer A unique_ptr to the `GLFWWindowUserPointer` object.
219 * Ownership is transferred to this Window-instance.
220 * @return
221 */
222 void setWindowUserPointer(std::unique_ptr<GLFWWindowUserPointer> windowUserPointer) noexcept {
223 windowUserPointer_ = std::move(windowUserPointer);
224 glfwSetWindowUserPointer(nativeHandle_, windowUserPointer_.get());
225 }
226
227
228 /**
229 * @brief Returns a const reference to the `GLFWWindowUserPointer` managed by this class.
230 *
231 * @return A const reference to this object's `GLFWWindowUserPointer`.
232 */
233 [[nodiscard]] const GLFWWindowUserPointer& windowUserPointer() const noexcept {
234 return *windowUserPointer_;
235 }
236 };
237
238} // namespace helios::ext::glfw::window

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.