From 9e3dfff7b6c86a2d5b3e205d43661187ff6cd45d Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Tue, 19 Jan 2016 00:52:17 +0100 Subject: [PATCH] WIP world pausing --- include/Engine/Core/EPause.h | 22 ++++++++++++++++++++++ include/Engine/Core/SystemPipeline.h | 26 +++++++++++++++++++++++++- include/Engine/Editor/EditorGUI.h | 6 ++++-- src/Engine/Editor/EditorGUI.cpp | 25 ++++++++++++++++++------- src/Engine/Editor/EditorSystem.cpp | 4 ++-- src/Game/Game.cpp | 2 +- 6 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 include/Engine/Core/EPause.h diff --git a/include/Engine/Core/EPause.h b/include/Engine/Core/EPause.h new file mode 100644 index 00000000..5aca36d8 --- /dev/null +++ b/include/Engine/Core/EPause.h @@ -0,0 +1,22 @@ +#ifndef EPause_h__ +#define EPause_h__ + +#include "EventBroker.h" +#include "World.h" + +namespace Events +{ + +struct Pause : Event +{ + ::World* World; +}; + +struct Resume : Event +{ + ::World* World; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index cc3c98e9..90303f12 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -5,6 +5,7 @@ #include "EventBroker.h" #include "System.h" #include "World.h" +#include "EPause.h" class SystemPipeline { @@ -12,7 +13,10 @@ public: SystemPipeline(World* world, EventBroker* eventBroker) : m_World(world) , m_EventBroker(eventBroker) - { } + { + EVENT_SUBSCRIBE_MEMBER(m_EPause, &SystemPipeline::OnPause); + EVENT_SUBSCRIBE_MEMBER(m_EResume, &SystemPipeline::OnResume); + } ~SystemPipeline() { @@ -51,6 +55,10 @@ public: void Update(double dt) { + if (m_Paused) { + dt = 0.0; + } + for (UnorderedSystems& group : m_OrderedSystemGroups) { // Process events for (auto& pair : group.Systems) { @@ -80,6 +88,7 @@ public: private: World* m_World; EventBroker* m_EventBroker; + bool m_Paused = false; struct UnorderedSystems { @@ -88,6 +97,21 @@ private: std::vector ImpureSystems; }; std::vector m_OrderedSystemGroups; + + EventRelay m_EPause; + bool OnPause(const Events::Pause& e) { + if (e.World == m_World) { + m_Paused = true; + } + return true; + } + EventRelay m_EResume; + bool OnResume(const Events::Resume& e) { + if (e.World == m_World) { + m_Paused = false; + } + return true; + } }; #endif \ No newline at end of file diff --git a/include/Engine/Editor/EditorGUI.h b/include/Engine/Editor/EditorGUI.h index 917d216f..7e36f211 100644 --- a/include/Engine/Editor/EditorGUI.h +++ b/include/Engine/Editor/EditorGUI.h @@ -14,14 +14,15 @@ #include "../Core/World.h" #include "../Core/EntityWrapper.h" #include "../Core/ResourceManager.h" +#include "../Core/EPause.h" #include "../Rendering/Texture.h" class EditorGUI { public: - EditorGUI(EventBroker* eventBroker); + EditorGUI(World* world, EventBroker* eventBroker); - void Draw(World* world); + void Draw(); void SelectEntity(EntityWrapper entity); @@ -57,6 +58,7 @@ public: void SetComponentDeleteCallback(OnComponentDelete_t f) { m_OnComponentDelete = f; } private: + World* m_World; EventBroker* m_EventBroker; // Config variables diff --git a/src/Engine/Editor/EditorGUI.cpp b/src/Engine/Editor/EditorGUI.cpp index b7a7fbb9..e3c714bd 100644 --- a/src/Engine/Editor/EditorGUI.cpp +++ b/src/Engine/Editor/EditorGUI.cpp @@ -1,17 +1,18 @@ #include "Editor/EditorGUI.h" -EditorGUI::EditorGUI(EventBroker* eventBroker) - : m_EventBroker(eventBroker) +EditorGUI::EditorGUI(World* world, EventBroker* eventBroker) + : m_World(world) + , m_EventBroker(eventBroker) { } -void EditorGUI::Draw(World* world) +void EditorGUI::Draw() { ImGui::ShowTestWindow(); drawMenu(); drawTools(); - drawEntities(world); + drawEntities(m_World); drawComponents(m_CurrentSelection); } @@ -65,10 +66,20 @@ void EditorGUI::drawTools() pauseIcon = ResourceManager::Load("Textures/Icons/Pause.png")->m_Texture; } catch (const std::exception&) { } - - ImGui::ImageButton((void*)playIcon, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), ImVec4(0, 1, 0, 1)); + static bool paused = false; + if (ImGui::ImageButton((void*)playIcon, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), (!paused) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1))) { + Events::Resume e; + e.World = m_World; + m_EventBroker->Publish(e); + paused = false; + } ImGui::SameLine(); - ImGui::ImageButton((void*)pauseIcon, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0)); + if (ImGui::ImageButton((void*)pauseIcon, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), (paused) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1))) { + Events::Pause e; + e.World = m_World; + m_EventBroker->Publish(e); + paused = true; + } ImGui::End(); } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 64f16cc6..05e9974b 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -19,7 +19,7 @@ EditorSystem::EditorSystem(World* world, EventBroker* eventBroker, IRenderer* re m_EditorWorld->AttachComponent(m_Camera.ID, "Camera"); m_DebugCameraInputController = new DebugCameraInputController(m_EventBroker, -1); - m_EditorGUI = new EditorGUI(m_EventBroker); + m_EditorGUI = new EditorGUI(m_World, m_EventBroker); m_EditorGUI->SetEntitySelectedCallback(std::bind(&EditorSystem::OnEntitySelected, this, std::placeholders::_1)); m_EditorGUI->SetEntityImportCallback(std::bind(&EditorSystem::importEntity, this, std::placeholders::_1, std::placeholders::_2)); m_EditorGUI->SetEntitySaveCallback(std::bind(&EditorSystem::OnEntitySave, this, std::placeholders::_1, std::placeholders::_2)); @@ -49,7 +49,7 @@ void EditorSystem::Update(double dt) { m_EditorWorldSystemPipeline->Update(dt); - m_EditorGUI->Draw(m_World); + m_EditorGUI->Draw(); m_EditorStats->Draw(dt); m_DebugCameraInputController->Update(dt); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index d60863bb..0f6d3dc5 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -27,7 +27,6 @@ Game::Game(int argc, char* argv[]) // Create the core event broker m_EventBroker = new EventBroker(); - // Create the renderer m_Renderer = new Renderer(m_EventBroker, m_World); m_Renderer->SetFullscreen(m_Config->Get("Video.Fullscreen", false)); @@ -143,6 +142,7 @@ void Game::Tick() m_ClientOrServer->Update(); } // Iterate through systems and update world! + m_EventBroker->Process(); m_SystemPipeline->Update(dt); debugTick(dt); m_Renderer->Update(dt);