Game::start增加bool参数,为true时自动在游戏结束时回收资源

This commit is contained in:
Nomango 2018-04-21 00:46:26 +08:00
parent afb24f7a32
commit 3d00ae7189
11 changed files with 120 additions and 25 deletions

View File

@ -69,7 +69,7 @@ bool e2d::Game::init(String sGameName)
return s_bInitialized; return s_bInitialized;
} }
int e2d::Game::start() int e2d::Game::start(bool bAutoRelease/* true */)
{ {
if (!s_bInitialized) if (!s_bInitialized)
{ {
@ -115,6 +115,11 @@ int e2d::Game::start()
} }
} }
if (bAutoRelease)
{
Game::destroy();
}
return 0; return 0;
} }
@ -150,18 +155,23 @@ void e2d::Game::destroy()
{ {
// 删除所有场景 // 删除所有场景
SceneManager::__uninit(); SceneManager::__uninit();
// 关闭输入
Input::__uninit();
// 关闭播放器 // 关闭播放器
MusicManager::__uninit(); MusicManager::__uninit();
// 清空定时器 // 清空定时器
TimerManager::__uninit(); TimerManager::__uninit();
// 删除监听器
InputManager::__uninit();
ColliderManager::__uninit();
// 删除动画
ActionManager::__uninit();
// 关闭输入
Input::__uninit();
// 恢复计时操作 // 恢复计时操作
Time::__uninit(); Time::__uninit();
// 清空图片缓存 // 清空图片缓存
Image::clearCache(); Image::clearCache();
// 刷新内存池 // 刷新内存池
ObjectManager::__uninit(); ObjectManager::__clear();
// 删除渲染相关资源 // 删除渲染相关资源
Renderer::__discardResources(); Renderer::__discardResources();
// 销毁窗口 // 销毁窗口

View File

@ -23,7 +23,7 @@ void e2d::Object::release()
{ {
m_nRefCount--; m_nRefCount--;
// 通知对象管理池刷新 // 通知对象管理池刷新
ObjectManager::clear(); ObjectManager::flush();
} }
int e2d::Object::getReferenceCount() const int e2d::Object::getReferenceCount() const

View File

@ -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() void e2d::ActionManager::resumeAll()
{ {
FOR_LOOP(child, SceneManager::getCurrentScene()->getRoot()->getChildren()) FOR_LOOP(child, SceneManager::getCurrentScene()->getRoot()->getChildren())

View File

@ -263,3 +263,12 @@ void e2d::ColliderManager::__removeCollider(Collider * pCollider)
} }
} }
} }
void e2d::ColliderManager::__uninit()
{
FOR_LOOP(listener, s_vListeners)
{
SafeRelease(&listener);
}
s_vListeners.clear();
}

View File

@ -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) void e2d::InputManager::__add(InputListener * pListener)
{ {
WARN_IF(pListener == nullptr, "InputListener NULL pointer exception!"); WARN_IF(pListener == nullptr, "InputListener NULL pointer exception!");

View File

@ -37,15 +37,14 @@ void e2d::ObjectManager::__update()
} }
} }
void e2d::ObjectManager::__uninit() void e2d::ObjectManager::__clear()
{
if (s_vObjectPool.size() != 0)
{ {
// ÊÍ·ÅÁ½±éÄÚ´æ
s_bNotifyed = true;
ObjectManager::__update();
s_bNotifyed = true; s_bNotifyed = true;
ObjectManager::__update(); ObjectManager::__update();
} }
}
void e2d::ObjectManager::add(e2d::Object * nptr) 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; s_bNotifyed = true;
} }

View File

@ -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) e2d::Timer* e2d::TimerManager::start(double timeOut, Function func)
{ {
auto t = new (std::nothrow) Timer(L"", func, timeOut, 1, false, true); auto pTimer = new (std::nothrow) Timer(L"", func, timeOut, 1, false, true);
t->start(); if (pTimer)
TimerManager::add(t); {
return t; pTimer->retain();
pTimer->start();
s_vTimers.push_back(pTimer);
}
return pTimer;
} }
void e2d::TimerManager::add(Timer * pTimer) void e2d::TimerManager::add(Timer * pTimer)

View File

@ -11,7 +11,7 @@ e2d::Timer::Timer(String name, Function func, double interval /* = 0 */, int upd
, m_nUpdateTimes(-1) , m_nUpdateTimes(-1)
, m_bAtOnce(false) , m_bAtOnce(false)
, m_bAutoRelease(false) , m_bAutoRelease(false)
, m_bClear(true) , m_bClear(false)
{ {
this->setName(name); this->setName(name);
this->setFunc(func); this->setFunc(func);
@ -38,9 +38,24 @@ void e2d::Timer::stopAndClear()
} }
void e2d::Timer::start() void e2d::Timer::start()
{
if (!m_bRunning)
{ {
m_bRunning = true; m_bRunning = true;
m_fLast = Time::getTotalTime(); 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 e2d::String e2d::Timer::getName() const
@ -66,7 +81,6 @@ void e2d::Timer::setFunc(Function func)
void e2d::Timer::setUpdateTimes(int updateTimes) void e2d::Timer::setUpdateTimes(int updateTimes)
{ {
m_nUpdateTimes = updateTimes; m_nUpdateTimes = updateTimes;
m_bClear = (m_nUpdateTimes == 0);
} }
void e2d::Timer::setRunAtOnce(bool bAtOnce) void e2d::Timer::setRunAtOnce(bool bAtOnce)

View File

@ -18,7 +18,9 @@ public:
); );
// 启动游戏 // 启动游戏
static int start(); static int start(
bool bAutoRelease = true
);
// 暂停游戏 // 暂停游戏
static void pause(); static void pause();

View File

@ -29,14 +29,14 @@ public:
); );
// 释放垃圾对象的内存空间 // 释放垃圾对象的内存空间
static void clear(); static void flush();
private: private:
// 更新对象管理器 // 更新对象管理器
static void __update(); static void __update();
// 清空所有对象 // 清空所有对象
static void __uninit(); static void __clear();
}; };
@ -94,6 +94,16 @@ class TimerManager
friend Timer; friend Timer;
public: public:
// 启动一个新任务
static Timer* start(
String name, /* 任务名称 */
Function func, /* 执行函数 */
double interval = 0, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */
bool atOnce = false, /* 是否立即执行 */
bool autoRelease = false /* 自动清除 */
);
// 启动一个新任务:等待一段时间后执行指定函数 // 启动一个新任务:等待一段时间后执行指定函数
static Timer* start( static Timer* start(
double timeOut, /* 等待的时长(秒) */ double timeOut, /* 等待的时长(秒) */
@ -231,6 +241,9 @@ private:
// 重置所有动作状态 // 重置所有动作状态
static void __resetAllActions(); static void __resetAllActions();
// 回收资源
static void __uninit();
}; };
@ -307,6 +320,7 @@ private:
// 键盘和鼠标消息管理器 // 键盘和鼠标消息管理器
class InputManager class InputManager
{ {
friend Game;
friend Input; friend Input;
friend InputListener; friend InputListener;
@ -357,12 +371,16 @@ private:
// 更新监听器 // 更新监听器
static void __update(); static void __update();
// 回收资源
static void __uninit();
}; };
// 碰撞管理器 // 碰撞管理器
class ColliderManager class ColliderManager
{ {
friend Game;
friend Node; friend Node;
friend Collider; friend Collider;
friend CollisionListener; friend CollisionListener;
@ -450,6 +468,9 @@ private:
static void __removeCollider( static void __removeCollider(
Collider * pCollider Collider * pCollider
); );
// 回收资源
static void __uninit();
}; };
} }

View File

@ -173,12 +173,17 @@ public:
double interval = 0, /* 时间间隔(秒) */ double interval = 0, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */ int times = -1, /* 执行次数(设 -1 为永久执行) */
bool atOnce = false, /* 是否立即执行 */ bool atOnce = false, /* 是否立即执行 */
bool autoRelease = false /* 自动清除 */ bool autoRelease = false /* 执行结束时自动清除 */
); );
// 启动定时器 // 启动定时器
void start(); void start();
// 启动定时器,并执行指定次数
void start(
int times /* 执行次数(设 -1 为永久执行) */
);
// 停止定时器 // 停止定时器
void stop(); void stop();