Skip to content
Snippets Groups Projects
Commit 05cb8f79 authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#14] add basic events to window

adds basic events like mousemovement, resize or keyEvents
This is only the basic code to fetch these events not to handle the actual event
parent a26f9a0a
No related branches found
No related tags found
1 merge request!25Resolve "EventPoll mit Lambdas"
......@@ -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
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment