对监听器、定时器和动画加入了等待和唤醒机制

This commit is contained in:
Nomango 2017-10-08 12:30:45 +08:00
parent 64aa61a554
commit 976d134030
18 changed files with 223 additions and 153 deletions

View File

@ -3,7 +3,8 @@
Action::Action() : Action::Action() :
m_bRunning(true), m_bRunning(true),
m_bStop(false), m_bWaiting(false),
m_bEnding(false),
m_pTargetSprite(nullptr), m_pTargetSprite(nullptr),
m_pParentScene(nullptr) m_pParentScene(nullptr)
{ {
@ -17,7 +18,12 @@ Action::~Action()
bool Action::isRunning() bool Action::isRunning()
{ {
return m_bRunning; return m_bRunning && !m_bWaiting;
}
bool Action::isEnding()
{
return m_bEnding;
} }
void Action::start() void Action::start()
@ -37,7 +43,16 @@ void Action::pause()
void Action::stop() void Action::stop()
{ {
m_bStop = true; m_bEnding = true;
}
void Action::wait()
{
m_bWaiting = true;
}
void Action::notify()
{
m_bWaiting = false;
} }
void Action::setInterval(UINT ms) void Action::setInterval(UINT ms)
@ -55,3 +70,8 @@ Action * Action::reverse() const
assert(0); assert(0);
return nullptr; return nullptr;
} }
void Action::_reset()
{
m_bEnding = false;
}

View File

@ -18,15 +18,13 @@ void ActionCallback::_init()
{ {
} }
bool ActionCallback::_exec(LARGE_INTEGER nNow) void ActionCallback::_exec(LARGE_INTEGER nNow)
{
if (!m_bStop)
{ {
m_Callback(); m_Callback();
} this->stop();
return true;
} }
void ActionCallback::_reset() void ActionCallback::_reset()
{ {
Action::_reset();
} }

View File

@ -20,21 +20,18 @@ void ActionDelay::_init()
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);
} }
bool ActionDelay::_exec(LARGE_INTEGER nNow) void ActionDelay::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true;
if (!m_bRunning) return false;
// 判断时间间隔是否足够 // 判断时间间隔是否足够
if (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart) if (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart)
{ {
return true; this->stop();
} }
return false;
} }
void ActionDelay::_reset() void ActionDelay::_reset()
{ {
Action::_reset();
// 重新记录当前时间 // 重新记录当前时间
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);
} }

View File

@ -27,11 +27,8 @@ void ActionFrames::_init()
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);
} }
bool ActionFrames::_exec(LARGE_INTEGER nNow) void ActionFrames::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true;
if (!m_bRunning) return false;
// 判断时间间隔是否足够 // 判断时间间隔是否足够
while (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart) while (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart)
{ {
@ -42,14 +39,14 @@ bool ActionFrames::_exec(LARGE_INTEGER nNow)
// 判断动作是否结束 // 判断动作是否结束
if (m_nFrameIndex == m_vFrames.size()) if (m_nFrameIndex == m_vFrames.size())
{ {
return true; this->stop();
} }
} }
return false;
} }
void ActionFrames::_reset() void ActionFrames::_reset()
{ {
Action::_reset();
m_nFrameIndex = 0; m_nFrameIndex = 0;
} }

View File

@ -16,12 +16,9 @@ void ActionMoveBy::_init()
m_BeginPos = m_pTargetSprite->getPos(); m_BeginPos = m_pTargetSprite->getPos();
} }
bool ActionMoveBy::_exec(LARGE_INTEGER nNow) void ActionMoveBy::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; if (Animation::_isDelayEnough(nNow))
if (!m_bRunning) return false;
while (Animation::_exec(nNow))
{ {
// 计算移动位置 // 计算移动位置
float scale = float(m_nDuration) / m_nTotalDuration; float scale = float(m_nDuration) / m_nTotalDuration;
@ -31,10 +28,9 @@ bool ActionMoveBy::_exec(LARGE_INTEGER nNow)
// 判断动作是否结束 // 判断动作是否结束
if (_isEnd()) if (_isEnd())
{ {
return true; this->stop();
} }
} }
return false;
} }
void ActionMoveBy::_reset() void ActionMoveBy::_reset()

View File

