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,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)

View File

@ -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;

View File

@ -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

View File

@ -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();
};

View File

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

View File

@ -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);

View File

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