diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 6605cb27..d1da9997 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -69,7 +69,7 @@ bool e2d::Game::init(String sGameName) return s_bInitialized; } -int e2d::Game::start() +int e2d::Game::start(bool bAutoRelease/* true */) { if (!s_bInitialized) { @@ -115,6 +115,11 @@ int e2d::Game::start() } } + if (bAutoRelease) + { + Game::destroy(); + } + return 0; } @@ -150,18 +155,23 @@ void e2d::Game::destroy() { // 删除所有场景 SceneManager::__uninit(); - // 关闭输入 - Input::__uninit(); // 关闭播放器 MusicManager::__uninit(); // 清空定时器 TimerManager::__uninit(); + // 删除监听器 + InputManager::__uninit(); + ColliderManager::__uninit(); + // 删除动画 + ActionManager::__uninit(); + // 关闭输入 + Input::__uninit(); // 恢复计时操作 Time::__uninit(); // 清空图片缓存 Image::clearCache(); // 刷新内存池 - ObjectManager::__uninit(); + ObjectManager::__clear(); // 删除渲染相关资源 Renderer::__discardResources(); // 销毁窗口 diff --git a/core/Common/Object.cpp b/core/Common/Object.cpp index cb30430c..7f5eb300 100644 --- a/core/Common/Object.cpp +++ b/core/Common/Object.cpp @@ -23,7 +23,7 @@ void e2d::Object::release() { m_nRefCount--; // 通知对象管理池刷新 - ObjectManager::clear(); + ObjectManager::flush(); } int e2d::Object::getReferenceCount() const diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index 80bdff08..de223ffd 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -167,6 +167,16 @@ void e2d::ActionManager::__clearAllBindedWith(Node * pTargetNode) } } +void e2d::ActionManager::__uninit() +{ + FOR_LOOP(child, s_vActions) + { + SafeRelease(&child); + } + s_vActions.clear(); + s_vRunningActions.clear(); +} + void e2d::ActionManager::resumeAll() { FOR_LOOP(child, SceneManager::getCurrentScene()->getRoot()->getChildren()) diff --git a/core/Manager/ColliderManager.cpp b/core/Manager/ColliderManager.cpp index 00329c2d..5bc525dd 100644 --- a/core/Manager/ColliderManager.cpp +++ b/core/Manager/ColliderManager.cpp @@ -263,3 +263,12 @@ void e2d::ColliderManager::__removeCollider(Collider * pCollider) } } } + +void e2d::ColliderManager::__uninit() +{ + FOR_LOOP(listener, s_vListeners) + { + SafeRelease(&listener); + } + s_vListeners.clear(); +} diff --git a/core/Manager/InputManager.cpp b/core/Manager/InputManager.cpp index 93a7b6c3..6b6b2267 100644 --- a/core/Manager/InputManager.cpp +++ b/core/Manager/InputManager.cpp @@ -23,6 +23,15 @@ void e2d::InputManager::__update() } } +void e2d::InputManager::__uninit() +{ + FOR_LOOP(listener, s_vListeners) + { + SafeRelease(&listener); + } + s_vListeners.clear(); +} + void e2d::InputManager::__add(InputListener * pListener) { WARN_IF(pListener == nullptr, "InputListener NULL pointer exception!"); diff --git a/core/Manager/ObjectManager.cpp b/core/Manager/ObjectManager.cpp index 7986516e..fda16d16 100644 --- a/core/Manager/ObjectManager.cpp +++ b/core/Manager/ObjectManager.cpp @@ -37,14 +37,13 @@ void e2d::ObjectManager::__update() } } -void e2d::ObjectManager::__uninit() +void e2d::ObjectManager::__clear() { - // 释放两遍内存 - s_bNotifyed = true; - ObjectManager::__update(); - - s_bNotifyed = true; - ObjectManager::__update(); + if (s_vObjectPool.size() != 0) + { + s_bNotifyed = true; + ObjectManager::__update(); + } } void e2d::ObjectManager::add(e2d::Object * nptr) @@ -56,7 +55,7 @@ void e2d::ObjectManager::add(e2d::Object * nptr) } } -void e2d::ObjectManager::clear() +void e2d::ObjectManager::flush() { s_bNotifyed = true; } diff --git a/core/Manager/TimerManager.cpp b/core/Manager/TimerManager.cpp index 29213d97..cbf4ca6c 100644 --- a/core/Manager/TimerManager.cpp +++ b/core/Manager/TimerManager.cpp @@ -28,12 +28,28 @@ void e2d::TimerManager::__update() } } +e2d::Timer * e2d::TimerManager::start(String name, Function func, double interval, int times, bool atOnce, bool autoRelease) +{ + auto pTimer = new (std::nothrow) Timer(name, func, interval, times, atOnce, autoRelease); + if (pTimer) + { + pTimer->retain(); + pTimer->start(); + s_vTimers.push_back(pTimer); + } + return pTimer; +} + e2d::Timer* e2d::TimerManager::start(double timeOut, Function func) { - auto t = new (std::nothrow) Timer(L"", func, timeOut, 1, false, true); - t->start(); - TimerManager::add(t); - return t; + auto pTimer = new (std::nothrow) Timer(L"", func, timeOut, 1, false, true); + if (pTimer) + { + pTimer->retain(); + pTimer->start(); + s_vTimers.push_back(pTimer); + } + return pTimer; } void e2d::TimerManager::add(Timer * pTimer) diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index b5581d15..f0063dab 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -11,7 +11,7 @@ e2d::Timer::Timer(String name, Function func, double interval /* = 0 */, int upd , m_nUpdateTimes(-1) , m_bAtOnce(false) , m_bAutoRelease(false) - , m_bClear(true) + , m_bClear(false) { this->setName(name); this->setFunc(func); @@ -39,8 +39,23 @@ void e2d::Timer::stopAndClear() void e2d::Timer::start() { - m_bRunning = true; - m_fLast = Time::getTotalTime(); + if (!m_bRunning) + { + m_bRunning = true; + m_fLast = Time::getTotalTime(); + m_nRunTimes = 0; + } +} + +void e2d::Timer::start(int times) +{ + if (!m_bRunning) + { + m_bRunning = true; + m_fLast = Time::getTotalTime(); + m_nUpdateTimes = times; + m_nRunTimes = 0; + } } e2d::String e2d::Timer::getName() const @@ -66,7 +81,6 @@ void e2d::Timer::setFunc(Function func) void e2d::Timer::setUpdateTimes(int updateTimes) { m_nUpdateTimes = updateTimes; - m_bClear = (m_nUpdateTimes == 0); } void e2d::Timer::setRunAtOnce(bool bAtOnce) diff --git a/core/ebase.h b/core/ebase.h index 6e1bea0a..d8a04f67 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -18,7 +18,9 @@ public: ); // 启动游戏 - static int start(); + static int start( + bool bAutoRelease = true + ); // 暂停游戏 static void pause(); diff --git a/core/emanager.h b/core/emanager.h index 62fe9d4a..cc64ca77 100644 --- a/core/emanager.h +++ b/core/emanager.h @@ -29,14 +29,14 @@ public: ); // 释放垃圾对象的内存空间 - static void clear(); + static void flush(); private: // 更新对象管理器 static void __update(); // 清空所有对象 - static void __uninit(); + static void __clear(); }; @@ -94,6 +94,16 @@ class TimerManager friend Timer; public: + // 启动一个新任务 + static Timer* start( + String name, /* 任务名称 */ + Function func, /* 执行函数 */ + double interval = 0, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ + bool atOnce = false, /* 是否立即执行 */ + bool autoRelease = false /* 自动清除 */ + ); + // 启动一个新任务:等待一段时间后执行指定函数 static Timer* start( double timeOut, /* 等待的时长(秒) */ @@ -231,6 +241,9 @@ private: // 重置所有动作状态 static void __resetAllActions(); + + // 回收资源 + static void __uninit(); }; @@ -307,6 +320,7 @@ private: // 键盘和鼠标消息管理器 class InputManager { + friend Game; friend Input; friend InputListener; @@ -357,12 +371,16 @@ private: // 更新监听器 static void __update(); + + // 回收资源 + static void __uninit(); }; // 碰撞管理器 class ColliderManager { + friend Game; friend Node; friend Collider; friend CollisionListener; @@ -450,6 +468,9 @@ private: static void __removeCollider( Collider * pCollider ); + + // 回收资源 + static void __uninit(); }; } \ No newline at end of file diff --git a/core/etool.h b/core/etool.h index ba14e32a..2b1ece94 100644 --- a/core/etool.h +++ b/core/etool.h @@ -173,12 +173,17 @@ public: double interval = 0, /* 时间间隔(秒) */ int times = -1, /* 执行次数(设 -1 为永久执行) */ bool atOnce = false, /* 是否立即执行 */ - bool autoRelease = false /* 自动清除 */ + bool autoRelease = false /* 执行结束时自动清除 */ ); // 启动定时器 void start(); + // 启动定时器,并执行指定次数 + void start( + int times /* 执行次数(设 -1 为永久执行) */ + ); + // 停止定时器 void stop();