diff --git a/include/vkcv/Window.hpp b/include/vkcv/Window.hpp index 87c98a9281e6081c98487c7fd314d960b818190f..bb8081c166360e97595ff6994971390e53d6aa32 100644 --- a/include/vkcv/Window.hpp +++ b/include/vkcv/Window.hpp @@ -7,6 +7,7 @@ #define NOMINMAX #include <algorithm> +#include "Event.hpp" struct GLFWwindow; @@ -23,6 +24,32 @@ namespace vkcv { */ explicit Window(GLFWwindow *window); + /** + * mouse callback for moving the mouse on the screen + * @param[in] window The window that received the event. + * @param[in] xpos The new cursor x-coordinate, relative to the left edge of the content area. + * @param[in] ypos The new cursor y-coordinate, relative to the top edge of the content area. + */ + static void onMouseMoveEvent(GLFWwindow *window, double x, double y); + + /** + * resize callback for the resize option of the window + * @param[in] window The window that was resized. + * @param[in] width The new width, in screen coordinates, of the window. + * @param[in] height The new height, in screen coordinates, of the window. + */ + static void onResize(GLFWwindow *callbackWindow, int width, int height); + + /** + * key callback for the pressed key + * @param[in] window The window that received the event. + * @param[in] key The [keyboard key](@ref keys) that was pressed or released. + * @param[in] scancode The system-specific scancode of the key. + * @param[in] action `GLFW_PRESS`, `GLFW_RELEASE` or `GLFW_REPEAT`. + * @param[in] mods Bit field describing which [modifier keys](@ref mods) were held down. + */ + static void onKeyEvent(GLFWwindow *callbackWindow, int key, int scancode, int action, int mods); + public: /** * creates a GLFWwindow with the parameters in the function @@ -41,11 +68,23 @@ namespace vkcv { [[nodiscard]] bool isWindowOpen() const; + /** + * binds windowEvents to lambda events + */ + void initEvents(); + /** * polls all events on the GLFWwindow */ static void pollEvents(); + /** + * basic events of the window + */ + event< double, double > e_mouseMove; + event< int, int > e_resize; + event< int, int, int, int > e_key; + /** * returns the current window * @return window handle diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp index 376f363a95c1e873c435825ba97e155205851347..87f302c146f79f82a9b5334fd6a651fa00a92616 100644 --- a/src/vkcv/Window.cpp +++ b/src/vkcv/Window.cpp @@ -42,14 +42,52 @@ namespace vkcv { return Window(window); } - bool Window::isWindowOpen() const { - return !glfwWindowShouldClose(m_window); + void Window::initEvents() { + glfwSetWindowUserPointer(m_window, this); + + // combine Callbacks with Events + glfwSetCursorPosCallback(m_window, Window::onMouseMoveEvent); + + glfwSetWindowSizeCallback(m_window, Window::onResize); + + glfwSetKeyCallback(m_window, Window::onKeyEvent); } void Window::pollEvents() { glfwPollEvents(); } + void Window::onMouseMoveEvent(GLFWwindow *callbackWindow, double x, double y) { + + auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); + + if (window != nullptr) { + window->e_mouseMove(x, y); + } + } + + void Window::onResize(GLFWwindow *callbackWindow, int width, int height) { + + auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); + + if (window != nullptr) { + window->e_resize(width, height); + } + } + + void Window::onKeyEvent(GLFWwindow *callbackWindow, int key, int scancode, int action, int mods) { + + auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow)); + + if (window != nullptr) { + window->e_key(key, scancode, action, mods); + } + } + + bool Window::isWindowOpen() const { + return !glfwWindowShouldClose(m_window); + } + int Window::getWidth() const { int width; glfwGetWindowSize(m_window, &width, nullptr);