add Module::BeforeRender & Module::AfterRender

This commit is contained in:
Nomango 2020-07-29 11:17:20 +08:00
parent 1c8c92862a
commit 0b9aaf799d
7 changed files with 82 additions and 32 deletions

View File

@ -88,9 +88,7 @@ void ImGuiModule::OnUpdate(UpdateModuleContext& ctx)
ctx.Next(); ctx.Next();
} }
void ImGuiModule::OnRender(RenderModuleContext& ctx) void ImGuiModule::BeforeRender(RenderModuleContext& ctx)
{
// Before render
{ {
ImGui_Impl_NewFrame(); ImGui_Impl_NewFrame();
@ -104,15 +102,12 @@ void ImGuiModule::OnRender(RenderModuleContext& ctx)
ImGui::NewFrame(); ImGui::NewFrame();
} }
ctx.Next(); void ImGuiModule::AfterRender(RenderModuleContext& ctx)
// After render
{ {
ImGui::Render(); ImGui::Render();
ImGui_Impl_RenderDrawData(ImGui::GetDrawData()); ImGui_Impl_RenderDrawData(ImGui::GetDrawData());
} }
}
void ImGuiModule::HandleEvent(EventModuleContext& ctx) void ImGuiModule::HandleEvent(EventModuleContext& ctx)
{ {

View File

@ -47,7 +47,9 @@ public:
void OnUpdate(UpdateModuleContext& ctx) override; 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; void HandleEvent(EventModuleContext& ctx) override;

View File

@ -32,6 +32,11 @@ ModuleContext::ModuleContext(ModuleList& modules)
ModuleContext::~ModuleContext() {} ModuleContext::~ModuleContext() {}
void ModuleContext::ResetIndex()
{
index_ = -1;
}
void ModuleContext::Next() void ModuleContext::Next()
{ {
index_++; index_++;
@ -43,19 +48,41 @@ void ModuleContext::Next()
RenderModuleContext::RenderModuleContext(ModuleList& modules, RenderContext& ctx) RenderModuleContext::RenderModuleContext(ModuleList& modules, RenderContext& ctx)
: ModuleContext(modules) : ModuleContext(modules)
, step_(Step::Before)
, render_ctx(ctx) , render_ctx(ctx)
{ {
this->Next();
this->ResetIndex();
render_ctx.BeginDraw(); render_ctx.BeginDraw();
step_ = Step::Rendering;
} }
RenderModuleContext::~RenderModuleContext() RenderModuleContext::~RenderModuleContext()
{ {
render_ctx.EndDraw(); render_ctx.EndDraw();
step_ = Step::After;
this->ResetIndex();
this->Next();
} }
void RenderModuleContext::Handle(Module* m) void RenderModuleContext::Handle(Module* m)
{ {
switch (step_)
{
case RenderModuleContext::Step::Before:
m->BeforeRender(*this);
break;
case RenderModuleContext::Step::Rendering:
m->OnRender(*this); m->OnRender(*this);
break;
case RenderModuleContext::Step::After:
m->AfterRender(*this);
break;
default:
break;
}
} }
UpdateModuleContext::UpdateModuleContext(ModuleList& modules, Duration dt) UpdateModuleContext::UpdateModuleContext(ModuleList& modules, Duration dt)
@ -88,17 +115,22 @@ void Module::DestroyModule() {}
void Module::OnRender(RenderModuleContext& ctx) void Module::OnRender(RenderModuleContext& ctx)
{ {
ctx.Next();
} }
void Module::OnUpdate(UpdateModuleContext& ctx) void Module::OnUpdate(UpdateModuleContext& ctx)
{ {
ctx.Next();
} }
void Module::HandleEvent(EventModuleContext& ctx) void Module::HandleEvent(EventModuleContext& ctx)
{ {
ctx.Next(); }
void Module::BeforeRender(RenderModuleContext& ctx)
{
}
void Module::AfterRender(RenderModuleContext& ctx)
{
} }
} // namespace kiwano } // namespace kiwano

View File

@ -45,6 +45,8 @@ protected:
virtual void Handle(Module* m) = 0; virtual void Handle(Module* m) = 0;
void ResetIndex();
private: private:
int index_; int index_;
ModuleList& modules_; ModuleList& modules_;
@ -63,6 +65,16 @@ public:
protected: protected:
void Handle(Module* m) override; void Handle(Module* m) override;
private:
enum class Step
{
Before,
Rendering,
After,
};
Step step_;
}; };
/// \~chinese /// \~chinese
@ -106,11 +118,6 @@ public:
/// @brief 销毁模块 /// @brief 销毁模块
virtual void DestroyModule(); virtual void DestroyModule();
/// \~chinese
/// @brief 渲染时
/// @param ctx 渲染上下文
virtual void OnRender(RenderModuleContext& ctx);
/// \~chinese /// \~chinese
/// @brief 更新时 /// @brief 更新时
/// @param ctx 更新上下文 /// @param ctx 更新上下文
@ -121,6 +128,21 @@ public:
/// @param ctx 事件上下文 /// @param ctx 事件上下文
virtual void HandleEvent(EventModuleContext& 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: protected:
Module(); Module();
}; };

View File

@ -32,7 +32,7 @@ namespace
bool tracing_leaks = false; bool tracing_leaks = false;
Vector<ObjectBase*> tracing_objects; Vector<ObjectBase*> tracing_objects;
std::atomic<uint64_t> last_object_id = 0; std::atomic<uint64_t> last_object_id = 0;
ObjectPolicyFunc object_policy_ = ObjectPolicy::Exception(); ObjectPolicyFunc object_policy_ = ObjectPolicy::ErrorLog();
} // namespace } // namespace

View File

@ -115,13 +115,13 @@ struct ObjectPolicy
static ObjectPolicyFunc WarnLog(int threshold = ObjectStatus::fail); static ObjectPolicyFunc WarnLog(int threshold = ObjectStatus::fail);
/// \~chinese /// \~chinese
/// @brief 在对象状态变为失败时打印错误日志 /// @brief 在对象状态变为失败时打印错误日志(默认策略)
/// @param threshold 触发阈值 /// @param threshold 触发阈值
/// @return 对象处理策略方法 /// @return 对象处理策略方法
static ObjectPolicyFunc ErrorLog(int threshold = ObjectStatus::fail); static ObjectPolicyFunc ErrorLog(int threshold = ObjectStatus::fail);
/// \~chinese /// \~chinese
/// @brief 在对象状态变为失败时抛出 ObjectFailException(默认策略) /// @brief 在对象状态变为失败时抛出 ObjectFailException
/// @param threshold 触发阈值 /// @param threshold 触发阈值
/// @return 对象处理策略方法 /// @return 对象处理策略方法
static ObjectPolicyFunc Exception(int threshold = ObjectStatus::fail); static ObjectPolicyFunc Exception(int threshold = ObjectStatus::fail);

View File

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
#include <kiwano/render/DirectX/D2DDeviceResources.h> #include <kiwano/render/DirectX/D2DDeviceResources.h>
#include <kiwano/utils/Logger.h>
#pragma comment(lib, "d2d1.lib") #pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib") #pragma comment(lib, "dwrite.lib")