92 lines
1.7 KiB
C++
92 lines
1.7 KiB
C++
#include <context/context.h>
|
|
#include <event/events.h>
|
|
#include <module/module_registry.h>
|
|
#include <plugin/plugin_loader.h>
|
|
#include <renderer/renderer_module.h>
|
|
#include <utils/timer_module.h>
|
|
|
|
namespace extra2d {
|
|
|
|
Context::Context() : pluginLoader_(std::make_unique<PluginLoader>()) {}
|
|
|
|
Context::~Context() {
|
|
if (inited_) {
|
|
shutdown();
|
|
}
|
|
}
|
|
|
|
Context::Context(Context &&) noexcept = default;
|
|
Context &Context::operator=(Context &&) noexcept = default;
|
|
|
|
std::unique_ptr<Context> Context::create() {
|
|
return std::make_unique<Context>();
|
|
}
|
|
|
|
bool Context::init() {
|
|
if (inited_) {
|
|
return true;
|
|
}
|
|
|
|
// 发送引擎初始化事件
|
|
events::OnInit::emit();
|
|
|
|
// 初始化所有插件
|
|
if (!pluginLoader_->initAll()) {
|
|
return false;
|
|
}
|
|
|
|
inited_ = true;
|
|
running_ = true;
|
|
return true;
|
|
}
|
|
|
|
void Context::shutdown() {
|
|
if (!inited_) {
|
|
return;
|
|
}
|
|
|
|
running_ = false;
|
|
|
|
// 关闭所有插件
|
|
pluginLoader_->shutdownAll();
|
|
|
|
// 发送引擎关闭事件
|
|
events::OnShutdown::emit();
|
|
|
|
inited_ = false;
|
|
}
|
|
|
|
void Context::tick(float dt) {
|
|
if (!running_) {
|
|
return;
|
|
}
|
|
|
|
// 更新时间和帧数
|
|
totalTime_ += dt;
|
|
frameCount_++;
|
|
|
|
// 发送更新事件
|
|
events::OnUpdate::emit(dt);
|
|
|
|
// 发送延迟更新事件
|
|
events::OnLateUpdate::emit(dt);
|
|
|
|
// 渲染阶段
|
|
// 1. 渲染开始
|
|
events::OnRenderBegin::emit();
|
|
|
|
// 2. 场景图提交渲染命令(通过 OnRenderSubmit 事件)
|
|
|
|
// 3. 渲染结束并执行绘制
|
|
events::OnRenderEnd::emit();
|
|
|
|
// 4. 呈现渲染结果(交换缓冲区)
|
|
events::OnRenderPresent::emit();
|
|
}
|
|
|
|
ModuleRegistry &Context::modules() { return ModuleRegistry::instance(); }
|
|
|
|
PluginLoader &Context::plugins() { return *pluginLoader_; }
|
|
|
|
} // namespace extra2d
|