refactor(core): 使用全局变量替换Director中的成员变量

将Director类中的Scheduler和SvcMgr成员变量替换为全局变量SCHED和SVC_MGR,
并添加IService的友元声明以允许SvcMgr访问其私有成员
This commit is contained in:
ChestnutYueyue 2026-02-27 20:55:06 +08:00
parent 71eeeac033
commit 5ef1873a44
3 changed files with 15 additions and 22 deletions

View File

@ -20,8 +20,8 @@ public:
void mainLoop(float dt);
void mainLoopParallel(float dt);
Scheduler& sched() { return *sched_; }
SvcMgr& svcs() { return *svcMgr_; }
Scheduler& sched() { return SCHED; }
SvcMgr& svcs() { return SVC_MGR; }
float dt() const { return dt_; }
float totalTime() const { return totalTime_; }
@ -36,9 +36,6 @@ public:
private:
Director() = default;
Unique<Scheduler> sched_;
Unique<SvcMgr> svcMgr_;
float dt_ = 0.0f;
float totalTime_ = 0.0f;
float fixedAccumulator_ = 0.0f;

View File

@ -37,6 +37,8 @@ protected:
IService() = default;
bool inited_ = false;
bool enabled_ = true;
friend class SvcMgr;
};
/**

View File

@ -10,9 +10,6 @@ Director& Director::inst() {
bool Director::init() {
if (inited_) return true;
sched_ = makeUnique<Scheduler>();
svcMgr_ = makeUnique<SvcMgr>();
inited_ = true;
return true;
}
@ -20,11 +17,8 @@ bool Director::init() {
void Director::shutdown() {
if (!inited_) return;
svcMgr_->shutdownAll();
sched_->unscheduleAll();
svcMgr_.reset();
sched_.reset();
SVC_MGR.shutdownAll();
SCHED.unscheduleAll();
inited_ = false;
}
@ -36,17 +30,17 @@ void Director::mainLoop(float dt) {
totalTime_ += dt;
frameCount_++;
svcMgr_->updateAll(dt);
SVC_MGR.updateAll(dt);
fixedAccumulator_ += dt;
while (fixedAccumulator_ >= fixedDt_) {
svcMgr_->fixedUpdateAll(fixedDt_);
SVC_MGR.fixedUpdateAll(fixedDt_);
fixedAccumulator_ -= fixedDt_;
}
sched_->update(dt);
SCHED.update(dt);
svcMgr_->lateUpdateAll(dt);
SVC_MGR.lateUpdateAll(dt);
}
void Director::mainLoopParallel(float dt) {
@ -56,17 +50,17 @@ void Director::mainLoopParallel(float dt) {
totalTime_ += dt;
frameCount_++;
svcMgr_->updateAll(dt);
SVC_MGR.updateAll(dt);
fixedAccumulator_ += dt;
while (fixedAccumulator_ >= fixedDt_) {
svcMgr_->fixedUpdateAll(fixedDt_);
SVC_MGR.fixedUpdateAll(fixedDt_);
fixedAccumulator_ -= fixedDt_;
}
sched_->updateParallel(dt);
SCHED.updateParallel(dt);
svcMgr_->lateUpdateAll(dt);
SVC_MGR.lateUpdateAll(dt);
}
void Director::pause() {
@ -78,7 +72,7 @@ void Director::resume() {
}
void Director::setTimeScale(float scale) {
sched_->setTimeScale(scale);
SCHED.setTimeScale(scale);
}
} // namespace extra2d