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);
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);
41 glfwDestroyWindow(window_);
53 glfwSwapBuffers(window_);
67 void GLFWWindow::
set_title(
const std::string &title) {
70 glfwSetWindowTitle(window_, title.c_str());
74 void GLFWWindow::set_size(
const int width,
const int height) {
78 glfwSetWindowSize(window_, width, height);
87 return window_ && glfwWindowShouldClose(window_);
97 glfwGetFramebufferSize(window_, &width, &height);
98 return {width, height};
103 bool GLFWWindow::is_key_pressed(
const int keycode)
const {
105 return glfwGetKey(window_, keycode) == GLFW_PRESS;
113 glfwGetCursorPos(window_, &x, &y);
114 return {
static_cast<
float>(x),
static_cast<
float>(y)};
121 glfwMakeContextCurrent(window_);
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);
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);
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));
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;
159 if (instance->event_handler_) {
160 instance->event_handler_->on_window_resize(width, height);
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);
179 return static_cast<
float>(glfwGetTime());
182 void GLFWWindow::warp_cursor(
const double x,
const double y) {
183 glfwSetCursorPos(window_, x, y);
189 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
192 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
195 glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
200 void GLFWWindow::enable_vsync(
const bool vsync) {
201 glfwSwapInterval(vsync);
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