Loading...
Searching...
No Matches
GLFWWindow.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 18/09/2025.
3//
4#include "GLFWWindow.h"
5
6namespace hellfire {
7 bool GLFWWindow::create(const int width, const int height, const std::string &title) {
8 if (!initialized_) {
9 if (!glfwInit()) {
10 return false;
11 }
12 initialized_ = true;
13 }
14
15 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
16 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
17 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
18
19 window_ = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
20 if (!window_) {
21 return false;
22 }
23
24 glfwMakeContextCurrent(window_);
25 glfwSetWindowUserPointer(window_, this);
26
27
28 // Set callbacks
29 glfwSetKeyCallback(window_, key_callback);
30 glfwSetMouseButtonCallback(window_, mouse_button_callback);
31 glfwSetCursorPosCallback(window_, cursor_position_callback);
32 glfwSetWindowSizeCallback(window_, window_size_callback);
33 glfwSetWindowIconifyCallback(window_, window_iconifiy_callback);
34
35 return true;
36 }
37
38 void GLFWWindow::destroy() {
39 if (window_) {
40 glfwDestroyWindow(window_);
41 window_ = nullptr;
42 }
43
44 if (initialized_) {
45 glfwTerminate();
46 initialized_ = false;
47 }
48 }
49
50 void GLFWWindow::swap_buffers() {
51 if (window_) {
52 glfwSwapBuffers(window_);
53 }
54 }
55
56 void GLFWWindow::poll_events() {
57 if (window_) {
58 glfwPollEvents();
59 }
60 }
61
62 void GLFWWindow::wait_for_events() {
63 glfwWaitEvents();
64 }
65
66 void GLFWWindow::set_title(const std::string &title) {
67 window_info_.title = title;
68 if (window_) {
69 glfwSetWindowTitle(window_, title.c_str());
70 }
71 }
72
73 void GLFWWindow::set_size(const int width, const int height) {
74 window_info_.width = width;
75 window_info_.height = height;
76 if (window_) {
77 glfwSetWindowSize(window_, width, height);
78 }
79 }
80
81 glm::ivec2 GLFWWindow::get_size() const {
83 }
84
85 bool GLFWWindow::should_close() const {
86 return window_ && glfwWindowShouldClose(window_);
87 }
88
89 void *GLFWWindow::get_native_handle() {
90 return window_;
91 }
92
93 glm::ivec2 GLFWWindow::get_framebuffer_size() const {
94 if (window_) {
95 int width, height;
96 glfwGetFramebufferSize(window_, &width, &height);
97 return {width, height};
98 }
99 return {0, 0};
100 }
101
102 bool GLFWWindow::is_key_pressed(const int keycode) const {
103 if (window_) {
104 return glfwGetKey(window_, keycode) == GLFW_PRESS;
105 }
106 return false;
107 }
108
109 glm::vec2 GLFWWindow::get_mouse_position() const {
110 if (window_) {
111 double x, y;
112 glfwGetCursorPos(window_, &x, &y);
113 return {static_cast<float>(x), static_cast<float>(y)};
114 }
115 return {0, 0};
116 }
117
118 void GLFWWindow::make_current() {
119 if (window_) {
120 glfwMakeContextCurrent(window_);
121 }
122 }
123
124 void GLFWWindow::key_callback(GLFWwindow *window, const int key, int scancode, const int action, int mods) {
125 if (const auto *instance = static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
126 instance && instance->event_handler_) {
127 if (action == GLFW_PRESS) {
128 instance->event_handler_->on_key_down(key);
129 } else if (action == GLFW_RELEASE) {
130 instance->event_handler_->on_key_up(key);
131 }
132 }
133 }
134
135 void GLFWWindow::mouse_button_callback(GLFWwindow *window, const int button, const int action, int mods) {
136 if (const auto *instance = static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
137 instance && instance->event_handler_) {
138 if (action == GLFW_PRESS) {
139 instance->event_handler_->on_mouse_button(button, true);
140 } else if (action == GLFW_RELEASE) {
141 instance->event_handler_->on_mouse_button(button, false);
142 }
143 }
144 }
145
146 void GLFWWindow::cursor_position_callback(GLFWwindow *window, const double x, const double y) {
147 if (const auto *instance = static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window));
148 instance && instance->event_handler_) {
149 instance->event_handler_->on_mouse_move(static_cast<float>(x), static_cast<float>(y));
150 }
151 }
152
153 void GLFWWindow::window_size_callback(GLFWwindow *window, const int width, const int height) {
154 if (auto *instance = static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window))) {
155 instance->window_info_.width = width;
156 instance->window_info_.height = height;
157
158 if (instance->event_handler_) {
159 instance->event_handler_->on_window_resize(width, height);
160 }
161 }
162 }
163
164 void GLFWWindow::window_iconifiy_callback(GLFWwindow *window, const int iconified) {
165 if (const auto *instance = static_cast<GLFWWindow *>(glfwGetWindowUserPointer(window))) {
166 if (instance->event_handler_) {
167 const bool minimized = iconified == GLFW_TRUE;
168 instance->event_handler_->on_window_minimize(minimized);
169 }
170 }
171 }
172
173 void GLFWWindow::set_event_handler(IWindowEventHandler *handler) {
174 event_handler_ = handler;
175 }
176
177 float GLFWWindow::get_elapsed_time() {
178 return static_cast<float>(glfwGetTime());
179 }
180
181 void GLFWWindow::warp_cursor(const double x, const double y) {
182 glfwSetCursorPos(window_, x, y);
183 }
184
185 void GLFWWindow::set_cursor_mode(const CursorMode mode) {
186 switch (mode) {
187 case HIDDEN:
188 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
189 break;
190 case DISABLED:
191 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
192 break;
193 case SHOWN:
194 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
195 break;
196 }
197 }
198
199 void GLFWWindow::enable_vsync(const bool vsync) {
200 glfwSwapInterval(vsync);
201 }
202
203 void GLFWWindow::maximize(bool should_maximize) {
204 if (window_ && should_maximize)
205 glfwMaximizeWindow(window_);
206 }
207}
void maximize(bool should_maximize) override
IWindowEventHandler * event_handler_
Definition GLFWWindow.h:42
glm::vec2 get_mouse_position() const override
void wait_for_events() override
glm::ivec2 get_size() const override
void destroy() override
void set_event_handler(IWindowEventHandler *handler) override
bool should_close() const override
void poll_events() override
void * get_native_handle() override
void set_title(const std::string &title) override
glm::ivec2 get_framebuffer_size() const override
float get_elapsed_time() override
void make_current() override
void swap_buffers() override
WindowInfo window_info_
Definition IWindow.h:103
std::string title
Definition IWindow.h:20