@ -22,20 +22,17 @@ void ActionNeverStop::_init()
m_Action->_init(); m_Action->_init();
} }
bool ActionNeverStop::_exec(LARGE_INTEGER nNow) void ActionNeverStop::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; m_Action->_exec(nNow);
if (!m_bRunning) return false;
if (m_Action->_exec(nNow)) if (m_Action->isEnding())
{ {
m_Action->_reset(); m_Action->_reset();
} }
// 永不结束
return false;
} }
void ActionNeverStop::_reset() void ActionNeverStop::_reset()
{ {
m_Action->_reset(); Action::_reset();
} }

View File

@ -16,12 +16,9 @@ void ActionOpacityBy::_init()
m_nBeginVal = m_pTargetSprite->getOpacity(); m_nBeginVal = m_pTargetSprite->getOpacity();
} }
bool ActionOpacityBy::_exec(LARGE_INTEGER nNow) void ActionOpacityBy::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; if (Animation::_isDelayEnough(nNow))
if (!m_bRunning) return false;
while (Animation::_exec(nNow))
{ {
// 计算移动位置 // 计算移动位置
float scale = float(m_nDuration) / m_nTotalDuration; float scale = float(m_nDuration) / m_nTotalDuration;
@ -30,10 +27,9 @@ bool ActionOpacityBy::_exec(LARGE_INTEGER nNow)
// 判断动作是否结束 // 判断动作是否结束
if (_isEnd()) if (_isEnd())
{ {
return true; this->stop();
} }
} }
return false;
} }
void ActionOpacityBy::_reset() void ActionOpacityBy::_reset()

View File

@ -18,12 +18,9 @@ void ActionScaleBy::_init()
m_nBeginScaleY = m_pTargetSprite->getScaleY(); m_nBeginScaleY = m_pTargetSprite->getScaleY();
} }
bool ActionScaleBy::_exec(LARGE_INTEGER nNow) void ActionScaleBy::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; if (Animation::_isDelayEnough(nNow))
if (!m_bRunning) return false;
while (Animation::_exec(nNow))
{ {
// 计算移动位置 // 计算移动位置
float scale = float(m_nDuration) / m_nTotalDuration; float scale = float(m_nDuration) / m_nTotalDuration;
@ -32,10 +29,9 @@ bool ActionScaleBy::_exec(LARGE_INTEGER nNow)
// 判断动作是否结束 // 判断动作是否结束
if (_isEnd()) if (_isEnd())
{ {
return true; this->stop();
} }
} }
return false;
} }
void ActionScaleBy::_reset() void ActionScaleBy::_reset()

View File

