33
44namespace reactor {
55 // The constructor initializes GLFW and creates the window.
6- Window::Window (int width, int height, std::string title) : m_title(std::move(title)) {
6+ Window::Window (int width, int height, std::string title, EventManager& eventManager) :
7+ m_width (width), m_height(height), m_title(title), m_eventManager(eventManager) {
78 // Initialize the GLFW library.
89 if (!glfwInit ()) {
910 throw std::runtime_error (" Failed to initialize GLFW!" );
@@ -19,12 +20,11 @@ namespace reactor {
1920 throw std::runtime_error (" Failed to create GLFW window!" );
2021 }
2122
22- // Store a pointer to this Window instance in the GLFW window's user data.
23- // This allows us to retrieve it in the static callback.
2423 glfwSetWindowUserPointer (m_window, this );
25-
26- // Set the static callback function for framebuffer resize events.
2724 glfwSetFramebufferSizeCallback (m_window, framebufferResizeCallback);
25+
26+ glfwSetKeyCallback (m_window, keyCallback);
27+ glfwSetCursorPosCallback (m_window, mouseButtonCallback);
2828 }
2929
3030 // The destructor cleans up GLFW resources.
@@ -59,12 +59,38 @@ namespace reactor {
5959
6060
6161 // This is the static callback that GLFW invokes when the window is resized.
62- void Window::framebufferResizeCallback (GLFWwindow* window, int width, int height) {
62+ void Window::framebufferResizeCallback (GLFWwindow * window, int width, int height) {
6363 // Retrieve the pointer to our Window class instance.
64- auto * windowInstance = static_cast <Window*>(glfwGetWindowUserPointer (window));
64+ auto * windowInstance = static_cast <Window *>(glfwGetWindowUserPointer (window));
6565 if (windowInstance) {
6666 // Set the flag indicating that a resize has occurred.
6767 windowInstance->m_framebufferResized = true ;
6868 }
6969 }
70- }
70+ void Window::keyCallback (GLFWwindow *window, int key, int scancode, int action, int mods) {
71+ auto * windowInstance = static_cast <Window*>(glfwGetWindowUserPointer (window));
72+ if (!windowInstance) return ;
73+
74+ Event event{};
75+ if (action == GLFW_PRESS) {
76+ event.type = EventType::KeyPressed;
77+ event.keyboard .key = key;
78+ windowInstance->m_eventManager .post (event);
79+ } else if (action == GLFW_RELEASE) {
80+ event.type = EventType::KeyReleased;
81+ event.keyboard .key = key;
82+ windowInstance->m_eventManager .post (event);
83+ }
84+
85+ }
86+ void Window::mouseButtonCallback (GLFWwindow *window, double xpos, double ypos) {
87+ auto * windowInstance = static_cast <Window*>(glfwGetWindowUserPointer (window));
88+ if (!windowInstance) return ;
89+ Event event{};
90+ event.type = EventType::MouseMoved;
91+ event.mouseMove .x = xpos;
92+ event.mouseMove .y = ypos;
93+ windowInstance->m_eventManager .post (event);
94+
95+ }
96+ } // namespace reactor
0 commit comments