7 bool GLFWWindow::create(
const int width,
const int height,
const std::string &title) {
15 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
16 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
17 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
19 window_ = glfwCreateWindow(width, height, title.c_str(),
nullptr,
nullptr);
24 glfwMakeContextCurrent(window_);
25 glfwSetWindowUserPointer(window_,
this);
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);
40 glfwDestroyWindow(window_);
52 glfwSwapBuffers(window_);
66 void GLFWWindow::
set_title(
const std::string &title) {
69 glfwSetWindowTitle(window_, title.c_str());
73 void GLFWWindow::set_size(
const int width,
const int height) {
77 glfwSetWindowSize(window_, width, height);
86 return window_ && glfwWindowShouldClose(window_);
96 glfwGetFramebufferSize(window_, &width, &height);
97 return {width, height};
102 bool GLFWWindow::is_key_pressed(
const int keycode)
const {
104 return glfwGetKey(window_, keycode) == GLFW_PRESS;
112 glfwGetCursorPos(window_, &x, &y);
113 return {
static_cast<
float>(x),
static_cast<
float>(y)};
120 glfwMakeContextCurrent(window_);
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);
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);
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));
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;
158 if (instance->event_handler_) {
159 instance->event_handler_->on_window_resize(width, height);
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);
178 return static_cast<
float>(glfwGetTime());
181 void GLFWWindow::warp_cursor(
const double x,
const double y) {
182 glfwSetCursorPos(window_, x, y);
188 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
191 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
194 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
199 void GLFWWindow::enable_vsync(
const bool vsync) {
200 glfwSwapInterval(vsync);
204 if (window_ && should_maximize)
205 glfwMaximizeWindow(window_);
void maximize(bool should_maximize) override
IWindowEventHandler * event_handler_
glm::vec2 get_mouse_position() const override
void wait_for_events() override
glm::ivec2 get_size() const 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