@ -38,28 +38,27 @@ void ActionSequence::_init()
m_vActions[0]->_init(); m_vActions[0]->_init();
} }
bool ActionSequence::_exec(LARGE_INTEGER nNow) void ActionSequence::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; m_vActions[m_nActionIndex]->_exec(nNow);
if (!m_bRunning) return false;
if (m_vActions[m_nActionIndex]->_exec(nNow)) if (m_vActions[m_nActionIndex]->isEnding())
{ {
m_nActionIndex++; m_nActionIndex++;
if (m_nActionIndex == m_vActions.size()) if (m_nActionIndex == m_vActions.size())
{ {
return true; this->stop();
} }
else else
{ {
m_vActions[m_nActionIndex]->_init(); m_vActions[m_nActionIndex]->_init();
} }
} }
return false;
} }
void ActionSequence::_reset() void ActionSequence::_reset()
{ {
Action::_reset();
for (auto action : m_vActions) for (auto action : m_vActions)
{ {
action->_reset(); action->_reset();

View File

@ -2,8 +2,7 @@
ActionTwo::ActionTwo(Action * actionFirst, Action * actionSecond) : ActionTwo::ActionTwo(Action * actionFirst, Action * actionSecond) :
m_FirstAction(actionFirst), m_FirstAction(actionFirst),
m_SecondAction(actionSecond), m_SecondAction(actionSecond)
m_bFirstFinished(false)
{ {
m_FirstAction->retain(); m_FirstAction->retain();
m_SecondAction->retain(); m_SecondAction->retain();
@ -40,32 +39,33 @@ void ActionTwo::_init()
m_FirstAction->_init(); m_FirstAction->_init();
} }
bool ActionTwo::_exec(LARGE_INTEGER nNow) void ActionTwo::_exec(LARGE_INTEGER nNow)
{ {
if (m_bStop) return true; if (!m_FirstAction->isEnding())
if (!m_bRunning) return false;
if (!m_bFirstFinished)
{ {
if (m_FirstAction->_exec(nNow)) m_FirstAction->_exec(nNow);
if (m_FirstAction->isEnding())
{ {
// 返回 true 表示第一个动作已经结束 // 返回 true 表示第一个动作已经结束
m_SecondAction->_init(); m_SecondAction->_init();
m_bFirstFinished = true;
} }
} }
else if (m_SecondAction->_exec(nNow)) else if (!m_SecondAction->isEnding())
{ {
return true; m_SecondAction->_exec(nNow);
}
else
{
this->stop();
} }
return false;
} }
void ActionTwo::_reset() void ActionTwo::_reset()
{ {
Action::_reset();
m_FirstAction->_reset(); m_FirstAction->_reset();
m_SecondAction->_reset(); m_SecondAction->_reset();
m_FirstAction->_init(); m_FirstAction->_init();
m_bFirstFinished = false;
} }

View File

@ -21,7 +21,7 @@ void Animation::_init()
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);
} }
bool Animation::_exec(LARGE_INTEGER nNow) bool Animation::_isDelayEnough(LARGE_INTEGER nNow)
{ {
// 判断时间间隔是否足够 // 判断时间间隔是否足够
if (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart) if (nNow.QuadPart - m_nLast.QuadPart > m_nAnimationInterval.QuadPart)
@ -36,6 +36,7 @@ bool Animation::_exec(LARGE_INTEGER nNow)
void Animation::_reset() void Animation::_reset()
{ {
Action::_reset();
m_nDuration = 0; m_nDuration = 0;
// 重新记录当前时间 // 重新记录当前时间
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);

View File

@ -16,6 +16,7 @@ static int originY = 0;
App::App() : App::App() :
m_pCurrentScene(nullptr), m_pCurrentScene(nullptr),
m_pNextScene(nullptr), m_pNextScene(nullptr),
m_pLoadingScene(nullptr),
m_bRunning(false), m_bRunning(false),
m_nWindowMode(0), m_nWindowMode(0),
m_bSaveScene(true) m_bSaveScene(true)
@ -302,10 +303,10 @@ void App::_enterNextScene()
// 若要保存当前场景,把它放入栈中 // 若要保存当前场景,把它放入栈中
m_SceneStack.push(m_pCurrentScene); m_SceneStack.push(m_pCurrentScene);
// 暂停当前场景上运行的所有定时器 // 暂停当前场景上运行的所有定时器
Timer::stopAllSceneTimers(m_pCurrentScene); Timer::waitAllSceneTimers(m_pCurrentScene);
MouseMsg::stopAllSceneListeners(m_pCurrentScene); MouseMsg::waitAllSceneListeners(m_pCurrentScene);
KeyMsg::stopAllSceneListeners(m_pCurrentScene); KeyMsg::waitAllSceneListeners(m_pCurrentScene);
ActionManager::pauseAllSceneActions(m_pCurrentScene); ActionManager::waitAllSceneActions(m_pCurrentScene);
} }
else else
{ {
@ -324,10 +325,10 @@ void App::_enterNextScene()
if (bBackScene) if (bBackScene)
{ {
// 返回上一场景时,恢复场景上的定时器 // 返回上一场景时,恢复场景上的定时器
Timer::startAllSceneTimers(m_pCurrentScene); Timer::notifyAllSceneTimers(m_pCurrentScene);
MouseMsg::startAllSceneListeners(m_pCurrentScene); MouseMsg::notifyAllSceneListeners(m_pCurrentScene);
KeyMsg::startAllSceneListeners(m_pCurrentScene); KeyMsg::notifyAllSceneListeners(m_pCurrentScene);
ActionManager::startAllSceneActions(m_pCurrentScene); ActionManager::notifyAllSceneActions(m_pCurrentScene);
} }
else else
{ {
@ -358,14 +359,12 @@ void App::reset()
Scene * App::getCurrentScene() Scene * App::getCurrentScene()
{ {
// 获取当前场景的指针 // 获取当前场景的指针
if (s_pInstance->m_pCurrentScene)
{
return s_pInstance->m_pCurrentScene; return s_pInstance->m_pCurrentScene;
} }
else
Scene * App::getLoadingScene()
{ {
return s_pInstance->m_pNextScene; return s_pInstance->m_pLoadingScene;
}
} }
void App::setFPS(DWORD fps) void App::setFPS(DWORD fps)

View File

@ -3,6 +3,7 @@
Scene::Scene() Scene::Scene()
{ {
App::get()->m_pLoadingScene = this;
} }
Scene::~Scene() Scene::~Scene()

View File

@ -84,7 +84,8 @@ KeyMsg::KeyMsg(TString name, const KEY_CALLBACK & callback) :
m_sName(name), m_sName(name),
m_callback(callback), m_callback(callback),
m_pParentScene(nullptr), m_pParentScene(nullptr),
m_bRunning(true) m_bRunning(true),
m_bWaiting(false)
{ {
} }
@ -107,6 +108,16 @@ void KeyMsg::stop()
m_bRunning = false; m_bRunning = false;
} }
void KeyMsg::wait()
{
m_bWaiting = true;
}
void KeyMsg::notify()
{
m_bWaiting = false;
}
void KeyMsg::__exec() void KeyMsg::__exec()
{ {
if (_kbhit()) // 检测有无按键消息 if (_kbhit()) // 检测有无按键消息
@ -115,7 +126,7 @@ void KeyMsg::__exec()
for (auto l : s_vListeners) // 分发该消息 for (auto l : s_vListeners) // 分发该消息
{ {
if (l->m_bRunning) if (!l->m_bWaiting && l->m_bRunning)
{ {
l->onKbHit(key); // 执行按键回调函数 l->onKbHit(key); // 执行按键回调函数
} }
@ -128,7 +139,7 @@ void KeyMsg::addListener(TString name, const KEY_CALLBACK & callback)
// 创建新的监听对象 // 创建新的监听对象
auto listener = new KeyMsg(name, callback); auto listener = new KeyMsg(name, callback);
// 绑定在场景上 // 绑定在场景上
listener->m_pParentScene = App::getCurrentScene(); listener->m_pParentScene = App::getLoadingScene();
// 添加新的按键回调函数 // 添加新的按键回调函数
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
} }
@ -138,7 +149,7 @@ void KeyMsg::startListener(TString name)
// 查找名称相同的监听器 // 查找名称相同的监听器
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_sName == name && l->m_pParentScene == App::getCurrentScene()) if (l->m_sName == name)
{ {
l->start(); l->start();
} }
@ -150,7 +161,7 @@ void KeyMsg::stopListener(TString name)
// 查找名称相同的监听器 // 查找名称相同的监听器
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_sName == name && l->m_pParentScene == App::getCurrentScene()) if (l->m_sName == name)
{ {
l->stop(); l->stop();
} }
@ -165,7 +176,7 @@ void KeyMsg::delListener(TString name)
for (iter = s_vListeners.begin(); iter != s_vListeners.end();) for (iter = s_vListeners.begin(); iter != s_vListeners.end();)
{ {
// 查找相同名称的监听器 // 查找相同名称的监听器
if ((*iter)->m_sName == name && (*iter)->m_pParentScene == App::getCurrentScene()) if ((*iter)->m_sName == name)
{ {
// 删除该定时器 // 删除该定时器
delete (*iter); delete (*iter);
@ -178,24 +189,24 @@ void KeyMsg::delListener(TString name)
} }
} }
void KeyMsg::startAllSceneListeners(Scene * scene) void KeyMsg::notifyAllSceneListeners(Scene * scene)
{ {
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_pParentScene == scene) if (l->m_pParentScene == scene)
{ {
l->start(); l->notify();
} }
} }
} }
void KeyMsg::stopAllSceneListeners(Scene * scene) void KeyMsg::waitAllSceneListeners(Scene * scene)
{ {
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_pParentScene == scene) if (l->m_pParentScene == scene)
{ {
l->stop(); l->wait();
} }
} }
} }

