Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include <GLFW/glfw3.h>
6#include <glm/vec2.hpp>
7#include <stdexcept>
8
10
11namespace Window::Resource {
12
25class Window {
26 private:
27 std::string _title;
28 GLFWwindow *_window;
29 GLFWmonitor *_monitor;
30 GLFWwindow *_share;
31
32 bool _isFullscreen = false;
33 int _windowedX = 0;
34 int _windowedY = 0;
37
38 public:
39 Window(uint32_t width, uint32_t height, const std::string &title, GLFWmonitor *monitor = nullptr,
40 GLFWwindow *share = nullptr);
41
45 void Destroy();
46
52 inline bool ShouldClose() const { return glfwWindowShouldClose(_window); }
53
59 inline GLFWwindow *GetGLFWWindow() const { return _window; }
60
66 inline void SetTitle(std::string_view title)
67 {
68 _title = title;
69 glfwSetWindowTitle(_window, _title.c_str());
70 }
71
79 glm::uvec2 GetSize() const;
80
87 void SetSize(int width, int height);
88
92 void ToggleFullscreen();
93
99 inline glm::vec2 GetMousePosition()
100 {
101 double x = 0;
102 double y = 0;
103 glm::ivec2 windowSize = GetSize();
104 glfwGetCursorPos(_window, &x, &y);
105 y = windowSize.y - y;
106 return {x, y};
107 }
108
109 inline void SetResizable(bool resizable)
110 {
111 glfwSetWindowAttrib(_window, GLFW_RESIZABLE, resizable ? GLFW_TRUE : GLFW_FALSE);
112 }
113
117 inline void MaskCursor() { glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); }
118
122 inline void ShowCursor() { glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); }
123
127 inline bool IsCursorMasked() const { return glfwGetInputMode(_window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED; }
128};
129
130} // namespace Window::Resource
bool _isFullscreen
Definition Window.hpp:32
int _windowedWidth
Definition Window.hpp:35
GLFWwindow * _window
Definition Window.hpp:28
void MaskCursor()
Hide the cursor and lock it to the window.
Definition Window.hpp:117
GLFWwindow * GetGLFWWindow() const
Get a pointer to the GLFW window.
Definition Window.hpp:59
int _windowedX
Definition Window.hpp:33
bool ShouldClose() const
Check if the window should close.
Definition Window.hpp:52
bool IsCursorMasked() const
Check if the cursor is currently masked (hidden and locked).
Definition Window.hpp:127
int _windowedHeight
Definition Window.hpp:36
GLFWmonitor * _monitor
Definition Window.hpp:29
void ShowCursor()
Show the cursor and unlock it from the window.
Definition Window.hpp:122
GLFWwindow * _share
Definition Window.hpp:30
void Destroy()
Destroy the window.
Definition Window.cpp:14
glm::vec2 GetMousePosition()
Get the current mouse position.
Definition Window.hpp:99
std::string _title
Definition Window.hpp:27
void SetResizable(bool resizable)
Definition Window.hpp:109
int _windowedY
Definition Window.hpp:34
glm::uvec2 GetSize() const
Get the window size.
Definition Window.cpp:23
void SetTitle(std::string_view title)
Set the window title.
Definition Window.hpp:66
Definition Window.cpp:4
Definition OnResize.hpp:12