diff --git a/GAME/source/GAME/Layers/MainGameLayer.cpp b/GAME/source/GAME/Layers/MainGameLayer.cpp index 2b170f7..30b2005 100644 --- a/GAME/source/GAME/Layers/MainGameLayer.cpp +++ b/GAME/source/GAME/Layers/MainGameLayer.cpp @@ -22,10 +22,15 @@ namespace soge_game void MainGameLayer::OnUpdate() { - auto inputModule = soge::Engine::GetInstance()->GetModule(); - if (inputModule->IsKeyPressed(soge::Keys::W)) + const auto engine = soge::Engine::GetInstance(); + if (const auto inputModule = engine->GetModule(); inputModule->IsKeyPressed(soge::Keys::W)) { - SOGE_APP_INFO_LOG("Key W pressed"); + SOGE_APP_INFO_LOG("Key W pressed!"); + } + else if (inputModule->IsKeyPressed(soge::Keys::Escape)) + { + SOGE_APP_INFO_LOG("Key Escape pressed - shutting down!"); + engine->RequestShutdown(); } } diff --git a/SOGE/include/SOGE/Event/EventModule.hpp b/SOGE/include/SOGE/Event/EventModule.hpp index 410d051..87ec255 100644 --- a/SOGE/include/SOGE/Event/EventModule.hpp +++ b/SOGE/include/SOGE/Event/EventModule.hpp @@ -117,9 +117,10 @@ namespace soge void ClearQueue(); void Clear(); - template - requires std::is_constructible_v - void Dispatch(Args&&... args); + // TODO: return this method when weird library bug will be fixed + // template + // requires std::is_constructible_v + // void Dispatch(Args&&... args); template requires std::is_constructible_v @@ -241,13 +242,13 @@ namespace soge return IsEmpty(eventType); } - template - requires std::is_constructible_v - void EventModule::Dispatch(Args&&... args) - { - AnyEvent event{E{std::forward(args)...}}; - m_eventQueue.dispatch(event); - } + // template + // requires std::is_constructible_v + // void EventModule::Dispatch(Args&&... args) + // { + // AnyEvent event{E{std::forward(args)...}}; + // m_eventQueue.dispatch(event); + // } template requires std::is_constructible_v diff --git a/SOGE/include/SOGE/Input/Device/Keyboard.hpp b/SOGE/include/SOGE/Input/Device/Keyboard.hpp index f9cbe3d..4092b2a 100644 --- a/SOGE/include/SOGE/Input/Device/Keyboard.hpp +++ b/SOGE/include/SOGE/Input/Device/Keyboard.hpp @@ -2,7 +2,6 @@ #define SOGE_INPUT_DEVICE_KEYBOARD_HPP #include "SOGE/Input/Device/InputDevice.hpp" -#include "SOGE/Input/InputTypes.hpp" namespace soge @@ -13,7 +12,6 @@ namespace soge explicit Keyboard(eastl::string_view aKeyboardName); virtual void Update() = 0; - }; } diff --git a/SOGE/include/SOGE/Input/Device/Mouse.hpp b/SOGE/include/SOGE/Input/Device/Mouse.hpp index bd309fb..04a23f5 100644 --- a/SOGE/include/SOGE/Input/Device/Mouse.hpp +++ b/SOGE/include/SOGE/Input/Device/Mouse.hpp @@ -2,7 +2,6 @@ #define SOGE_INPUT_DEVICE_MOUSE_HPP #include "SOGE/Input/Device/InputDevice.hpp" -#include "SOGE/Input/InputTypes.hpp" namespace soge @@ -13,7 +12,6 @@ namespace soge explicit Mouse(eastl::string_view aMouseName); virtual void Update() = 0; - }; } diff --git a/SOGE/include/SOGE/Input/InputModule.hpp b/SOGE/include/SOGE/Input/InputModule.hpp index fad90e7..3deb1aa 100644 --- a/SOGE/include/SOGE/Input/InputModule.hpp +++ b/SOGE/include/SOGE/Input/InputModule.hpp @@ -14,7 +14,11 @@ namespace soge class InputModule : public Module { private: + void Update() const; + InputCore* m_inputCore; + EventModule* m_eventModule; + EventModule::FunctionHandle m_updateEventHandle; public: explicit InputModule(); @@ -33,8 +37,6 @@ namespace soge Gamepad* GetGamepad() const; [[nodiscard]] Mouse* GetMouse() const; - - void Update() const; }; } diff --git a/SOGE/source/SOGE/Core/Engine.cpp b/SOGE/source/SOGE/Core/Engine.cpp index 57dddab..88c21cc 100644 --- a/SOGE/source/SOGE/Core/Engine.cpp +++ b/SOGE/source/SOGE/Core/Engine.cpp @@ -70,16 +70,16 @@ namespace soge SOGE_INFO_LOG(R"(Created window "{}" of width {} and height {} with UUID {})", EAToNarrow(window.GetTitle()).c_str(), window.GetWidth(), window.GetHeight(), uuid.str()); + const auto eventModule = GetModule(); + m_shutdownRequested = false; while (!m_shutdownRequested) { Timestep::StartFrame(); Timestep::CalculateDelta(); - GetModule()->Update(); - - const auto eventModule = GetModule(); - eventModule->Dispatch(Timestep::DeltaTime()); + eventModule->Enqueue(Timestep::DeltaTime()); + eventModule->DispatchQueue(); for (const auto layer : m_renderLayers) { diff --git a/SOGE/source/SOGE/Input/InputModule.cpp b/SOGE/source/SOGE/Input/InputModule.cpp index 015fd35..f513c52 100644 --- a/SOGE/source/SOGE/Input/InputModule.cpp +++ b/SOGE/source/SOGE/Input/InputModule.cpp @@ -11,21 +11,24 @@ namespace soge { - InputModule::InputModule() : m_inputCore(nullptr) + InputModule::InputModule() : m_inputCore(nullptr), m_eventModule(nullptr) { Keys::Initialize(); } void InputModule::Load(di::Container& aContainer, ModuleManager& aModuleManager) { - aModuleManager.CreateModule(); m_inputCore = &aContainer.Provide(); + m_eventModule = &aModuleManager.CreateModule().first; + m_updateEventHandle = m_eventModule->PushBack([this](const UpdateEvent&) { Update(); }); SOGE_INFO_LOG("Input module loaded..."); } void InputModule::Unload(di::Container& aContainer, ModuleManager& aModuleManager) { + m_eventModule->Remove(m_updateEventHandle); + m_eventModule = nullptr; m_inputCore = nullptr; SOGE_INFO_LOG("Input module unloaded...");