View File

@ -19,7 +19,7 @@ void MouseMsg::__exec()
// 执行鼠标监听回调函数 // 执行鼠标监听回调函数
for (auto l : s_vListeners) // 循环遍历所有的鼠标监听 for (auto l : s_vListeners) // 循环遍历所有的鼠标监听
{ {
if (l->m_bRunning) if (!l->m_bWaiting && l->m_bRunning)
{ {
l->onMouseMsg(); // 执行回调函数 l->onMouseMsg(); // 执行回调函数
} }
@ -30,7 +30,8 @@ void MouseMsg::__exec()
MouseMsg::MouseMsg() : MouseMsg::MouseMsg() :
m_callback([]() {}), m_callback([]() {}),
m_pParentScene(nullptr), m_pParentScene(nullptr),
m_bRunning(true) m_bRunning(true),
m_bWaiting(false)
{ {
} }
@ -38,7 +39,8 @@ MouseMsg::MouseMsg(TString name, const MOUSE_CALLBACK & callback) :
m_sName(name), m_sName(name),
m_callback(callback), m_callback(callback),
m_pParentScene(nullptr), m_pParentScene(nullptr),
m_bRunning(true) m_bRunning(true),
m_bWaiting(false)
{ {
} }
@ -56,7 +58,7 @@ void MouseMsg::addListener(TString name, const MOUSE_CALLBACK & callback)
// 创建新的监听对象 // 创建新的监听对象
auto listener = new MouseMsg(name, callback); auto listener = new MouseMsg(name, callback);
// 绑定在场景上 // 绑定在场景上
listener->m_pParentScene = App::getCurrentScene(); listener->m_pParentScene = App::getLoadingScene();
// 添加新的按键回调函数 // 添加新的按键回调函数
s_vListeners.push_back(listener); s_vListeners.push_back(listener);
} }
@ -66,7 +68,7 @@ void MouseMsg::startListener(TString name)
// 查找名称相同的监听器 // 查找名称相同的监听器
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_sName == name && l->m_pParentScene == App::getCurrentScene()) if (l->m_sName == name)
{ {
l->start(); l->start();
} }
@ -78,7 +80,7 @@ void MouseMsg::stopListener(TString name)
// 查找名称相同的监听器 // 查找名称相同的监听器
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_sName == name && l->m_pParentScene == App::getCurrentScene()) if (l->m_sName == name)
{ {
l->stop(); l->stop();
} }
@ -93,7 +95,7 @@ void MouseMsg::delListener(TString name)
for (iter = s_vListeners.begin(); iter != s_vListeners.end();) for (iter = s_vListeners.begin(); iter != s_vListeners.end();)
{ {
// 查找相同名称的监听器 // 查找相同名称的监听器
if ((*iter)->m_sName == name && (*iter)->m_pParentScene == App::getCurrentScene()) if ((*iter)->m_sName == name)
{ {
// 删除该定时器 // 删除该定时器
delete (*iter); delete (*iter);
@ -116,6 +118,16 @@ void MouseMsg::stop()
m_bRunning = false; m_bRunning = false;
} }
void MouseMsg::wait()
{
m_bWaiting = true;
}
void MouseMsg::notify()
{
m_bWaiting = false;
}
void MouseMsg::clearAllListeners() void MouseMsg::clearAllListeners()
{ {
// 删除所有监听器 // 删除所有监听器
@ -127,24 +139,24 @@ void MouseMsg::clearAllListeners()
s_vListeners.clear(); s_vListeners.clear();
} }
void MouseMsg::startAllSceneListeners(Scene * scene) void MouseMsg::notifyAllSceneListeners(Scene * scene)
{ {
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_pParentScene == scene) if (l->m_pParentScene == scene)
{ {
l->start(); l->notify();
} }
} }
} }
void MouseMsg::stopAllSceneListeners(Scene * scene) void MouseMsg::waitAllSceneListeners(Scene * scene)
{ {
for (auto l : s_vListeners) for (auto l : s_vListeners)
{ {
if (l->m_pParentScene == scene) if (l->m_pParentScene == scene)
{ {
l->stop(); l->wait();
} }
} }
} }

