From 0b9aaf799db34596b63b4bdbeff8ffc5548b9c71 Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 29 Jul 2020 11:17:20 +0800 Subject: [PATCH] add Module::BeforeRender & Module::AfterRender --- src/kiwano-imgui/ImGuiModule.cpp | 31 ++++++-------- src/kiwano-imgui/ImGuiModule.h | 4 +- src/kiwano/base/Module.cpp | 40 +++++++++++++++++-- src/kiwano/base/Module.h | 32 ++++++++++++--- src/kiwano/base/ObjectBase.cpp | 2 +- src/kiwano/base/ObjectBase.h | 4 +- .../render/DirectX/D2DDeviceResources.cpp | 1 - 7 files changed, 82 insertions(+), 32 deletions(-) diff --git a/src/kiwano-imgui/ImGuiModule.cpp b/src/kiwano-imgui/ImGuiModule.cpp index 84d1dff8..3331f82a 100644 --- a/src/kiwano-imgui/ImGuiModule.cpp +++ b/src/kiwano-imgui/ImGuiModule.cpp @@ -88,30 +88,25 @@ void ImGuiModule::OnUpdate(UpdateModuleContext& ctx) ctx.Next(); } -void ImGuiModule::OnRender(RenderModuleContext& ctx) +void ImGuiModule::BeforeRender(RenderModuleContext& ctx) { - // Before render - { - ImGui_Impl_NewFrame(); + ImGui_Impl_NewFrame(); - ImGuiIO& io = ImGui::GetIO(); - KGE_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built!"); + ImGuiIO& io = ImGui::GetIO(); + KGE_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built!"); - // Setup display size (every frame to accommodate for window resizing) - Size display_size = Renderer::GetInstance().GetOutputSize(); - io.DisplaySize = ImVec2(display_size.x, display_size.y); + // Setup display size (every frame to accommodate for window resizing) + Size display_size = Renderer::GetInstance().GetOutputSize(); + io.DisplaySize = ImVec2(display_size.x, display_size.y); - ImGui::NewFrame(); - } + ImGui::NewFrame(); +} - ctx.Next(); +void ImGuiModule::AfterRender(RenderModuleContext& ctx) +{ + ImGui::Render(); - // After render - { - ImGui::Render(); - - ImGui_Impl_RenderDrawData(ImGui::GetDrawData()); - } + ImGui_Impl_RenderDrawData(ImGui::GetDrawData()); } void ImGuiModule::HandleEvent(EventModuleContext& ctx) diff --git a/src/kiwano-imgui/ImGuiModule.h b/src/kiwano-imgui/ImGuiModule.h index 53ec924c..171e795b 100644 --- a/src/kiwano-imgui/ImGuiModule.h +++ b/src/kiwano-imgui/ImGuiModule.h @@ -47,7 +47,9 @@ public: void OnUpdate(UpdateModuleContext& ctx) override; - void OnRender(RenderModuleContext& ctx) override; + void BeforeRender(RenderModuleContext& ctx) override; + + void AfterRender(RenderModuleContext& ctx) override; void HandleEvent(EventModuleContext& ctx) override; diff --git a/src/kiwano/base/Module.cpp b/src/kiwano/base/Module.cpp index 5f492049..8a7287fe 100644 --- a/src/kiwano/base/Module.cpp +++ b/src/kiwano/base/Module.cpp @@ -32,6 +32,11 @@ ModuleContext::ModuleContext(ModuleList& modules) ModuleContext::~ModuleContext() {} +void ModuleContext::ResetIndex() +{ + index_ = -1; +} + void ModuleContext::Next() { index_++; @@ -43,19 +48,41 @@ void ModuleContext::Next() RenderModuleContext::RenderModuleContext(ModuleList& modules, RenderContext& ctx) : ModuleContext(modules) + , step_(Step::Before) , render_ctx(ctx) { + this->Next(); + this->ResetIndex(); + render_ctx.BeginDraw(); + step_ = Step::Rendering; } RenderModuleContext::~RenderModuleContext() { render_ctx.EndDraw(); + step_ = Step::After; + + this->ResetIndex(); + this->Next(); } void RenderModuleContext::Handle(Module* m) { - m->OnRender(*this); + switch (step_) + { + case RenderModuleContext::Step::Before: + m->BeforeRender(*this); + break; + case RenderModuleContext::Step::Rendering: + m->OnRender(*this); + break; + case RenderModuleContext::Step::After: + m->AfterRender(*this); + break; + default: + break; + } } UpdateModuleContext::UpdateModuleContext(ModuleList& modules, Duration dt) @@ -88,17 +115,22 @@ void Module::DestroyModule() {} void Module::OnRender(RenderModuleContext& ctx) { - ctx.Next(); } void Module::OnUpdate(UpdateModuleContext& ctx) { - ctx.Next(); } void Module::HandleEvent(EventModuleContext& ctx) { - ctx.Next(); +} + +void Module::BeforeRender(RenderModuleContext& ctx) +{ +} + +void Module::AfterRender(RenderModuleContext& ctx) +{ } } // namespace kiwano diff --git a/src/kiwano/base/Module.h b/src/kiwano/base/Module.h index 68c66676..7130088c 100644 --- a/src/kiwano/base/Module.h +++ b/src/kiwano/base/Module.h @@ -45,6 +45,8 @@ protected: virtual void Handle(Module* m) = 0; + void ResetIndex(); + private: int index_; ModuleList& modules_; @@ -63,6 +65,16 @@ public: protected: void Handle(Module* m) override; + +private: + enum class Step + { + Before, + Rendering, + After, + }; + + Step step_; }; /// \~chinese @@ -106,11 +118,6 @@ public: /// @brief 销毁模块 virtual void DestroyModule(); - /// \~chinese - /// @brief 渲染时 - /// @param ctx 渲染上下文 - virtual void OnRender(RenderModuleContext& ctx); - /// \~chinese /// @brief 更新时 /// @param ctx 更新上下文 @@ -121,6 +128,21 @@ public: /// @param ctx 事件上下文 virtual void HandleEvent(EventModuleContext& ctx); + /// \~chinese + /// @brief 渲染前 + /// @param ctx 渲染上下文 + virtual void BeforeRender(RenderModuleContext& ctx); + + /// \~chinese + /// @brief 渲染时 + /// @param ctx 渲染上下文 + virtual void OnRender(RenderModuleContext& ctx); + + /// \~chinese + /// @brief 渲染后 + /// @param ctx 渲染上下文 + virtual void AfterRender(RenderModuleContext& ctx); + protected: Module(); }; diff --git a/src/kiwano/base/ObjectBase.cpp b/src/kiwano/base/ObjectBase.cpp index b0b3e1c4..7dd31480 100644 --- a/src/kiwano/base/ObjectBase.cpp +++ b/src/kiwano/base/ObjectBase.cpp @@ -32,7 +32,7 @@ namespace bool tracing_leaks = false; Vector tracing_objects; std::atomic last_object_id = 0; -ObjectPolicyFunc object_policy_ = ObjectPolicy::Exception(); +ObjectPolicyFunc object_policy_ = ObjectPolicy::ErrorLog(); } // namespace diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h index 9705bebf..6bd6bb9a 100644 --- a/src/kiwano/base/ObjectBase.h +++ b/src/kiwano/base/ObjectBase.h @@ -115,13 +115,13 @@ struct ObjectPolicy static ObjectPolicyFunc WarnLog(int threshold = ObjectStatus::fail); /// \~chinese - /// @brief 在对象状态变为失败时打印错误日志 + /// @brief 在对象状态变为失败时打印错误日志(默认策略) /// @param threshold 触发阈值 /// @return 对象处理策略方法 static ObjectPolicyFunc ErrorLog(int threshold = ObjectStatus::fail); /// \~chinese - /// @brief 在对象状态变为失败时抛出 ObjectFailException(默认策略) + /// @brief 在对象状态变为失败时抛出 ObjectFailException /// @param threshold 触发阈值 /// @return 对象处理策略方法 static ObjectPolicyFunc Exception(int threshold = ObjectStatus::fail); diff --git a/src/kiwano/render/DirectX/D2DDeviceResources.cpp b/src/kiwano/render/DirectX/D2DDeviceResources.cpp index 1fb786b9..c151d133 100644 --- a/src/kiwano/render/DirectX/D2DDeviceResources.cpp +++ b/src/kiwano/render/DirectX/D2DDeviceResources.cpp @@ -19,7 +19,6 @@ // THE SOFTWARE. #include -#include #pragma comment(lib, "d2d1.lib") #pragma comment(lib, "dwrite.lib")