vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
window_manager.h
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <utility>
7#include <vector>
8
9#include "core/reflect.h"
10#include "platform/window/frame_limiter.h"
11#include "platform/input/input_handle.h"
12#include "platform/window/window_surface.h"
13
14struct GLFWwindow;
15struct GLFWmonitor;
16
17namespace Vkm::Engine {
18
19// Window-creation defaults.
20inline constexpr int DEFAULT_WINDOW_WIDTH = 1920;
21inline constexpr int DEFAULT_WINDOW_HEIGHT = 1080;
22
26enum class WindowMode {
27 Fullscreen,
28 Windowed,
29 Count
30};
31
35struct WindowSize {
36 int width = 0;
37 int height = 0;
38
39 bool operator==(const WindowSize& other) const { return width == other.width && height == other.height; }
40};
41
51std::vector<WindowSize> fittingWindowSizes(const std::vector<WindowSize>& modes, WindowSize monitor);
52
56enum class CursorMode {
57 Normal = 0,
58 Hidden = 1,
59 Disabled = 2,
60 Captured = 3
61};
62
69class WindowManager {
70 public:
71 WindowManager();
72 ~WindowManager();
73
74 WindowManager(const WindowManager& other) = delete;
75 WindowManager& operator=(const WindowManager& other) = delete;
76
77 WindowManager(WindowManager && other) = delete;
78 WindowManager& operator=(WindowManager && other) = delete;
79
80 public:
89 void createWindow(const std::string& title, std::unique_ptr<WindowSurface> surface);
90
98 void setIcon(const std::string& path);
99
105 bool shouldClose() const;
106
111
116
122 void setTitle(const std::string& title);
123
128
138 void saveScreenshot(std::string pngPath) { m_screenshotPath = std::move(pngPath); }
139
145 std::string takeScreenshotRequest() { return std::exchange(m_screenshotPath, std::string()); }
146
152 void updateMode(WindowMode windowMode);
153
163 std::vector<WindowSize> renderResolutions() const;
164
169
176
182 void setVSync(bool enabled);
183
190
198 void setCursorMode(CursorMode mode);
199
207 bool isOpen() const { return m_windowHandle != nullptr; }
208
214 WindowMode mode() const { return m_windowMode; }
215
221 bool vsync() const { return m_vsync; }
222
230 int framerate() const { return m_frameLimiter.targetFramerate(); }
231
237 CursorMode cursorMode() const { return m_cursorMode; }
238
244 InputHandle& getInputHandle() { return m_inputHandle; }
245 const InputHandle& getInputHandle() const { return m_inputHandle; }
246
254 size_t getWidth() const;
255
261 size_t getHeight() const;
262
271 float framebufferScale() const;
272
278 GLFWwindow* getWindowContext() const;
279
287 int displayHeight() const;
288
289 private:
298 void setSize(int width, int height);
299
305 int getRefreshRate() const;
306
307 private:
308 GLFWwindow* m_windowHandle = nullptr;
309 std::unique_ptr<WindowSurface> m_surface;
310 std::string m_title;
311
313 bool m_closeRequested = false;
314
315 int m_width = 0;
316 int m_height = 0;
317
318 int m_windowWidth = 0;
319 double m_cursorX = 0.0;
320 double m_cursorY = 0.0;
321 CursorMode m_cursorMode = CursorMode::Normal;
322
323 InputHandle m_inputHandle;
324 FrameLimiter m_frameLimiter;
325
326 WindowMode m_windowMode = WindowMode::Windowed;
327 bool m_vsync = false;
328 std::string m_screenshotPath;
329};
330
331} // namespace Vkm::Engine
332
333VKM_ENUM_NAMES(::Vkm::Engine::WindowMode, "Fullscreen", "Windowed")
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:214
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:230
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:237
float framebufferScale() const
Framebuffer pixels per window screen coordinate.
void swapBuffers()
Present the rendered image through the window's surface, then keep the frame cap.
void createWindow(const std::string &title, std::unique_ptr< WindowSurface > surface)
Creates the main application window with the specified title.
std::string takeScreenshotRequest()
Hand over the pending screenshot request, leaving none.
Definition window_manager.h:145
bool beginFrame()
Start the frame limiter's clock for the next frame.
void updateInput()
Updates all input states (keyboard, mouse, etc.).
bool isOpen() const
Whether a window exists at all.
Definition window_manager.h:207
std::vector< WindowSize > renderResolutions() const
The resolutions a settings screen can offer to render at: the monitor's own video modes that fit on i...
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:244
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:138
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:221
void requestClose()
Requests that the window be closed.
A window's size in pixels.
Definition window_manager.h:35