vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
window_manager.h
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <utility>
6
7#include "platform/window/frame_limiter.h"
8#include "platform/input/input_handle.h"
9
10struct GLFWwindow;
11struct GLFWmonitor;
12
13namespace Vkm::Engine {
14
15// Window-creation defaults; the GL version is here because WindowManager creates the context.
16inline constexpr int OPENGL_MAJOR_VERSION = 4;
17inline constexpr int OPENGL_MINOR_VERSION = 3;
19inline constexpr int OPENGL_GLSL_VERSION = OPENGL_MAJOR_VERSION * 100 + OPENGL_MINOR_VERSION * 10;
20inline constexpr int DEFAULT_WINDOW_WIDTH = 1920;
21inline constexpr int DEFAULT_WINDOW_HEIGHT = 1080;
22
26enum class WindowMode {
27 Fullscreen = 1,
28 Windowed = 2
29};
30
34enum class CursorMode {
35 Normal = 0,
36 Hidden = 1,
37 Disabled = 2,
38 Captured = 3
39};
40
47constexpr const char* toString(WindowMode type) {
48 switch (type) {
49 case WindowMode::Fullscreen: return "Fullscreen";
50 case WindowMode::Windowed: return "Windowed";
51 default: return "UNKNOWN";
52 }
53}
54
61class WindowManager {
62 public:
63 WindowManager();
64 ~WindowManager();
65
66 WindowManager(const WindowManager& other) = delete;
67 WindowManager& operator=(const WindowManager& other) = delete;
68
69 WindowManager(WindowManager && other) = delete;
70 WindowManager& operator=(WindowManager && other) = delete;
71
72 public:
78 void createWindow(const std::string& title);
79
87 void setIcon(const std::string& path);
88
94 bool shouldClose() const;
95
100
105
111 void setTitle(const std::string& title);
112
117
127 void saveScreenshot(std::string pngPath) { m_screenshotPath = std::move(pngPath); }
128
134 std::string takeScreenshotRequest() { return std::exchange(m_screenshotPath, std::string()); }
135
141 void updateMode(WindowMode windowMode);
142
147
154
160 void setVSync(bool enabled);
161
168
176 void setCursorMode(CursorMode mode);
177
185 bool isOpen() const { return m_windowHandle != nullptr; }
186
192 WindowMode mode() const { return m_windowMode; }
193
199 bool vsync() const { return m_vsync; }
200
208 int framerate() const { return m_frameLimiter.targetFramerate(); }
209
215 CursorMode cursorMode() const { return m_cursorMode; }
216
222 InputHandle& getInputHandle() { return m_inputHandle; }
223 const InputHandle& getInputHandle() const { return m_inputHandle; }
224
232 size_t getWidth() const;
233
239 size_t getHeight() const;
240
249 float framebufferScale() const;
250
256 GLFWwindow* getWindowContext() const;
257
265 int displayHeight() const;
266
267 private:
276 void setSize(int width, int height);
277
283 int getRefreshRate() const;
284
285 private:
286 GLFWwindow* m_windowHandle = nullptr;
287 std::string m_title;
288
290 bool m_closeRequested = false;
291
292 int m_width = 0;
293 int m_height = 0;
294
295 int m_windowWidth = 0;
296 double m_cursorX = 0.0;
297 double m_cursorY = 0.0;
298 CursorMode m_cursorMode = CursorMode::Normal;
299
300 InputHandle m_inputHandle;
301 FrameLimiter m_frameLimiter;
302
303 WindowMode m_windowMode = WindowMode::Windowed;
304 bool m_vsync = false;
305 std::string m_screenshotPath;
306};
307
308} // namespace Vkm::Engine
Limits a render loop's frame rate by sleeping, then spin-waiting.
Definition frame_limiter.h:12
One frame of raw device state: keys and buttons down, cursor position and wheel turn.
Definition input_handle.h:22
void setTitle(const std::string &title)
Update the window title.
int displayHeight() const
Height in pixels of the monitor this window is on.
void updateMode(WindowMode windowMode)
Changes the current window mode (fullscreen or windowed).
WindowMode mode() const
The window mode last applied by updateMode.
Definition window_manager.h:192
void cancelClose()
Cancel a pending close, e.g. behind an unsaved-changes guard.
int framerate() const
The frame cap currently in effect.
Definition window_manager.h:208
void setVSync(bool enabled)
Enables or disables vertical synchronization (VSync).
GLFWwindow * getWindowContext() const
Get the underlying GLFW window pointer.
CursorMode cursorMode() const
The cursor mode last set.
Definition window_manager.h:215
float framebufferScale() const
Framebuffer pixels per window screen coordinate.
void swapBuffers()
Swaps the front and back buffers of the window, presenting the rendered image.
std::string takeScreenshotRequest()
Hand over the pending screenshot request, leaving none.
Definition window_manager.h:134
bool beginFrame()
Start the frame limiter's clock for the next frame.
void createWindow(const std::string &title)
Creates the main application window with the specified title.
void updateInput()
Updates all input states (keyboard, mouse, etc.).
bool isOpen() const
Whether a window exists at all.
Definition window_manager.h:185
bool shouldClose() const
Checks if the window close event has been triggered.
size_t getWidth() const
Get the framebuffer width in pixels.
InputHandle & getInputHandle()
Get the current input handle for querying input state.
Definition window_manager.h:222
void setCursorMode(CursorMode mode)
Sets the cursor mode.
void setIcon(const std::string &path)
Sets the window/taskbar icon from an image file (PNG, etc.).
size_t getHeight() const
Get the framebuffer height in pixels; see getWidth().
void saveScreenshot(std::string pngPath)
Write the next frame the game draws to pngPath, once.
Definition window_manager.h:127
void setFramerate(int framerate)
Sets the maximum framerate for the render loop.
bool vsync() const
Whether vsync was last enabled via setVSync.
Definition window_manager.h:199
void requestClose()
Requests that the window be closed.