MetaImGUI 1.0.0
ImGui Application Template for C++20
Loading...
Searching...
No Matches
WindowManager.h
Go to the documentation of this file.
1/*
2 MetaImGUI
3 Copyright (C) 2026 A P Nicholson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <functional>
22#include <string>
23
24// Forward declarations
25struct GLFWwindow;
26
27namespace MetaImGUI {
28
36public:
43 WindowManager(std::string title, int width, int height);
45
46 // Disable copy and move
47 WindowManager(const WindowManager&) = delete;
51
56 bool Initialize();
57
61 void Shutdown();
62
67 [[nodiscard]] bool ShouldClose() const;
68
72 void PollEvents();
73
77 void BeginFrame();
78
82 void EndFrame();
83
89 void GetFramebufferSize(int& width, int& height) const;
90
96 void GetWindowSize(int& width, int& height) const;
97
102 [[nodiscard]] GLFWwindow* GetNativeWindow() const {
103 return m_window;
104 }
105
109 void RequestClose();
110
115 void SetFramebufferSizeCallback(std::function<void(int, int)> callback);
116
121 void SetKeyCallback(std::function<void(int, int, int, int)> callback);
122
127 void SetWindowCloseCallback(std::function<void()> callback);
128
132 void CancelClose();
133
139 void SetContextLossCallback(std::function<bool()> callback);
140
145 bool ValidateContext();
146
147private:
152 bool RecreateContext();
153 // GLFW callbacks (static because GLFW is C API)
154 static void ErrorCallback(int error, const char* description);
155 static void FramebufferSizeCallbackInternal(GLFWwindow* window, int width, int height);
156 static void KeyCallbackInternal(GLFWwindow* window, int key, int scancode, int action, int mods);
157 static void WindowCloseCallbackInternal(GLFWwindow* window);
158
159 GLFWwindow* m_window = nullptr;
160 std::string m_title;
161 int m_width;
162 int m_height;
163 bool m_initialized = false;
164
165 // Context recovery
166 int m_contextRecoveryAttempts = 0;
167 static constexpr int MAX_RECOVERY_ATTEMPTS = 3;
168
169 // Callbacks
170 std::function<void(int, int)> m_framebufferSizeCallback;
171 std::function<void(int, int, int, int)> m_keyCallback;
172 std::function<void()> m_windowCloseCallback;
173 std::function<bool()> m_contextLossCallback;
174};
175
176} // namespace MetaImGUI
Manages GLFW window creation, lifecycle, and input handling.
void SetWindowCloseCallback(std::function< void()> callback)
Set window close callback.
void SetKeyCallback(std::function< void(int, int, int, int)> callback)
Set key input callback.
void GetFramebufferSize(int &width, int &height) const
Get the current framebuffer size.
bool ValidateContext()
Check if context is valid and attempt recovery if not.
void CancelClose()
Cancel a close request (clears the should close flag)
void GetWindowSize(int &width, int &height) const
Get the current window size.
void EndFrame()
Present the rendered frame.
void SetFramebufferSizeCallback(std::function< void(int, int)> callback)
Set framebuffer size callback.
WindowManager & operator=(WindowManager &&)=delete
GLFWwindow * GetNativeWindow() const
Get the GLFW window pointer (for ImGui integration)
WindowManager(const WindowManager &)=delete
void SetContextLossCallback(std::function< bool()> callback)
Set context loss callback.
void BeginFrame()
Prepare the window for a new frame.
WindowManager(WindowManager &&)=delete
void RequestClose()
Request the window to close.
bool ShouldClose() const
Check if the window should close.
void Shutdown()
Shutdown and cleanup the window.
bool Initialize()
Initialize the window and GLFW.
void PollEvents()
Poll for input events.
WindowManager & operator=(const WindowManager &)=delete