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;
}
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();
// 销毁窗口

View File

@ -23,7 +23,7 @@ void e2d::Object::release()
{
m_nRefCount--;
// 通知对象管理池刷新
ObjectManager::clear();
ObjectManager::flush();
}
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()
{
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)
{
WARN_IF(pListener == nullptr, "InputListener NULL pointer exception!");

View File

@ -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;
}

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)
{
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)

View File

@ -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)

View File

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

View File

@ -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();
};
}

View File

@ -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();