Extra2D/src/context/context.cpp

107 lines
1.9 KiB
C++
Raw Normal View History

#include <context/context.h>
#include <module/module_registry.h>
#include <module/timer_module.h>
#include <plugin/plugin_loader.h>
#include <event/events.h>
#include <algorithm>
namespace extra2d {
Context::Context()
: moduleRegistry_(std::make_unique<ModuleRegistry>())
, pluginLoader_(std::make_unique<PluginLoader>())
, timerModule_(std::make_unique<TimerModule>())
{
}
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();
// 注册核心模块
moduleRegistry_->registerModule(timerModule_.get());
// 初始化所有模块
if (!moduleRegistry_->initAll()) {
return false;
}
// 初始化所有插件
if (!pluginLoader_->initAll()) {
return false;
}
inited_ = true;
running_ = true;
return true;
}
void Context::shutdown() {
if (!inited_) {
return;
}
running_ = false;
// 关闭所有插件
pluginLoader_->shutdownAll();
// 关闭所有模块
moduleRegistry_->shutdownAll();
// 发送引擎关闭事件
events::OnShutdown::emit();
inited_ = false;
}
void Context::tick(float dt) {
if (!running_) {
return;
}
// 更新时间和帧数
totalTime_ += dt;
frameCount_++;
// 更新定时器模块
timerModule_->update(dt);
// 发送更新事件
events::OnUpdate::emit(dt);
// 发送延迟更新事件
events::OnLateUpdate::emit(dt);
}
ModuleRegistry& Context::modules() {
return *moduleRegistry_;
}
PluginLoader& Context::plugins() {
return *pluginLoader_;
}
TimerModule& Context::timer() {
return *timerModule_;
}
} // namespace extra2d