Started implementation of first event/message passing system

This commit is contained in:
Nigel Barink 2022-06-10 22:44:40 +02:00
parent 4df6cfba90
commit 85f9c78adf
11 changed files with 140 additions and 12 deletions

View File

@ -2,6 +2,7 @@
#include <phonon.h> #include <phonon.h>
EngineStatistics* ES; EngineStatistics* ES;
BarinkEngine::InputManager InputSystem;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Setup performance sampler // Setup performance sampler
@ -12,7 +13,7 @@ int main(int argc, char* argv[]) {
BarinkWindow MainWindow = BarinkWindow(800, 600); BarinkWindow MainWindow = BarinkWindow(800, 600);
BarinkEngine::Renderer renderer = BarinkEngine::Renderer(); BarinkEngine::Renderer renderer = BarinkEngine::Renderer();
BarinkEngine::InputManager InputSystem = BarinkEngine::InputManager(); InputSystem = BarinkEngine::InputManager();
InputSystem.attach(&MainWindow); InputSystem.attach(&MainWindow);

View File

@ -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<EventListener*>{};
}

View File

@ -20,3 +20,5 @@ extern void Start();
extern void Update(); extern void Update();
extern void ImmediateGraphicsDraw(); extern void ImmediateGraphicsDraw();
extern void Stop(); extern void Stop();
extern BarinkEngine::InputManager InputSystem;

View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
struct Event
{
public:
std::string name;
int argc;
void** argv;
};

View File

@ -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<EventListener*> subscribers;
void EmitEvent(Event& incident);
EventEmitter();
};

View File

@ -0,0 +1,5 @@
#include "EventListener.h"
void EventListener::ReceiveEvent(Event& incident)
{
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "Event.h"
#include <list>
class EventListener{
public:
virtual void ReceiveEvent(Event& incident);
};

View File

@ -3,12 +3,13 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../Include/EventSystem/EventListener.h"
class BarinkWindow{ class BarinkWindow : EventListener {
private: private:
GLFWwindow* window; GLFWwindow* window;
bool FullScreen; bool FullScreen;
@ -18,12 +19,15 @@ class BarinkWindow{
static bool InitGLFW(); static bool InitGLFW();
public: public:
BarinkWindow(const int width, const int height); BarinkWindow(const int width, const int height);
~BarinkWindow(); ~BarinkWindow();
GLFWwindow* windowptr(); GLFWwindow* windowptr();
void ReceiveEvent(Event& incident) override;
bool WindowShouldClose(); bool WindowShouldClose();
void Poll(); void Poll();

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include "Graphics/Window.h" #include "Graphics/Window.h"
#include "EventSystem/EventEmitter.h"
namespace BarinkEngine { namespace BarinkEngine {
class InputManager { class InputManager : EventEmitter {
public: public:
InputManager(); InputManager();
@ -13,6 +13,7 @@ namespace BarinkEngine {
void attach(BarinkWindow* window); void attach(BarinkWindow* window);
// GLFW Handlers // GLFW Handlers
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void CursorPositionCallback(GLFWwindow* window, double x, double y); static void CursorPositionCallback(GLFWwindow* window, double x, double y);
@ -21,5 +22,6 @@ namespace BarinkEngine {
static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
private: private:
std::vector<BarinkWindow*> windows; std::vector<BarinkWindow*> windows;
}; };
} }

View File

@ -1,16 +1,24 @@
#include "BarinkEngine.h"
#include "Input/InputManager.h" #include "Input/InputManager.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <iostream> #include <iostream>
void BarinkEngine::InputManager::PollEvents() void BarinkEngine::InputManager::PollEvents()
{ {
for (std::vector<BarinkWindow*>::iterator it = windows.begin(); it != windows.end(); ++it) { for (auto it = windows.begin(); it != windows.end(); ++it) {
(*it)->Poll(); (*it)->Poll();
} }
} }
void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) 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) 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) 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) void BarinkEngine::InputManager::CursorEnterCallback(GLFWwindow* window, int entered)
{ {
if (entered) { if (entered) {
// Cursor entered the window's screen space Event mouseEntered {};
std::cout << "Cursor entered!" << std::endl; mouseEntered.name = "Mouse Entered Window's confines!";
mouseEntered.argc = 0;
InputSystem.EmitEvent(mouseEntered);
} }
else { 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) 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) { if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
std::cout << "Right mouse button was pressed!" << std::endl; 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) void BarinkEngine::InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{ {
std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl; 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); glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback);
glfwSetScrollCallback(window->windowptr(), ScrollCallback); glfwSetScrollCallback(window->windowptr(), ScrollCallback);
this->Subscribe( (EventListener&)(*window));
} }
BarinkEngine::InputManager::InputManager() BarinkEngine::InputManager::InputManager() : EventEmitter ()
{ {
windows = std::vector<BarinkWindow*>(); windows = std::vector<BarinkWindow*>();
} }

View File

@ -1,9 +1,10 @@
#include "Graphics/Window.h" #include "Graphics/Window.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <iostream>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "../Include/EventSystem/Event.h"
bool BarinkWindow::InitGLFW(){ bool BarinkWindow::InitGLFW(){
if(!glfwInit()) if(!glfwInit())
@ -73,4 +74,11 @@ void BarinkWindow::Poll()
void BarinkWindow::SwapBuffers() void BarinkWindow::SwapBuffers()
{ {
glfwSwapBuffers(window); glfwSwapBuffers(window);
}
void BarinkWindow::ReceiveEvent(Event& incident)
{
std::cout << "EVENT RECEIVED: " << incident.name << std::endl;
} }