View File

@ -11,13 +11,19 @@ void ActionManager::__exec()
// 循环遍历所有正在运行的动作 // 循环遍历所有正在运行的动作
for (size_t i = 0; i < s_vActions.size(); i++) for (size_t i = 0; i < s_vActions.size(); i++)
{ {
auto a = s_vActions[i]; if (s_vActions[i]->isRunning())
if (a->isRunning() && a->_exec(nNow))
{ {
// _exec 返回 true 时说明动作已经结束 if (s_vActions[i]->isEnding())
a->release(); {
// 动作已经结束
s_vActions[i]->release();
s_vActions.erase(s_vActions.begin() + i); s_vActions.erase(s_vActions.begin() + i);
} }
else
{
s_vActions[i]->_exec(nNow);
}
}
} }
} }
@ -31,30 +37,30 @@ void ActionManager::addAction(Action * action)
assert(a != action); assert(a != action);
} }
#endif #endif
action->m_pParentScene = App::getCurrentScene(); action->m_pParentScene = App::getLoadingScene();
action->_init(); action->_init();
s_vActions.push_back(action); s_vActions.push_back(action);
} }
} }
void ActionManager::startAllSceneActions(Scene * scene) void ActionManager::notifyAllSceneActions(Scene * scene)
{ {
for (auto action : s_vActions) for (auto action : s_vActions)
{ {
if (action->m_pParentScene == scene) if (action->m_pParentScene == scene)
{ {
action->start(); action->notify();
} }
} }
} }
void ActionManager::pauseAllSceneActions(Scene * scene) void ActionManager::waitAllSceneActions(Scene * scene)
{ {
for (auto action : s_vActions) for (auto action : s_vActions)
{ {
if (action->m_pParentScene == scene) if (action->m_pParentScene == scene)
{ {
action->pause(); action->wait();
} }
} }
} }

