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