Skip to content
Snippets Groups Projects
Verified Commit a68ddf84 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

[#43] Synchronized window events with mutexes

parent c5ea5767
No related branches found
No related tags found
1 merge request!65Resolve "Kamera - Steuerung mittels Controller"
Pipeline #25435 passed
#pragma once #pragma once
#include <functional> #include <functional>
#include <mutex>
namespace vkcv { namespace vkcv {
...@@ -17,6 +18,7 @@ namespace vkcv { ...@@ -17,6 +18,7 @@ namespace vkcv {
struct event { struct event {
private: private:
std::vector<typename event_function<T...>::type> m_handles; std::vector<typename event_function<T...>::type> m_handles;
std::mutex m_mutex;
public: public:
...@@ -25,9 +27,13 @@ namespace vkcv { ...@@ -25,9 +27,13 @@ namespace vkcv {
* @param arguments of the given function * @param arguments of the given function
*/ */
void operator()(T... arguments) { void operator()(T... arguments) {
lock();
for (auto &handle : this->m_handles) { for (auto &handle : this->m_handles) {
handle(arguments...); handle(arguments...);
} }
unlock();
} }
/** /**
...@@ -49,6 +55,20 @@ namespace vkcv { ...@@ -49,6 +55,20 @@ namespace vkcv {
this->m_handles.end() this->m_handles.end()
); );
} }
/**
* locks the event so its function handles won't be called
*/
void lock() {
m_mutex.lock();
}
/**
* unlocks the event so its function handles can be called after locking
*/
void unlock() {
m_mutex.unlock();
}
event() = default; event() = default;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#define NOMINMAX #define NOMINMAX
#include <algorithm> #include <algorithm>
#include "Event.hpp" #include "Event.hpp"
struct GLFWwindow; struct GLFWwindow;
......
...@@ -17,8 +17,6 @@ int main(int argc, const char** argv) { ...@@ -17,8 +17,6 @@ int main(int argc, const char** argv) {
vkcv::CameraManager cameraManager(window, static_cast<float>(window.getWidth()), static_cast<float>(window.getHeight())); vkcv::CameraManager cameraManager(window, static_cast<float>(window.getWidth()), static_cast<float>(window.getHeight()));
window.initEvents();
vkcv::Core core = vkcv::Core::create( vkcv::Core core = vkcv::Core::create(
window, window,
applicationName, applicationName,
......
...@@ -18,8 +18,6 @@ int main(int argc, const char** argv) { ...@@ -18,8 +18,6 @@ int main(int argc, const char** argv) {
vkcv::CameraManager cameraManager(window, windowWidth, windowHeight); vkcv::CameraManager cameraManager(window, windowWidth, windowHeight);
window.initEvents();
vkcv::Core core = vkcv::Core::create( vkcv::Core core = vkcv::Core::create(
window, window,
applicationName, applicationName,
......
...@@ -10,55 +10,68 @@ ...@@ -10,55 +10,68 @@
namespace vkcv { namespace vkcv {
static uint32_t s_WindowCount = 0; static std::vector<GLFWwindow*> s_Windows;
Window::Window(GLFWwindow *window) Window::Window(GLFWwindow *window)
: m_window(window) { : m_window(window) {
glfwSetWindowUserPointer(m_window, this);
// combine Callbacks with Events
glfwSetMouseButtonCallback(m_window, Window::onMouseButtonEvent);
glfwSetCursorPosCallback(m_window, Window::onMouseMoveEvent);
glfwSetWindowSizeCallback(m_window, Window::onResize);
glfwSetKeyCallback(m_window, Window::onKeyEvent);
glfwSetScrollCallback(m_window, Window::onMouseScrollEvent);
} }
Window::~Window() { Window::~Window() {
s_Windows.erase(std::find(s_Windows.begin(), s_Windows.end(), m_window));
glfwDestroyWindow(m_window); glfwDestroyWindow(m_window);
s_WindowCount--;
if(s_WindowCount == 0) { if(s_Windows.empty()) {
glfwTerminate(); glfwTerminate();
} }
} }
Window Window::create( const char *windowTitle, int width, int height, bool resizable) { Window Window::create( const char *windowTitle, int width, int height, bool resizable) {
if(s_WindowCount == 0) { if(s_Windows.empty()) {
glfwInit(); glfwInit();
} }
s_WindowCount++;
width = std::max(width, 1); width = std::max(width, 1);
height = std::max(height, 1); height = std::max(height, 1);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, resizable ? GLFW_TRUE : GLFW_FALSE); glfwWindowHint(GLFW_RESIZABLE, resizable ? GLFW_TRUE : GLFW_FALSE);
GLFWwindow *window; GLFWwindow *window = glfwCreateWindow(width, height, windowTitle, nullptr, nullptr);
window = glfwCreateWindow(width, height, windowTitle, nullptr, nullptr);
s_Windows.push_back(window);
return Window(window); return Window(window);
} }
void Window::initEvents() {
glfwSetWindowUserPointer(m_window, this);
// combine Callbacks with Events
glfwSetMouseButtonCallback(m_window, Window::onMouseButtonEvent);
glfwSetCursorPosCallback(m_window, Window::onMouseMoveEvent);
glfwSetWindowSizeCallback(m_window, Window::onResize);
glfwSetKeyCallback(m_window, Window::onKeyEvent);
glfwSetScrollCallback(m_window, Window::onMouseScrollEvent);
}
void Window::pollEvents() { void Window::pollEvents() {
for (auto glfwWindow : s_Windows) {
auto window = static_cast<Window *>(glfwGetWindowUserPointer(glfwWindow));
window->e_mouseButton.unlock();
window->e_mouseMove.unlock();
window->e_resize.unlock();
window->e_key.unlock();
window->e_mouseScroll.unlock();
}
glfwPollEvents(); glfwPollEvents();
for (auto glfwWindow : s_Windows) {
auto window = static_cast<Window *>(glfwGetWindowUserPointer(glfwWindow));
window->e_mouseButton.lock();
window->e_mouseMove.lock();
window->e_resize.lock();
window->e_key.lock();
window->e_mouseScroll.lock();
}
} }
void Window::onMouseButtonEvent(GLFWwindow *callbackWindow, int button, int action, int mods) { void Window::onMouseButtonEvent(GLFWwindow *callbackWindow, int button, int action, int mods) {
......
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