From 6864a907feae8b3ca70f3bb49dad46baf2017b78 Mon Sep 17 00:00:00 2001 From: Light3039 Date: Sun, 23 May 2021 21:25:31 +0430 Subject: [PATCH] Removed dispatcher, Added window events --- Engine/src/Engine/Base.h | 5 ++- Engine/src/Engine/Core/Application.cpp | 10 ++++- Engine/src/Engine/Core/Application.h | 2 - Engine/src/Engine/Core/Window.h | 5 +++ Engine/src/Engine/Events/Dispatcher.h | 42 ------------------- Engine/src/Engine/Events/Event.h | 12 ++++++ Engine/src/Engine/Events/Events.cpp | 3 -- Engine/src/Engine/Events/KeyboardEvents.h | 4 +- Engine/src/Engine/Events/MouseEvents.h | 5 ++- Engine/src/Engine/Events/WindowEvents.h | 22 ++++++++++ Engine/src/Engine/Layer/Layer.h | 18 +++++--- Engine/src/Engine/Layer/LayerStack.cpp | 40 ++++++++++++++++++ Engine/src/Engine/Layer/LayerStack.h | 6 +++ .../Engine/Platforms/OS/Windows/wWindow.cpp | 16 +++++++ .../src/Engine/Platforms/OS/Windows/wWindow.h | 1 + 15 files changed, 134 insertions(+), 57 deletions(-) delete mode 100644 Engine/src/Engine/Events/Dispatcher.h delete mode 100644 Engine/src/Engine/Events/Events.cpp create mode 100644 Engine/src/Engine/Events/WindowEvents.h diff --git a/Engine/src/Engine/Base.h b/Engine/src/Engine/Base.h index b74be19..51b8005 100644 --- a/Engine/src/Engine/Base.h +++ b/Engine/src/Engine/Base.h @@ -1,5 +1,7 @@ #pragma once +#include "Core/Logger.h" + #if defined(LT_PLATFORM_WINDOWS) #define LT_BUILD_PLATFORM "Windows" #elif defined(LT_PLATFORM_LINUX) @@ -8,4 +10,5 @@ #error "Unsupported platform: Unknown" #endif -#include "Core/Logger.h" \ No newline at end of file + +#define BIT(x) 1 << x \ No newline at end of file diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index 3bb3ce8..c655537 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -28,12 +28,18 @@ namespace Light { void Application::GameLoop() { - while (true) { m_Window->OnUpdate(); } + while (m_Window->IsOpen()) + { + m_Window->OnUpdate(); + } } void Application::OnEvent(Event& event) { - m_Dispatcher.Dispatch(event, m_LayerStack.begin(), m_LayerStack.end()); + if (event.IsInCategory(WindowEventCategory)) + m_Window->OnEvent(event); + + m_LayerStack.OnEvent(event); } } \ No newline at end of file diff --git a/Engine/src/Engine/Core/Application.h b/Engine/src/Engine/Core/Application.h index 42495d0..7ccd047 100644 --- a/Engine/src/Engine/Core/Application.h +++ b/Engine/src/Engine/Core/Application.h @@ -3,7 +3,6 @@ #include "Base.h" #include "Events/Event.h" -#include "Events/Dispatcher.h" #include "Layer/LayerStack.h" @@ -18,7 +17,6 @@ namespace Light { private: std::unique_ptr m_Window = nullptr; LayerStack m_LayerStack; - Dispatcher m_Dispatcher; public: virtual ~Application(); diff --git a/Engine/src/Engine/Core/Window.h b/Engine/src/Engine/Core/Window.h index 9112b8b..1102c03 100644 --- a/Engine/src/Engine/Core/Window.h +++ b/Engine/src/Engine/Core/Window.h @@ -17,10 +17,15 @@ namespace Light { class Window { + protected: + bool b_Open; public: virtual ~Window() = default; + inline bool IsOpen() const { return b_Open; } + virtual void OnUpdate() = 0; + virtual void OnEvent(Event& event) = 0; virtual unsigned int GetHeight() = 0; virtual unsigned int GetWidth() = 0; diff --git a/Engine/src/Engine/Events/Dispatcher.h b/Engine/src/Engine/Events/Dispatcher.h deleted file mode 100644 index b8062e5..0000000 --- a/Engine/src/Engine/Events/Dispatcher.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "Base.h" - -#include "Event.h" - -#include "Layer/Layer.h" - -namespace Light { - - class Dispatcher - { - public: - void Dispatch(Event& event, std::vector::iterator begin, std::vector::iterator end) - { - switch (event.GetEventType()) - { - case EventType::MouseMoved: - for (auto it = begin; it != end; it++) - if ((*it)->OnMouseMoved((MouseMovedEvent&)event)) return; - return; - case EventType::ButtonPressed: - for (auto it = begin; it != end; it++) - if ((*it)->OnButtonPressed((ButtonPressedEvent&)event)) return; - return; - case EventType::ButtonReleased: - for (auto it = begin; it != end; it++) - if ((*it)->OnButtonReleased((ButtonReleasedEvent&)event)) return; - return; - case EventType::KeyPressed: - for (auto it = begin; it != end; it++) - if ((*it)->OnKeyPressed((KeyPressedEvent&)event)) return; - return; - case EventType::KeyReleased: - for (auto it = begin; it != end; it++) - if ((*it)->OnKeyReleased((KeyReleasedEvent&)event)) return; - return; - } - } - }; - -} \ No newline at end of file diff --git a/Engine/src/Engine/Events/Event.h b/Engine/src/Engine/Events/Event.h index 7f4ae5f..34b99e7 100644 --- a/Engine/src/Engine/Events/Event.h +++ b/Engine/src/Engine/Events/Event.h @@ -20,13 +20,25 @@ namespace Light { WindowMoved, WindowResized, WindowClosed, }; + enum EventCategory + { + None = 0, + + WindowEventCategory = BIT(0), + InputEventCategory = BIT(1), + KeyboardEventCategory = BIT(2), + MouseEventCategory = BIT(3), + }; + #define EVENT_TYPE(type) EventType GetEventType() const override { return EventType::##type; } +#define EVENT_CATEGORY(eCategory) inline bool IsInCategory(EventCategory category) const override { return (eCategory) & category; } class Event { public: virtual EventType GetEventType() const = 0; virtual std::string GetInfoLog() const = 0; + virtual bool IsInCategory(EventCategory category) const = 0; friend std::ostream & operator<<(std::ostream & os, const Event& e) { diff --git a/Engine/src/Engine/Events/Events.cpp b/Engine/src/Engine/Events/Events.cpp deleted file mode 100644 index 2689c97..0000000 --- a/Engine/src/Engine/Events/Events.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "Event.h" -#include "KeyboardEvents.h" -#include "MouseEvents.h" \ No newline at end of file diff --git a/Engine/src/Engine/Events/KeyboardEvents.h b/Engine/src/Engine/Events/KeyboardEvents.h index a556efd..b1537da 100644 --- a/Engine/src/Engine/Events/KeyboardEvents.h +++ b/Engine/src/Engine/Events/KeyboardEvents.h @@ -2,7 +2,7 @@ #include "Base.h" -#include "Events/Event.h" +#include "Event.h" #include @@ -25,6 +25,7 @@ namespace Light { return ss.str(); } EVENT_TYPE(KeyPressed) + EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory) }; class KeyReleasedEvent : public Event @@ -44,6 +45,7 @@ namespace Light { return ss.str(); } EVENT_TYPE(KeyReleased) + EVENT_CATEGORY(InputEventCategory | KeyboardEventCategory) }; diff --git a/Engine/src/Engine/Events/MouseEvents.h b/Engine/src/Engine/Events/MouseEvents.h index 42c8b21..3d918d0 100644 --- a/Engine/src/Engine/Events/MouseEvents.h +++ b/Engine/src/Engine/Events/MouseEvents.h @@ -26,6 +26,7 @@ namespace Light { return ss.str(); } EVENT_TYPE(MouseMoved) + EVENT_CATEGORY(InputEventCategory | MouseEventCategory) }; class ButtonPressedEvent : public Event @@ -45,6 +46,7 @@ namespace Light { return ss.str(); } EVENT_TYPE(ButtonPressed) + EVENT_CATEGORY(InputEventCategory | MouseEventCategory) }; class ButtonReleasedEvent : public Event @@ -64,7 +66,8 @@ namespace Light { return ss.str(); } - EVENT_TYPE(ButtonReleased); + EVENT_TYPE(ButtonReleased) + EVENT_CATEGORY(InputEventCategory | MouseEventCategory) }; } \ No newline at end of file diff --git a/Engine/src/Engine/Events/WindowEvents.h b/Engine/src/Engine/Events/WindowEvents.h new file mode 100644 index 0000000..2618d54 --- /dev/null +++ b/Engine/src/Engine/Events/WindowEvents.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Base.h" + +#include "Event.h" + +#include + +namespace Light { + + class WindowClosedEvent : public Event + { + public: + virtual std::string GetInfoLog() const override + { + return "WindowClosedEvent"; + } + EVENT_TYPE(WindowClosed) + EVENT_CATEGORY(WindowEventCategory) + }; + +} \ No newline at end of file diff --git a/Engine/src/Engine/Layer/Layer.h b/Engine/src/Engine/Layer/Layer.h index c406683..a1d7641 100644 --- a/Engine/src/Engine/Layer/Layer.h +++ b/Engine/src/Engine/Layer/Layer.h @@ -4,6 +4,7 @@ #include "Events/MouseEvents.h" #include "Events/KeyboardEvents.h" +#include "Events/WindowEvents.h" #include @@ -27,6 +28,10 @@ namespace Light { // Keyboard events virtual bool OnKeyPressed(const KeyPressedEvent& event) { return false; } virtual bool OnKeyReleased(const KeyReleasedEvent& event) { return false; } + + // Window Events + virtual bool OnWindowClosed(const WindowClosedEvent& event) { return false; } + }; class TestLayer : public Layer @@ -35,13 +40,16 @@ namespace Light { TestLayer(const std::string& name): Layer(name) {} // Mouse events - virtual bool OnMouseMoved(const MouseMovedEvent& event) { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; } - virtual bool OnButtonPressed(const ButtonPressedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } - virtual bool OnButtonReleased(const ButtonReleasedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnMouseMoved(const MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; } + virtual bool OnButtonPressed(const ButtonPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnButtonReleased(const ButtonReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } // Keyboard events - virtual bool OnKeyPressed(const KeyPressedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } - virtual bool OnKeyReleased(const KeyReleasedEvent& event) { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnKeyPressed(const KeyPressedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + virtual bool OnKeyReleased(const KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } + + // Window events + virtual bool OnWindowClosed(const WindowClosedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } }; } \ No newline at end of file diff --git a/Engine/src/Engine/Layer/LayerStack.cpp b/Engine/src/Engine/Layer/LayerStack.cpp index 17010a9..a61327c 100644 --- a/Engine/src/Engine/Layer/LayerStack.cpp +++ b/Engine/src/Engine/Layer/LayerStack.cpp @@ -11,13 +11,53 @@ namespace Light { void LayerStack::PushLayer(Layer* layer) { m_Layers.push_back(layer); + m_Begin = m_Layers.begin(); + m_End = m_Layers.end(); + LT_ENGINE_TRACE("LayerStack::PushLayer: Attached [{}]", layer->GetName()); } void LayerStack::PopLayer(Layer* layer) { m_Layers.erase(std::find(m_Layers.begin(), m_Layers.end(), layer)); + m_Begin = m_Layers.begin(); + m_End = m_Layers.end(); + LT_ENGINE_TRACE("LayerStack::PushLayer: Detatched[{}]", layer->GetName()); } + void LayerStack::OnEvent(Event& event) + { + switch (event.GetEventType()) + { + case EventType::MouseMoved: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnMouseMoved((MouseMovedEvent&)event)) return; + return; + + case EventType::ButtonPressed: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnButtonPressed((ButtonPressedEvent&)event)) return; + return; + case EventType::ButtonReleased: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnButtonReleased((ButtonReleasedEvent&)event)) return; + return; + + case EventType::KeyPressed: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnKeyPressed((KeyPressedEvent&)event)) return; + return; + case EventType::KeyReleased: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnKeyReleased((KeyReleasedEvent&)event)) return; + return; + + case EventType::WindowClosed: + for (auto it = m_Begin; it != m_End; it++) + if ((*it)->OnWindowClosed((WindowClosedEvent&)event)) return; + return; + } + } + } \ No newline at end of file diff --git a/Engine/src/Engine/Layer/LayerStack.h b/Engine/src/Engine/Layer/LayerStack.h index 2b13640..5478887 100644 --- a/Engine/src/Engine/Layer/LayerStack.h +++ b/Engine/src/Engine/Layer/LayerStack.h @@ -4,6 +4,8 @@ #include "Layer.h" +#include "Events/Event.h" + #include namespace Light { @@ -13,12 +15,16 @@ namespace Light { private: std::vector m_Layers; + std::vector::iterator m_Begin; + std::vector::iterator m_End; public: ~LayerStack(); void PushLayer(Layer* layer); void PopLayer(Layer* layer); + void OnEvent(Event& event); + std::vector::iterator begin() { return m_Layers.begin(); } std::vector::iterator end() { return m_Layers.end(); } }; diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp index cc485ff..b965561 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.cpp @@ -2,6 +2,7 @@ #include "Events/KeyboardEvents.h" #include "Events/MouseEvents.h" +#include "Events/WindowEvents.h" namespace Light { @@ -34,6 +35,15 @@ namespace Light { glfwSwapBuffers(m_Handle); } + void wWindow::OnEvent(Event& event) + { + switch (event.GetEventType()) + { + case EventType::WindowClosed: + b_Open = false; + } + } + unsigned int wWindow::GetWidth() { return m_Properties.width; @@ -71,6 +81,12 @@ namespace Light { else callback(KeyReleasedEvent(key)); }); + + glfwSetWindowCloseCallback(m_Handle, [](GLFWwindow* window) + { + std::function callback = *(std::function*)glfwGetWindowUserPointer(window); + callback(WindowClosedEvent()); + }); } } \ No newline at end of file diff --git a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h index 7a11a2d..f28a349 100644 --- a/Engine/src/Engine/Platforms/OS/Windows/wWindow.h +++ b/Engine/src/Engine/Platforms/OS/Windows/wWindow.h @@ -25,6 +25,7 @@ namespace Light { ~wWindow(); virtual void OnUpdate() override; + virtual void OnEvent(Event& event) override; virtual unsigned int GetWidth() override; virtual unsigned int GetHeight() override;