View File

@ -6,6 +6,7 @@ static std::vector<Timer*> s_vTimers;
Timer::Timer(TString name, UINT ms, const TIMER_CALLBACK & callback) : Timer::Timer(TString name, UINT ms, const TIMER_CALLBACK & callback) :
m_sName(name), m_sName(name),
m_bRunning(false), m_bRunning(false),
m_bWaiting(false),
m_callback(callback), m_callback(callback),
m_pParentScene(nullptr) m_pParentScene(nullptr)
{ {
@ -19,19 +20,29 @@ Timer::~Timer()
void Timer::start() void Timer::start()
{ {
// 标志该定时器正在运行 // 标志该定时器正在运行
this->m_bRunning = true; m_bRunning = true;
// 记录当前时间 // 记录当前时间
QueryPerformanceCounter(&m_nLast); QueryPerformanceCounter(&m_nLast);
} }
void Timer::stop() void Timer::stop()
{ {
this->m_bRunning = false; // 标志该定时器已停止 m_bRunning = false; // 标志该定时器已停止
}
void Timer::wait()
{
m_bWaiting = true;
}
void Timer::notify()
{
m_bWaiting = false;
} }
bool Timer::isRunning() bool Timer::isRunning()
{ {
return m_bRunning; // 获取该定时器的运行状态 return m_bRunning && !m_bWaiting; // 获取该定时器的运行状态
} }
void Timer::setInterval(UINT ms) void Timer::setInterval(UINT ms)
@ -78,7 +89,7 @@ void Timer::__exec()
for (auto timer : s_vTimers) for (auto timer : s_vTimers)
{ {
// 若定时器未运行,跳过这个定时器 // 若定时器未运行,跳过这个定时器
if (!timer->m_bRunning) if (!timer->isRunning())
{ {
continue; continue;
} }
@ -98,7 +109,7 @@ void Timer::addTimer(Timer * timer)
// 启动定时器 // 启动定时器
timer->start(); timer->start();
// 绑定在场景上 // 绑定在场景上
timer->m_pParentScene = App::getCurrentScene(); timer->m_pParentScene = App::getLoadingScene();
// 将该定时器放入容器 // 将该定时器放入容器
s_vTimers.push_back(timer); s_vTimers.push_back(timer);
} }
@ -169,24 +180,24 @@ void Timer::clearAllTimers()
s_vTimers.clear(); s_vTimers.clear();
} }
void Timer::startAllSceneTimers(Scene * scene) void Timer::notifyAllSceneTimers(Scene * scene)
{ {
for (auto t : s_vTimers) for (auto t : s_vTimers)
{ {
if (t->m_pParentScene == scene) if (t->m_pParentScene == scene)
{ {
t->start(); t->notify();
} }
} }
} }
void Timer::stopAllSceneTimers(Scene * scene) void Timer::waitAllSceneTimers(Scene * scene)
{ {
for (auto t : s_vTimers) for (auto t : s_vTimers)
{ {
if (t->m_pParentScene == scene) if (t->m_pParentScene == scene)
{ {
t->stop(); t->wait();
} }
} }
} }

