hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/window.cpp
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-12 16:35:42 +0200
committerhachem <im@hachem.wtf>2025-08-12 16:35:42 +0200
commitdbe642e5e26cf2bd99218f727157b58c7fed286e (patch)
treeb964424747bdd0b143a4dfac894aee61fd752302 /src/core/window.cpp
parentd6b9f6c8fad3e46caca5dc167ce1cbf5ccfb489e (diff)
feat: application manager and EventDispatcher
Diffstat (limited to 'src/core/window.cpp')
-rw-r--r--src/core/window.cpp157
1 files changed, 152 insertions, 5 deletions
diff --git a/src/core/window.cpp b/src/core/window.cpp
index 3a6ba2c..ab5b977 100644
--- a/src/core/window.cpp
+++ b/src/core/window.cpp
@@ -1,5 +1,6 @@
#include "window.h"
#include <cstdint>
+#include <iostream>
namespace Donut
{
@@ -7,21 +8,57 @@ namespace Donut
static uint32_t s_glfw_window_count = 0;
Window::Window(const std::string& title, int width, int height)
- : m_title(title), m_width(width), m_height(height)
+ : m_title(title), m_width(width),
+ m_height(height), m_is_closed(false)
+ {
+ init();
+ }
+
+ Window::~Window()
+ {
+ shutdown();
+ }
+
+ void Window::init()
{
if (!s_glfw_initialized)
{
- glfwInit();
+ int success = glfwInit();
+ if (!success)
+ {
+ std::cout << "Could not initialize GLFW!" << std::endl;
+ return;
+ }
+
s_glfw_initialized = true;
}
- m_window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
+ m_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, nullptr);
+ if (!m_window)
+ {
+ std::cout << "Could not create GLFW window!" << std::endl;
+ glfwTerminate();
+ return;
+ }
+
glfwMakeContextCurrent(m_window);
+ glfwSetWindowUserPointer(m_window, this);
+
+ glfwSetErrorCallback(glfw_error_callback);
+ glfwSetWindowCloseCallback(m_window, glfw_window_close_callback);
+ glfwSetWindowSizeCallback(m_window, glfw_window_size_callback);
+ glfwSetWindowFocusCallback(m_window, glfw_window_focus_callback);
+ glfwSetWindowPosCallback(m_window, glfw_window_pos_callback);
+ glfwSetKeyCallback(m_window, glfw_key_callback);
+ glfwSetCharCallback(m_window, glfw_char_callback);
+ glfwSetMouseButtonCallback(m_window, glfw_mouse_button_callback);
+ glfwSetScrollCallback(m_window, glfw_mouse_scroll_callback);
+ glfwSetCursorPosCallback(m_window, glfw_cursor_pos_callback);
s_glfw_window_count++;
}
- Window::~Window()
+ void Window::shutdown()
{
glfwDestroyWindow(m_window);
s_glfw_window_count--;
@@ -41,6 +78,116 @@ namespace Donut
bool Window::should_close() const
{
- return glfwWindowShouldClose(m_window);
+ return glfwWindowShouldClose(m_window) || m_is_closed;
+ }
+
+ void Window::glfw_error_callback(int error, const char* description)
+ {
+ std::cout << "GLFW Error (" << error << "): " << description << std::endl;
+ }
+
+ void Window::glfw_window_close_callback(GLFWwindow* window)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ WindowCloseEvent event;
+ win->m_event_handler.on_event(event);
+ }
+
+ void Window::glfw_window_size_callback(GLFWwindow* window, int width, int height)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ win->m_width = width;
+ win->m_height = height;
+
+ WindowResizeEvent event(width, height);
+ win->m_event_handler.on_event(event);
+ }
+
+ void Window::glfw_window_focus_callback(GLFWwindow* window, int focused)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+
+ if (focused)
+ {
+ WindowFocusEvent event;
+ win->m_event_handler.on_event(event);
+ }
+ else
+ {
+ WindowLostFocusEvent event;
+ win->m_event_handler.on_event(event);
+ }
+ }
+
+ void Window::glfw_window_pos_callback(GLFWwindow* window, int xpos, int ypos)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ WindowMovedEvent event(xpos, ypos);
+ win->m_event_handler.on_event(event);
+ }
+
+ void Window::glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+
+ switch (action)
+ {
+ case GLFW_PRESS:
+ {
+ KeyPressedEvent event(key, false);
+ win->m_event_handler.on_event(event);
+ } break;
+ case GLFW_RELEASE:
+ {
+ KeyReleasedEvent event(key);
+ win->m_event_handler.on_event(event);
+ } break;
+ case GLFW_REPEAT:
+ {
+ KeyPressedEvent event(key, true);
+ win->m_event_handler.on_event(event);
+ } break;
+ }
+ }
+
+ void Window::glfw_char_callback(GLFWwindow* window, unsigned int keycode)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ KeyTypedEvent event(keycode);
+ win->m_event_handler.on_event(event);
+ }
+
+ void Window::glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+
+ switch (action)
+ {
+ case GLFW_PRESS:
+ {
+ MouseButtonPressedEvent event(button);
+ win->m_event_handler.on_event(event);
+ } break;
+ case GLFW_RELEASE:
+ {
+ MouseButtonReleasedEvent event(button);
+ win->m_event_handler.on_event(event);
+ break;
+ }
+ }
+ }
+
+ void Window::glfw_mouse_scroll_callback(GLFWwindow* window, double x_offset, double y_offset)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ MouseScrolledEvent event((float)x_offset, (float)y_offset);
+ win->m_event_handler.on_event(event);
+ }
+
+ void Window::glfw_cursor_pos_callback(GLFWwindow* window, double x_pos, double y_pos)
+ {
+ Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
+ MouseMovedEvent event((float)x_pos, (float)y_pos);
+ win->m_event_handler.on_event(event);
}
}