2026-02-28 20:56:11 +08:00
|
|
|
#include <context/context.h>
|
2026-03-01 00:01:48 +08:00
|
|
|
#include <event/events.h>
|
2026-02-28 20:56:11 +08:00
|
|
|
#include <module/module_registry.h>
|
|
|
|
|
#include <plugin/plugin_loader.h>
|
2026-03-01 00:01:48 +08:00
|
|
|
#include <utils/timer_module.h>
|
2026-03-01 15:39:07 +08:00
|
|
|
#include <resource/resource_manager.h>
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
Context::Context()
|
2026-03-01 00:01:48 +08:00
|
|
|
: pluginLoader_(std::make_unique<PluginLoader>()),
|
2026-03-01 15:39:07 +08:00
|
|
|
timerModule_(std::make_unique<TimerModule>()),
|
|
|
|
|
resourceManager_(std::make_unique<ResourceManager>()) {}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
Context::~Context() {
|
2026-03-01 00:01:48 +08:00
|
|
|
if (inited_) {
|
|
|
|
|
shutdown();
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
Context::Context(Context &&) noexcept = default;
|
|
|
|
|
Context &Context::operator=(Context &&) noexcept = default;
|
2026-02-28 20:56:11 +08:00
|
|
|
|
|
|
|
|
std::unique_ptr<Context> Context::create() {
|
2026-03-01 00:01:48 +08:00
|
|
|
return std::make_unique<Context>();
|
2026-02-28 20:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Context::init() {
|
2026-03-01 00:01:48 +08:00
|
|
|
if (inited_) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 发送引擎初始化事件
|
|
|
|
|
events::OnInit::emit();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 15:39:07 +08:00
|
|
|
// 初始化资源管理器
|
|
|
|
|
if (!resourceManager_->init()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 初始化定时器模块
|
|
|
|
|
if (!timerModule_->init()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 初始化所有插件
|
|
|
|
|
if (!pluginLoader_->initAll()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
inited_ = true;
|
|
|
|
|
running_ = true;
|
|
|
|
|
return true;
|
2026-02-28 20:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Context::shutdown() {
|
2026-03-01 00:01:48 +08:00
|
|
|
if (!inited_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
running_ = false;
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 关闭所有插件
|
|
|
|
|
pluginLoader_->shutdownAll();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 关闭定时器模块
|
|
|
|
|
timerModule_->shutdown();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 发送引擎关闭事件
|
|
|
|
|
events::OnShutdown::emit();
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 15:39:07 +08:00
|
|
|
// 关闭资源管理器
|
|
|
|
|
resourceManager_->shutdown();
|
|
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
inited_ = false;
|
2026-02-28 20:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Context::tick(float dt) {
|
2026-03-01 00:01:48 +08:00
|
|
|
if (!running_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 更新时间和帧数
|
|
|
|
|
totalTime_ += dt;
|
|
|
|
|
frameCount_++;
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 更新定时器模块
|
|
|
|
|
timerModule_->update(dt);
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 发送更新事件
|
|
|
|
|
events::OnUpdate::emit(dt);
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
// 发送延迟更新事件
|
|
|
|
|
events::OnLateUpdate::emit(dt);
|
2026-02-28 20:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
ModuleRegistry &Context::modules() { return ModuleRegistry::instance(); }
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
PluginLoader &Context::plugins() { return *pluginLoader_; }
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 00:01:48 +08:00
|
|
|
TimerModule &Context::timer() { return *timerModule_; }
|
2026-02-28 20:56:11 +08:00
|
|
|
|
2026-03-01 15:39:07 +08:00
|
|
|
ResourceManager &Context::resources() { return *resourceManager_; }
|
|
|
|
|
|
2026-02-28 20:56:11 +08:00
|
|
|
} // namespace extra2d
|