View File

@ -112,6 +112,7 @@ namespace easy2d
class App class App
{ {
friend Scene;
public: public:
App(); App();
~App(); ~App();
@ -176,12 +177,15 @@ public:
static void reset(); static void reset();
// 获取当前场景 // 获取当前场景
static Scene * getCurrentScene(); static Scene * getCurrentScene();
// 获取正处于加载中的场景
static Scene * getLoadingScene();
protected: protected:
TString m_sTitle; TString m_sTitle;
TString m_sAppName; TString m_sAppName;
Scene* m_pCurrentScene; Scene* m_pCurrentScene;
Scene* m_pNextScene; Scene* m_pNextScene;
Scene* m_pLoadingScene;
std::stack<Scene*> m_SceneStack; std::stack<Scene*> m_SceneStack;
LARGE_INTEGER m_nAnimationInterval; LARGE_INTEGER m_nAnimationInterval;
CSize m_Size; CSize m_Size;
@ -283,6 +287,10 @@ public:
void start(); void start();
// 停止监听 // 停止监听
void stop(); void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
// 左键是否按下 // 左键是否按下
static bool isLButtonDown(); static bool isLButtonDown();
@ -312,9 +320,9 @@ public:
// 删除所有鼠标消息监听 // 删除所有鼠标消息监听
static void clearAllListeners(); static void clearAllListeners();
// 启动绑定在场景上的所有监听器 // 启动绑定在场景上的所有监听器
static void startAllSceneListeners(Scene* scene); static void notifyAllSceneListeners(Scene* scene);
// 停止绑定在场景上的所有监听器 // 停止绑定在场景上的所有监听器
static void stopAllSceneListeners(Scene* scene); static void waitAllSceneListeners(Scene* scene);
// 清除绑定在场景上的所有监听器 // 清除绑定在场景上的所有监听器
static void clearAllSceneListeners(Scene* scene); static void clearAllSceneListeners(Scene* scene);
@ -323,6 +331,7 @@ private:
protected: protected:
bool m_bRunning; bool m_bRunning;
bool m_bWaiting;
TString m_sName; TString m_sName;
MOUSE_CALLBACK m_callback; MOUSE_CALLBACK m_callback;
Scene * m_pParentScene; Scene * m_pParentScene;
@ -345,6 +354,10 @@ public:
void start(); void start();
// 停止监听 // 停止监听
void stop(); void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
// 判断键是否被按下按下返回true // 判断键是否被按下按下返回true
static bool isKeyDown(VK_KEY key); static bool isKeyDown(VK_KEY key);
@ -357,9 +370,9 @@ public:
// 删除按键监听 // 删除按键监听
static void KeyMsg::delListener(TString name); static void KeyMsg::delListener(TString name);
// 启动绑定在场景上的所有监听器 // 启动绑定在场景上的所有监听器
static void startAllSceneListeners(Scene* scene); static void notifyAllSceneListeners(Scene* scene);
// 停止绑定在场景上的所有监听器 // 停止绑定在场景上的所有监听器
static void stopAllSceneListeners(Scene* scene); static void waitAllSceneListeners(Scene* scene);
// 停止绑定在场景上的所有定时器 // 停止绑定在场景上的所有定时器
static void clearAllSceneListeners(Scene* scene); static void clearAllSceneListeners(Scene* scene);
// 删除所有按键监听 // 删除所有按键监听
@ -382,6 +395,7 @@ private:
protected: protected:
bool m_bRunning; bool m_bRunning;
bool m_bWaiting;
TString m_sName; TString m_sName;
KEY_CALLBACK m_callback; KEY_CALLBACK m_callback;
Scene * m_pParentScene; Scene * m_pParentScene;
@ -1060,18 +1074,33 @@ public:
Action(); Action();
virtual ~Action(); virtual ~Action();
bool isRunning(); // 获取动作运行状态
void start(); virtual bool isRunning();
void resume(); // 获取动作结束状态
void pause(); virtual bool isEnding();
void stop(); // 继续动作
void setInterval(UINT ms); virtual void start();
// 继续动作
virtual void resume();
// 暂停动作
virtual void pause();
// 停止动作
virtual void stop();
// 进入等待状态
virtual void wait();
// 唤醒
virtual void notify();
// 设置动作每一帧时间间隔
virtual void setInterval(UINT ms);
// 获取一个新的拷贝动作
virtual Action * copy() const = 0; virtual Action * copy() const = 0;
// 获取一个新的逆向动作
virtual Action * reverse() const; virtual Action * reverse() const;
protected: protected:
bool m_bRunning; bool m_bRunning;
bool m_bStop; bool m_bWaiting;
bool m_bEnding;
Sprite * m_pTargetSprite; Sprite * m_pTargetSprite;
Scene * m_pParentScene; Scene * m_pParentScene;
UINT m_nMilliSeconds; UINT m_nMilliSeconds;
@ -1080,8 +1109,8 @@ protected:
protected: protected:
virtual void _init() = 0; virtual void _init() = 0;
virtual bool _exec(LARGE_INTEGER nNow) = 0; virtual void _exec(LARGE_INTEGER nNow) = 0;
virtual void _reset() = 0; virtual void _reset();
}; };
class Animation : class Animation :
@ -1097,8 +1126,8 @@ protected:
protected: protected:
bool _isEnd() const; bool _isEnd() const;
bool _isDelayEnough(LARGE_INTEGER nNow);
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1118,7 +1147,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1157,7 +1186,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1195,7 +1224,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1243,11 +1272,10 @@ public:
protected: protected:
Action * m_FirstAction; Action * m_FirstAction;
Action * m_SecondAction; Action * m_SecondAction;
bool m_bFirstFinished;
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1269,7 +1297,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1284,7 +1312,7 @@ public:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1302,7 +1330,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1324,7 +1352,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1342,7 +1370,7 @@ protected:
protected: protected:
virtual void _init() override; virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override; virtual void _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override; virtual void _reset() override;
}; };
@ -1430,6 +1458,10 @@ public:
void start(); void start();
// 停止定时器 // 停止定时器
void stop(); void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
// 定时器是否正在运行 // 定时器是否正在运行
bool isRunning(); bool isRunning();
// 设置间隔时间 // 设置间隔时间
@ -1457,14 +1489,15 @@ public:
static void clearAllTimers(); static void clearAllTimers();
// 继续绑定在场景上的所有定时器 // 继续绑定在场景上的所有定时器
static void startAllSceneTimers(Scene* scene); static void notifyAllSceneTimers(Scene* scene);
// 停止绑定在场景上的所有定时器 // 停止绑定在场景上的所有定时器
static void stopAllSceneTimers(Scene* scene); static void waitAllSceneTimers(Scene* scene);
// 清除绑定在场景上的所有定时器 // 清除绑定在场景上的所有定时器
static void clearAllSceneTimers(Scene* scene); static void clearAllSceneTimers(Scene* scene);
protected: protected:
bool m_bRunning; bool m_bRunning;
bool m_bWaiting;
TString m_sName; TString m_sName;
TIMER_CALLBACK m_callback; TIMER_CALLBACK m_callback;
LARGE_INTEGER m_nLast; LARGE_INTEGER m_nLast;
@ -1511,9 +1544,9 @@ public:
static void clearAllActions(); static void clearAllActions();
// 继续绑定在场景上的动作 // 继续绑定在场景上的动作
static void startAllSceneActions(Scene* scene); static void notifyAllSceneActions(Scene* scene);
// 暂停绑定在场景上的动作 // 暂停绑定在场景上的动作
static void pauseAllSceneActions(Scene* scene); static void waitAllSceneActions(Scene* scene);
// 停止绑定在场景上的动作 // 停止绑定在场景上的动作
static void stopAllSceneActions(Scene* scene); static void stopAllSceneActions(Scene* scene);