From 85f9c78adfbb93cdebf234cb278f2ab5e2c27891 Mon Sep 17 00:00:00 2001 From: nigel Date: Fri, 10 Jun 2022 22:44:40 +0200 Subject: [PATCH] Started implementation of first event/message passing system --- BarinkEngine/BarinkEngine.cpp | 3 +- BarinkEngine/EventSystem/EventEmitter.cpp | 25 ++++++++ BarinkEngine/Include/BarinkEngine.h | 2 + BarinkEngine/Include/EventSystem/Event.h | 11 ++++ .../Include/EventSystem/EventEmitter.h | 16 +++++ .../Include/EventSystem/EventListener.cpp | 5 ++ .../Include/EventSystem/EventListener.h | 8 +++ BarinkEngine/Include/Graphics/Window.h | 6 +- BarinkEngine/Include/Input/InputManager.h | 6 +- BarinkEngine/Input/InputManager.cpp | 60 ++++++++++++++++--- BarinkEngine/graphics/window.cpp | 10 +++- 11 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 BarinkEngine/EventSystem/EventEmitter.cpp create mode 100644 BarinkEngine/Include/EventSystem/Event.h create mode 100644 BarinkEngine/Include/EventSystem/EventEmitter.h create mode 100644 BarinkEngine/Include/EventSystem/EventListener.cpp create mode 100644 BarinkEngine/Include/EventSystem/EventListener.h diff --git a/BarinkEngine/BarinkEngine.cpp b/BarinkEngine/BarinkEngine.cpp index 6fc3be5..914eebf 100644 --- a/BarinkEngine/BarinkEngine.cpp +++ b/BarinkEngine/BarinkEngine.cpp @@ -2,6 +2,7 @@ #include EngineStatistics* ES; +BarinkEngine::InputManager InputSystem; int main(int argc, char* argv[]) { // Setup performance sampler @@ -12,7 +13,7 @@ int main(int argc, char* argv[]) { BarinkWindow MainWindow = BarinkWindow(800, 600); BarinkEngine::Renderer renderer = BarinkEngine::Renderer(); - BarinkEngine::InputManager InputSystem = BarinkEngine::InputManager(); + InputSystem = BarinkEngine::InputManager(); InputSystem.attach(&MainWindow); diff --git a/BarinkEngine/EventSystem/EventEmitter.cpp b/BarinkEngine/EventSystem/EventEmitter.cpp new file mode 100644 index 0000000..f256cfa --- /dev/null +++ b/BarinkEngine/EventSystem/EventEmitter.cpp @@ -0,0 +1,25 @@ +#include "../Include/EventSystem/EventEmitter.h" + + +void EventEmitter::Subscribe(EventListener& subscriber) +{ + subscribers.push_back(&subscriber); +} + +void EventEmitter::Unsubscribe(EventListener& subscriber) +{ + subscribers.remove(&subscriber); +} + +void EventEmitter::EmitEvent(Event& incident) +{ + // Notify all subscribers an event has taken place + for (auto it = subscribers.begin(); it != subscribers.end(); ++it) + { + (*it)->ReceiveEvent(incident); + } +} + +EventEmitter::EventEmitter() { + subscribers = std::list{}; +} \ No newline at end of file diff --git a/BarinkEngine/Include/BarinkEngine.h b/BarinkEngine/Include/BarinkEngine.h index 7495b31..6471ec1 100644 --- a/BarinkEngine/Include/BarinkEngine.h +++ b/BarinkEngine/Include/BarinkEngine.h @@ -20,3 +20,5 @@ extern void Start(); extern void Update(); extern void ImmediateGraphicsDraw(); extern void Stop(); + +extern BarinkEngine::InputManager InputSystem; \ No newline at end of file diff --git a/BarinkEngine/Include/EventSystem/Event.h b/BarinkEngine/Include/EventSystem/Event.h new file mode 100644 index 0000000..9489364 --- /dev/null +++ b/BarinkEngine/Include/EventSystem/Event.h @@ -0,0 +1,11 @@ +#pragma once +#include + +struct Event +{ + public: + std::string name; + int argc; + void** argv; + +}; \ No newline at end of file diff --git a/BarinkEngine/Include/EventSystem/EventEmitter.h b/BarinkEngine/Include/EventSystem/EventEmitter.h new file mode 100644 index 0000000..aa3144c --- /dev/null +++ b/BarinkEngine/Include/EventSystem/EventEmitter.h @@ -0,0 +1,16 @@ +#pragma once +#include "Event.h" +#include "EventListener.h" + +class EventEmitter { +public: + void Subscribe (EventListener& subscriber); + void Unsubscribe(EventListener& subscriber); + +protected: + std::list subscribers; + void EmitEvent(Event& incident); + + EventEmitter(); + +}; \ No newline at end of file diff --git a/BarinkEngine/Include/EventSystem/EventListener.cpp b/BarinkEngine/Include/EventSystem/EventListener.cpp new file mode 100644 index 0000000..8e3a1ad --- /dev/null +++ b/BarinkEngine/Include/EventSystem/EventListener.cpp @@ -0,0 +1,5 @@ +#include "EventListener.h" + +void EventListener::ReceiveEvent(Event& incident) +{ +} diff --git a/BarinkEngine/Include/EventSystem/EventListener.h b/BarinkEngine/Include/EventSystem/EventListener.h new file mode 100644 index 0000000..520a564 --- /dev/null +++ b/BarinkEngine/Include/EventSystem/EventListener.h @@ -0,0 +1,8 @@ +#pragma once +#include "Event.h" +#include +class EventListener{ + public: + virtual void ReceiveEvent(Event& incident); + +}; \ No newline at end of file diff --git a/BarinkEngine/Include/Graphics/Window.h b/BarinkEngine/Include/Graphics/Window.h index 5229893..9951952 100644 --- a/BarinkEngine/Include/Graphics/Window.h +++ b/BarinkEngine/Include/Graphics/Window.h @@ -3,12 +3,13 @@ #include #include +#include "../Include/EventSystem/EventListener.h" -class BarinkWindow{ +class BarinkWindow : EventListener { private: GLFWwindow* window; bool FullScreen; @@ -18,12 +19,15 @@ class BarinkWindow{ static bool InitGLFW(); + + public: BarinkWindow(const int width, const int height); ~BarinkWindow(); GLFWwindow* windowptr(); + void ReceiveEvent(Event& incident) override; bool WindowShouldClose(); void Poll(); diff --git a/BarinkEngine/Include/Input/InputManager.h b/BarinkEngine/Include/Input/InputManager.h index 2317895..5a9e2c0 100644 --- a/BarinkEngine/Include/Input/InputManager.h +++ b/BarinkEngine/Include/Input/InputManager.h @@ -1,11 +1,11 @@ #pragma once #include - #include "Graphics/Window.h" +#include "EventSystem/EventEmitter.h" namespace BarinkEngine { - class InputManager { + class InputManager : EventEmitter { public: InputManager(); @@ -13,6 +13,7 @@ namespace BarinkEngine { void attach(BarinkWindow* window); + // GLFW Handlers static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void CursorPositionCallback(GLFWwindow* window, double x, double y); @@ -21,5 +22,6 @@ namespace BarinkEngine { static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); private: std::vector windows; + }; } diff --git a/BarinkEngine/Input/InputManager.cpp b/BarinkEngine/Input/InputManager.cpp index 68716aa..9922080 100644 --- a/BarinkEngine/Input/InputManager.cpp +++ b/BarinkEngine/Input/InputManager.cpp @@ -1,16 +1,24 @@ +#include "BarinkEngine.h" #include "Input/InputManager.h" #include "GLFW/glfw3.h" #include "spdlog/spdlog.h" #include void BarinkEngine::InputManager::PollEvents() { - for (std::vector::iterator it = windows.begin(); it != windows.end(); ++it) { + for (auto it = windows.begin(); it != windows.end(); ++it) { (*it)->Poll(); } } void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { + + Event KeyEvent{}; + KeyEvent.name = "KEY"; + + InputSystem.EmitEvent(KeyEvent); + + if (key == GLFW_KEY_A && action == GLFW_PRESS) { @@ -21,23 +29,49 @@ void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int sc void BarinkEngine::InputManager::CursorPositionCallback(GLFWwindow* window, double x, double y) { - std::cout << "Cursor Position x: " << x << ", y: " << y << std::endl; + //std::cout << "Cursor Position x: " << x << ", y: " << y << std::endl; + Event CursorPosUpdate{}; + CursorPosUpdate.name = "UPDATE::CURSOR:POSITION"; + + InputSystem.EmitEvent(CursorPosUpdate); + + + } void BarinkEngine::InputManager::CursorEnterCallback(GLFWwindow* window, int entered) { if (entered) { - // Cursor entered the window's screen space - std::cout << "Cursor entered!" << std::endl; + Event mouseEntered {}; + mouseEntered.name = "Mouse Entered Window's confines!"; + mouseEntered.argc = 0; + + InputSystem.EmitEvent(mouseEntered); + + + } else { - std::cout << "Cursor left!" << std::endl; + Event mouseLeft{}; + mouseLeft.name = "Mouse Left Window's confines!"; + mouseLeft.argc = 0; + + InputSystem.EmitEvent(mouseLeft); + + } } void BarinkEngine::InputManager::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { + + Event MouseButtonEvent{}; + MouseButtonEvent.name = "MOUSEBUTTON"; + + InputSystem.EmitEvent(MouseButtonEvent); + + if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { std::cout << "Right mouse button was pressed!" << std::endl; } @@ -47,7 +81,12 @@ void BarinkEngine::InputManager::MouseButtonCallback(GLFWwindow* window, int but void BarinkEngine::InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) { std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl; - + + Event ScrollEvent{}; + ScrollEvent.name = "SCROLL"; + + InputSystem.EmitEvent(ScrollEvent); + } @@ -64,9 +103,16 @@ void BarinkEngine::InputManager::attach(BarinkWindow* window) glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback); glfwSetScrollCallback(window->windowptr(), ScrollCallback); + this->Subscribe( (EventListener&)(*window)); + + + } -BarinkEngine::InputManager::InputManager() +BarinkEngine::InputManager::InputManager() : EventEmitter () { windows = std::vector(); } + + + diff --git a/BarinkEngine/graphics/window.cpp b/BarinkEngine/graphics/window.cpp index 6306d85..3843a0c 100644 --- a/BarinkEngine/graphics/window.cpp +++ b/BarinkEngine/graphics/window.cpp @@ -1,9 +1,10 @@ #include "Graphics/Window.h" #include #include +#include #include #include - +#include "../Include/EventSystem/Event.h" bool BarinkWindow::InitGLFW(){ if(!glfwInit()) @@ -73,4 +74,11 @@ void BarinkWindow::Poll() void BarinkWindow::SwapBuffers() { glfwSwapBuffers(window); +} + + +void BarinkWindow::ReceiveEvent(Event& incident) +{ + std::cout << "EVENT RECEIVED: " << incident.name << std::endl; + } \ No newline at end of file