重做Timer单例模式,增加Task定时任务类
This commit is contained in:
parent
95a811cc17
commit
2d2c70c05d
|
|
@ -12,6 +12,7 @@ e2d::GC::~GC()
|
|||
Renderer::destroyInstance();
|
||||
Input::destroyInstance();
|
||||
Window::destroyInstance();
|
||||
Timer::destroyInstance();
|
||||
Player::destroyInstance();
|
||||
SceneManager::destroyInstance();
|
||||
ActionManager::destroyInstance();
|
||||
|
|
@ -24,7 +25,7 @@ e2d::GC::~GC()
|
|||
// 它记录了对象被使用的次数,当计数为 0 时,GC 会自动释放这个对象
|
||||
// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中)
|
||||
// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数
|
||||
void e2d::GC::__update()
|
||||
void e2d::GC::update()
|
||||
{
|
||||
if (!_notifyed) return;
|
||||
|
||||
|
|
@ -53,11 +54,11 @@ void e2d::GC::clear()
|
|||
_pool.clear();
|
||||
}
|
||||
|
||||
void e2d::GC::addObject(e2d::Object * pObject)
|
||||
void e2d::GC::addObject(e2d::Object * object)
|
||||
{
|
||||
if (pObject)
|
||||
if (object)
|
||||
{
|
||||
_pool.insert(pObject);
|
||||
_pool.insert(object);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,9 +71,3 @@ void e2d::GC::notify()
|
|||
{
|
||||
_notifyed = true;
|
||||
}
|
||||
|
||||
void e2d::GC::flush()
|
||||
{
|
||||
GC::notify();
|
||||
GC::__update();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,15 +43,16 @@ void e2d::Game::start(bool cleanup)
|
|||
auto input = Input::getInstance();
|
||||
auto window = Window::getInstance();
|
||||
auto renderer = Renderer::getInstance();
|
||||
auto actionManager = ActionManager::getInstance();
|
||||
auto timer = Timer::getInstance();
|
||||
auto sceneManager = SceneManager::getInstance();
|
||||
auto actionManager = ActionManager::getInstance();
|
||||
|
||||
// 显示窗口
|
||||
::ShowWindow(window->getHWnd(), SW_SHOWNORMAL);
|
||||
// 刷新窗口内容
|
||||
::UpdateWindow(window->getHWnd());
|
||||
// 处理窗口消息
|
||||
window->__poll();
|
||||
window->poll();
|
||||
// 初始化计时
|
||||
Time::__init();
|
||||
|
||||
|
|
@ -60,7 +61,7 @@ void e2d::Game::start(bool cleanup)
|
|||
while (!_ended)
|
||||
{
|
||||
// 处理窗口消息
|
||||
window->__poll();
|
||||
window->poll();
|
||||
// 刷新时间
|
||||
Time::__updateNow();
|
||||
|
||||
|
|
@ -73,18 +74,18 @@ void e2d::Game::start(bool cleanup)
|
|||
_config->_update();
|
||||
}
|
||||
|
||||
input->__update(); // 获取用户输入
|
||||
Timer::__update(); // 更新定时器
|
||||
actionManager->__update(); // 更新动作管理器
|
||||
sceneManager->__update(); // 更新场景内容
|
||||
renderer->__render(); // 渲染游戏画面
|
||||
input->update(); // 获取用户输入
|
||||
timer->update(); // 更新定时器
|
||||
actionManager->update(); // 更新动作管理器
|
||||
sceneManager->update(); // 更新场景内容
|
||||
renderer->render(); // 渲染游戏画面
|
||||
|
||||
Time::__updateLast(); // 刷新时间信息
|
||||
}
|
||||
else
|
||||
{
|
||||
Time::__sleep(); // 挂起线程
|
||||
gc->__update(); // 刷新内存池
|
||||
gc->update(); // 刷新内存池
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,8 +107,8 @@ void e2d::Game::resume()
|
|||
if (_paused && !_ended)
|
||||
{
|
||||
Time::__reset();
|
||||
Timer::__resetAll();
|
||||
ActionManager::getInstance()->__resetAll();
|
||||
Timer::getInstance()->updateTime();
|
||||
ActionManager::getInstance()->updateTime();
|
||||
}
|
||||
_paused = false;
|
||||
}
|
||||
|
|
@ -147,14 +148,20 @@ void e2d::Game::cleanup()
|
|||
{
|
||||
// 删除所有场景
|
||||
SceneManager::getInstance()->clear();
|
||||
// 清空定时器
|
||||
Timer::getInstance()->clearAllTasks();
|
||||
// 清除所有动作
|
||||
ActionManager::getInstance()->clearAll();
|
||||
// 清除所有碰撞体
|
||||
ColliderManager::getInstance()->clearAll();
|
||||
// 删除碰撞监听器
|
||||
Collision::removeAllListeners();
|
||||
Collision::clearAllListeners();
|
||||
// 删除输入监听器
|
||||
Input::removeAllListeners();
|
||||
Input::clearAllListeners();
|
||||
// 清空图片缓存
|
||||
Image::clearCache();
|
||||
// 清空定时器
|
||||
Timer::removeAll();
|
||||
// 清空音乐缓存
|
||||
Player::getInstance()->clearCache();
|
||||
// 删除所有对象
|
||||
GC::getInstance()->clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ void e2d::Input::destroyInstance()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::Input::__update()
|
||||
void e2d::Input::update()
|
||||
{
|
||||
Input::__updateDeviceState();
|
||||
Input::__updateListeners();
|
||||
|
|
@ -330,7 +330,15 @@ void e2d::Input::removeAllListeners()
|
|||
{
|
||||
for (auto listener : s_vListeners)
|
||||
{
|
||||
GC::release(listener);
|
||||
listener->_stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Input::clearAllListeners()
|
||||
{
|
||||
for (auto listener : s_vListeners)
|
||||
{
|
||||
listener->release();
|
||||
}
|
||||
s_vListeners.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ void e2d::Renderer::__discardDeviceResources()
|
|||
SafeRelease(_roundStrokeStyle);
|
||||
}
|
||||
|
||||
void e2d::Renderer::__render()
|
||||
void e2d::Renderer::render()
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ void e2d::Renderer::__render()
|
|||
_renderTarget->Clear(_clearColor);
|
||||
|
||||
// äÖȾ³¡¾°
|
||||
SceneManager::getInstance()->__render();
|
||||
SceneManager::getInstance()->render();
|
||||
|
||||
// äÖȾ FPS
|
||||
if (_showFps)
|
||||
|
|
|
|||
|
|
@ -172,14 +172,12 @@ HWND e2d::Window::__create()
|
|||
return hWnd;
|
||||
}
|
||||
|
||||
void e2d::Window::__poll()
|
||||
void e2d::Window::poll()
|
||||
{
|
||||
static MSG msg;
|
||||
|
||||
while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
|
||||
while (::PeekMessage(&_msg, nullptr, 0, 0, PM_REMOVE))
|
||||
{
|
||||
::TranslateMessage(&msg);
|
||||
::DispatchMessage(&msg);
|
||||
::TranslateMessage(&_msg);
|
||||
::DispatchMessage(&_msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -429,7 +427,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
|
|||
// ÖØ»æ´°¿Ú
|
||||
case WM_PAINT:
|
||||
{
|
||||
e2d::Renderer::getInstance()->__render();
|
||||
e2d::Renderer::getInstance()->render();
|
||||
ValidateRect(hWnd, nullptr);
|
||||
}
|
||||
result = 0;
|
||||
|
|
|
|||
|
|
@ -215,7 +215,15 @@ void e2d::Collision::removeAllListeners()
|
|||
{
|
||||
for (auto listener : s_vListeners)
|
||||
{
|
||||
GC::release(listener);
|
||||
listener->_stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Collision::clearAllListeners()
|
||||
{
|
||||
for (auto listener : s_vListeners)
|
||||
{
|
||||
listener->release();
|
||||
}
|
||||
s_vListeners.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,11 +29,9 @@ e2d::ActionManager::ActionManager()
|
|||
|
||||
e2d::ActionManager::~ActionManager()
|
||||
{
|
||||
_actions.clear();
|
||||
_runningActions.clear();
|
||||
}
|
||||
|
||||
void e2d::ActionManager::__update()
|
||||
void e2d::ActionManager::update()
|
||||
{
|
||||
if (_runningActions.empty() || Game::getInstance()->isPaused())
|
||||
return;
|
||||
|
|
@ -213,40 +211,14 @@ void e2d::ActionManager::clearAllBindedWith(Node * target)
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::ActionManager::resumeAll()
|
||||
void e2d::ActionManager::clearAll()
|
||||
{
|
||||
auto scene = SceneManager::getInstance()->getCurrentScene();
|
||||
if (scene)
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
for (auto child : scene->getRoot()->getAllChildren())
|
||||
{
|
||||
ActionManager::resumeAllBindedWith(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::ActionManager::pauseAll()
|
||||
{
|
||||
auto scene = SceneManager::getInstance()->getCurrentScene();
|
||||
if (scene)
|
||||
{
|
||||
for (auto child : scene->getRoot()->getAllChildren())
|
||||
{
|
||||
ActionManager::pauseAllBindedWith(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::ActionManager::stopAll()
|
||||
{
|
||||
auto scene = SceneManager::getInstance()->getCurrentScene();
|
||||
if (scene)
|
||||
{
|
||||
for (auto child : scene->getRoot()->getAllChildren())
|
||||
{
|
||||
ActionManager::stopAllBindedWith(child);
|
||||
}
|
||||
action->release();
|
||||
}
|
||||
_actions.clear();
|
||||
_runningActions.clear();
|
||||
}
|
||||
|
||||
std::vector<e2d::Action*> e2d::ActionManager::get(const String& name)
|
||||
|
|
@ -267,7 +239,7 @@ const std::vector<e2d::Action*>& e2d::ActionManager::getAll()
|
|||
return _actions;
|
||||
}
|
||||
|
||||
void e2d::ActionManager::__resetAll()
|
||||
void e2d::ActionManager::updateTime()
|
||||
{
|
||||
for (auto action : _runningActions)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -31,6 +31,15 @@ e2d::ColliderManager::~ColliderManager()
|
|||
{
|
||||
}
|
||||
|
||||
void e2d::ColliderManager::clearAll()
|
||||
{
|
||||
for (auto collder : _colliders)
|
||||
{
|
||||
collder->release();
|
||||
}
|
||||
_colliders.clear();
|
||||
}
|
||||
|
||||
void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
|
||||
{
|
||||
// 判断碰撞监听是否打开
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ bool e2d::SceneManager::isTransitioning()
|
|||
return _transition != nullptr;
|
||||
}
|
||||
|
||||
void e2d::SceneManager::__update()
|
||||
void e2d::SceneManager::update()
|
||||
{
|
||||
if (_transition == nullptr)
|
||||
{
|
||||
|
|
@ -168,7 +168,7 @@ void e2d::SceneManager::__update()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::SceneManager::__render()
|
||||
void e2d::SceneManager::render()
|
||||
{
|
||||
if (_transition)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -499,7 +499,7 @@ bool e2d::Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wcha
|
|||
if (nullptr == strFilename || nullptr == strDestPath || cchDest < 10)
|
||||
return false;
|
||||
|
||||
// Get the exe name, and exe path
|
||||
// Get the exe _name, and exe path
|
||||
wchar_t strExePath[MAX_PATH] = { 0 };
|
||||
wchar_t strExeName[MAX_PATH] = { 0 };
|
||||
wchar_t* strLastSlash = nullptr;
|
||||
|
|
@ -510,10 +510,10 @@ bool e2d::Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wcha
|
|||
{
|
||||
wcscpy_s(strExeName, MAX_PATH, &strLastSlash[1]);
|
||||
|
||||
// Chop the exe name from the exe path
|
||||
// Chop the exe _name from the exe path
|
||||
*strLastSlash = 0;
|
||||
|
||||
// Chop the .exe from the exe name
|
||||
// Chop the .exe from the exe _name
|
||||
strLastSlash = wcsrchr(strExeName, TEXT('.'));
|
||||
if (strLastSlash)
|
||||
*strLastSlash = 0;
|
||||
|
|
@ -523,7 +523,7 @@ bool e2d::Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wcha
|
|||
if (GetFileAttributes(strDestPath) != 0xFFFFFFFF)
|
||||
return true;
|
||||
|
||||
// Search all parent directories starting at .\ and using strFilename as the leaf name
|
||||
// Search all parent directories starting at .\ and using strFilename as the leaf _name
|
||||
wchar_t strLeafName[MAX_PATH] = { 0 };
|
||||
wcscpy_s(strLeafName, MAX_PATH, strFilename);
|
||||
|
||||
|
|
|
|||
|
|
@ -221,6 +221,11 @@ void e2d::Player::setVolume(double volume)
|
|||
{
|
||||
pair.second->setVolume(_volume);
|
||||
}
|
||||
|
||||
for (auto pair : _resList)
|
||||
{
|
||||
pair.second->setVolume(_volume);
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Player::pauseAll()
|
||||
|
|
@ -229,6 +234,11 @@ void e2d::Player::pauseAll()
|
|||
{
|
||||
pair.second->pause();
|
||||
}
|
||||
|
||||
for (auto pair : _resList)
|
||||
{
|
||||
pair.second->pause();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Player::resumeAll()
|
||||
|
|
@ -237,6 +247,11 @@ void e2d::Player::resumeAll()
|
|||
{
|
||||
pair.second->resume();
|
||||
}
|
||||
|
||||
for (auto pair : _resList)
|
||||
{
|
||||
pair.second->resume();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Player::stopAll()
|
||||
|
|
@ -245,4 +260,24 @@ void e2d::Player::stopAll()
|
|||
{
|
||||
pair.second->stop();
|
||||
}
|
||||
|
||||
for (auto pair : _resList)
|
||||
{
|
||||
pair.second->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Player::clearCache()
|
||||
{
|
||||
for (auto pair : _fileList)
|
||||
{
|
||||
delete pair.second;
|
||||
}
|
||||
_fileList.clear();
|
||||
|
||||
for (auto pair : _resList)
|
||||
{
|
||||
delete pair.second;
|
||||
}
|
||||
_resList.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,84 @@
|
|||
#include "..\e2dtool.h"
|
||||
|
||||
|
||||
e2d::Task::Task(const Function & func, const String & name)
|
||||
: _running(true)
|
||||
, _stopped(false)
|
||||
, _runTimes(0)
|
||||
, _totalTimes(-1)
|
||||
, _delay(0.0)
|
||||
, _lastTime(0.0)
|
||||
, _callback(func)
|
||||
, _name(name)
|
||||
{
|
||||
}
|
||||
|
||||
e2d::Task::Task(const Function & func, double delay, int times, const String & name)
|
||||
: _running(true)
|
||||
, _stopped(false)
|
||||
, _runTimes(0)
|
||||
, _totalTimes(times)
|
||||
, _delay(std::max(delay, 0.0))
|
||||
, _lastTime(0.0)
|
||||
, _callback(func)
|
||||
, _name(name)
|
||||
{
|
||||
}
|
||||
|
||||
void e2d::Task::pause()
|
||||
{
|
||||
_running = false;
|
||||
}
|
||||
|
||||
void e2d::Task::resume()
|
||||
{
|
||||
_running = true;
|
||||
updateTime();
|
||||
}
|
||||
|
||||
void e2d::Task::update()
|
||||
{
|
||||
if (_callback)
|
||||
{
|
||||
_callback();
|
||||
}
|
||||
|
||||
++_runTimes;
|
||||
_lastTime += _delay;
|
||||
|
||||
if (_runTimes == _totalTimes)
|
||||
{
|
||||
_stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool e2d::Task::isReady() const
|
||||
{
|
||||
if (_running)
|
||||
{
|
||||
if (_delay == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ((Time::getTotalTime() - _lastTime) >= _delay)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool e2d::Task::isRunning() const
|
||||
{
|
||||
return _running;
|
||||
}
|
||||
|
||||
e2d::String e2d::Task::getName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
void e2d::Task::updateTime()
|
||||
{
|
||||
_lastTime = Time::getTotalTime();
|
||||
}
|
||||
|
|
@ -1,168 +1,132 @@
|
|||
#include "..\e2dtool.h"
|
||||
#include "..\e2dnode.h"
|
||||
|
||||
namespace e2d
|
||||
e2d::Timer * e2d::Timer::_instance = nullptr;
|
||||
|
||||
e2d::Timer * e2d::Timer::getInstance()
|
||||
{
|
||||
class TimerEntity
|
||||
{
|
||||
public:
|
||||
explicit TimerEntity(
|
||||
const e2d::Function& func,
|
||||
const e2d::String& name,
|
||||
double delay,
|
||||
int updateTimes,
|
||||
bool paused
|
||||
)
|
||||
: running(!paused)
|
||||
, stopped(false)
|
||||
, runTimes(0)
|
||||
, totalTimes(updateTimes)
|
||||
, delay(std::max(delay, 0.0))
|
||||
, lastTime(e2d::Time::getTotalTime())
|
||||
, callback(func)
|
||||
, name(name)
|
||||
{
|
||||
}
|
||||
|
||||
void update()
|
||||
{
|
||||
if (callback)
|
||||
{
|
||||
callback();
|
||||
}
|
||||
|
||||
++runTimes;
|
||||
lastTime += delay;
|
||||
|
||||
if (runTimes == totalTimes)
|
||||
{
|
||||
stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool ready()
|
||||
{
|
||||
if (this->running)
|
||||
{
|
||||
if (this->delay == 0)
|
||||
return true;
|
||||
|
||||
if ((e2d::Time::getTotalTime() - this->lastTime) >= this->delay)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public:
|
||||
bool running;
|
||||
bool stopped;
|
||||
int runTimes;
|
||||
int totalTimes;
|
||||
double delay;
|
||||
double lastTime;
|
||||
e2d::String name;
|
||||
e2d::Function callback;
|
||||
};
|
||||
if (!_instance)
|
||||
_instance = new (std::nothrow) Timer;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
static std::vector<e2d::TimerEntity*> s_vTimers;
|
||||
|
||||
|
||||
void e2d::Timer::add(const Function& func, double delay, int updateTimes, bool paused, const String& name)
|
||||
void e2d::Timer::destroyInstance()
|
||||
{
|
||||
auto timer = new (std::nothrow) TimerEntity(func, name, delay, updateTimes, paused);
|
||||
s_vTimers.push_back(timer);
|
||||
}
|
||||
|
||||
void e2d::Timer::add(const Function& func, const String& name)
|
||||
{
|
||||
Timer::add(func, 0, -1, false, name);
|
||||
}
|
||||
|
||||
void e2d::Timer::start(double timeout, const Function& func)
|
||||
{
|
||||
auto timer = new (std::nothrow) TimerEntity(func, L"", timeout, 1, false);
|
||||
s_vTimers.push_back(timer);
|
||||
}
|
||||
|
||||
void e2d::Timer::stop(const String& name)
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
if (_instance)
|
||||
{
|
||||
if (timer->name == name)
|
||||
delete _instance;
|
||||
_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
e2d::Timer::Timer()
|
||||
: _tasks()
|
||||
{
|
||||
}
|
||||
|
||||
e2d::Timer::~Timer()
|
||||
{
|
||||
}
|
||||
|
||||
void e2d::Timer::addTask(Task * task)
|
||||
{
|
||||
if (task)
|
||||
{
|
||||
timer->running = false;
|
||||
auto iter = std::find(_tasks.begin(), _tasks.end(), task);
|
||||
if (iter == _tasks.end())
|
||||
{
|
||||
task->retain();
|
||||
task->updateTime();
|
||||
_tasks.push_back(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::start(const String& name)
|
||||
void e2d::Timer::pauseTasks(const String& name)
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
if (timer->name == name)
|
||||
if (task->getName() == name)
|
||||
{
|
||||
timer->running = true;
|
||||
task->pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::remove(const String& name)
|
||||
void e2d::Timer::resumeTasks(const String& name)
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
if (timer->name == name)
|
||||
if (task->getName() == name)
|
||||
{
|
||||
timer->stopped = true;
|
||||
task->resume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::stopAll()
|
||||
void e2d::Timer::removeTasks(const String& name)
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
timer->running = false;
|
||||
if (task->getName() == name)
|
||||
{
|
||||
task->_stopped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::startAll()
|
||||
void e2d::Timer::pauseAllTasks()
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
timer->running = true;
|
||||
task->pause();
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::removeAll()
|
||||
void e2d::Timer::resumeAllTasks()
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
delete timer;
|
||||
task->resume();
|
||||
}
|
||||
s_vTimers.clear();
|
||||
}
|
||||
|
||||
void e2d::Timer::__update()
|
||||
void e2d::Timer::removeAllTasks()
|
||||
{
|
||||
if (s_vTimers.empty() || Game::getInstance()->isPaused())
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
task->_stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::clearAllTasks()
|
||||
{
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
task->release();
|
||||
}
|
||||
_tasks.clear();
|
||||
}
|
||||
|
||||
void e2d::Timer::update()
|
||||
{
|
||||
if (_tasks.empty() || Game::getInstance()->isPaused())
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < s_vTimers.size();)
|
||||
for (size_t i = 0; i < _tasks.size();)
|
||||
{
|
||||
auto timer = s_vTimers[i];
|
||||
// Çå³ýÒÑÍ£Ö¹µÄ¶¨Ê±Æ÷
|
||||
if (timer->stopped)
|
||||
auto task = _tasks[i];
|
||||
// Çå³ýÒÑÍ£Ö¹µÄÈÎÎñ
|
||||
if (task->_stopped)
|
||||
{
|
||||
delete timer;
|
||||
s_vTimers.erase(s_vTimers.begin() + i);
|
||||
task->release();
|
||||
_tasks.erase(_tasks.begin() + i);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 更新定时器
|
||||
if (timer->ready())
|
||||
if (task->isReady())
|
||||
{
|
||||
timer->update();
|
||||
task->update();
|
||||
}
|
||||
|
||||
++i;
|
||||
|
|
@ -170,10 +134,10 @@ void e2d::Timer::__update()
|
|||
}
|
||||
}
|
||||
|
||||
void e2d::Timer::__resetAll()
|
||||
void e2d::Timer::updateTime()
|
||||
{
|
||||
for (auto timer : s_vTimers)
|
||||
for (auto task : _tasks)
|
||||
{
|
||||
timer->lastTime = Time::getTotalTime();
|
||||
task->updateTime();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,8 +67,6 @@ private:
|
|||
// 窗口控制
|
||||
class Window
|
||||
{
|
||||
friend class Game;
|
||||
|
||||
public:
|
||||
// 鼠标指针样式
|
||||
enum class Cursor : int
|
||||
|
|
@ -156,6 +154,9 @@ public:
|
|||
const String& title = L"Error" /* 窗口标题 */
|
||||
);
|
||||
|
||||
// 处理窗口消息
|
||||
void poll();
|
||||
|
||||
private:
|
||||
Window();
|
||||
|
||||
|
|
@ -166,9 +167,6 @@ private:
|
|||
// 注册窗口
|
||||
HWND __create();
|
||||
|
||||
// 处理窗口消息
|
||||
void __poll();
|
||||
|
||||
// Win32 窗口消息回调程序
|
||||
static LRESULT CALLBACK WndProc(
|
||||
HWND hWnd,
|
||||
|
|
@ -179,6 +177,7 @@ private:
|
|||
|
||||
private:
|
||||
HWND _hWnd;
|
||||
MSG _msg;
|
||||
Size _size;
|
||||
String _title;
|
||||
int _iconID;
|
||||
|
|
@ -232,8 +231,6 @@ class Listener;
|
|||
// 输入设备
|
||||
class Input
|
||||
{
|
||||
friend class Game;
|
||||
|
||||
public:
|
||||
// 鼠标键值
|
||||
enum class Mouse : int
|
||||
|
|
@ -243,7 +240,6 @@ public:
|
|||
Middle /* 鼠标中键 */
|
||||
};
|
||||
|
||||
|
||||
// 键盘键值
|
||||
enum class Key : int
|
||||
{
|
||||
|
|
@ -357,6 +353,9 @@ public:
|
|||
// 获得鼠标Z轴(鼠标滚轮)坐标增量
|
||||
double getMouseDeltaZ();
|
||||
|
||||
// 刷新输入设备状态
|
||||
void update();
|
||||
|
||||
// 添加输入监听
|
||||
static Listener * addListener(
|
||||
const Function& func, /* 监听到用户输入时的执行函数 */
|
||||
|
|
@ -398,6 +397,9 @@ public:
|
|||
// 移除所有监听器
|
||||
static void removeAllListeners();
|
||||
|
||||
// 强制清空所有监听器
|
||||
static void clearAllListeners();
|
||||
|
||||
private:
|
||||
Input();
|
||||
|
||||
|
|
@ -405,9 +407,6 @@ private:
|
|||
|
||||
E2D_DISABLE_COPY(Input);
|
||||
|
||||
// 刷新输入信息
|
||||
void __update();
|
||||
|
||||
// 刷新设备状态
|
||||
void __updateDeviceState();
|
||||
|
||||
|
|
@ -429,9 +428,6 @@ private:
|
|||
// 渲染器
|
||||
class Renderer
|
||||
{
|
||||
friend class Game;
|
||||
friend class Window;
|
||||
|
||||
public:
|
||||
// 获取渲染器实例
|
||||
static Renderer * getInstance();
|
||||
|
|
@ -452,6 +448,9 @@ public:
|
|||
bool show = true
|
||||
);
|
||||
|
||||
// 渲染游戏画面
|
||||
void render();
|
||||
|
||||
// 获取文字渲染器
|
||||
TextRenderer * getTextRenderer();
|
||||
|
||||
|
|
@ -489,9 +488,6 @@ private:
|
|||
|
||||
E2D_DISABLE_COPY(Renderer);
|
||||
|
||||
// 渲染游戏画面
|
||||
void __render();
|
||||
|
||||
// 创建设备相关资源
|
||||
bool __createDeviceResources();
|
||||
|
||||
|
|
@ -516,7 +512,7 @@ private:
|
|||
};
|
||||
|
||||
|
||||
// 垃圾回收装置
|
||||
// 垃圾回收器
|
||||
class GC
|
||||
{
|
||||
friend class Game;
|
||||
|
|
@ -549,14 +545,14 @@ public:
|
|||
// 通知 GC 回收垃圾内存
|
||||
void notify();
|
||||
|
||||
// 手动回收垃圾内存
|
||||
void flush();
|
||||
|
||||
// 将对象放入 GC
|
||||
// 将对象放入释放池
|
||||
void addObject(
|
||||
Object * pObject
|
||||
Object * object
|
||||
);
|
||||
|
||||
// 更新垃圾回收器状态
|
||||
void update();
|
||||
|
||||
// 清空所有对象
|
||||
void clear();
|
||||
|
||||
|
|
@ -567,9 +563,6 @@ private:
|
|||
|
||||
E2D_DISABLE_COPY(GC);
|
||||
|
||||
// 更新 GC
|
||||
void __update();
|
||||
|
||||
private:
|
||||
bool _notifyed;
|
||||
std::set<Object*> _pool;
|
||||
|
|
|
|||
|
|
@ -109,6 +109,9 @@ public:
|
|||
// 移除所有监听器
|
||||
static void removeAllListeners();
|
||||
|
||||
// 强制清除所有监听器
|
||||
static void clearAllListeners();
|
||||
|
||||
private:
|
||||
// 更新监听器
|
||||
static void __update(
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ class Game;
|
|||
class Input;
|
||||
class Renderer;
|
||||
class Node;
|
||||
class Timer;
|
||||
class Task;
|
||||
class Action;
|
||||
class Player;
|
||||
class Collider;
|
||||
|
|
@ -19,9 +19,6 @@ class Transition;
|
|||
// 场景管理器
|
||||
class SceneManager
|
||||
{
|
||||
friend class Game;
|
||||
friend class Renderer;
|
||||
|
||||
public:
|
||||
// 获取场景管理器实例
|
||||
static SceneManager * getInstance();
|
||||
|
|
@ -53,6 +50,12 @@ public:
|
|||
// 是否正在进行转场动作
|
||||
bool isTransitioning();
|
||||
|
||||
// 更新场景内容
|
||||
void update();
|
||||
|
||||
// 渲染场景画面
|
||||
void render();
|
||||
|
||||
private:
|
||||
SceneManager();
|
||||
|
||||
|
|
@ -60,12 +63,6 @@ private:
|
|||
|
||||
E2D_DISABLE_COPY(SceneManager);
|
||||
|
||||
// 更新场景内容
|
||||
void __update();
|
||||
|
||||
// 渲染场景画面
|
||||
void __render();
|
||||
|
||||
private:
|
||||
bool _saveCurrScene;
|
||||
Scene * _currScene;
|
||||
|
|
@ -80,7 +77,6 @@ private:
|
|||
// 动作管理器
|
||||
class ActionManager
|
||||
{
|
||||
friend class Game;
|
||||
friend class Action;
|
||||
|
||||
public:
|
||||
|
|
@ -120,15 +116,6 @@ public:
|
|||
const String& name
|
||||
);
|
||||
|
||||
// 继续所有动作
|
||||
void resumeAll();
|
||||
|
||||
// 暂停所有动作
|
||||
void pauseAll();
|
||||
|
||||
// 停止所有动作
|
||||
void stopAll();
|
||||
|
||||
// 继续绑定在节点上的所有动作
|
||||
void resumeAllBindedWith(
|
||||
Node * target
|
||||
|
|
@ -144,11 +131,20 @@ public:
|
|||
Node * target
|
||||
);
|
||||
|
||||
// 清空绑定在节点上的所有动作
|
||||
// 强制清除绑定在节点上的所有动作
|
||||
void clearAllBindedWith(
|
||||
Node * target
|
||||
);
|
||||
|
||||
// 强制清除所有动作
|
||||
void clearAll();
|
||||
|
||||
// 更新动作管理器状态
|
||||
void update();
|
||||
|
||||
// 刷新所有动作计时
|
||||
void updateTime();
|
||||
|
||||
private:
|
||||
ActionManager();
|
||||
|
||||
|
|
@ -156,9 +152,6 @@ private:
|
|||
|
||||
E2D_DISABLE_COPY(ActionManager);
|
||||
|
||||
// 更新动作状态
|
||||
void __update();
|
||||
|
||||
// 添加动作
|
||||
void __add(
|
||||
Action * action
|
||||
|
|
@ -169,9 +162,6 @@ private:
|
|||
Action * action
|
||||
);
|
||||
|
||||
// 重置所有动作状态
|
||||
void __resetAll();
|
||||
|
||||
private:
|
||||
std::vector<Action*> _actions;
|
||||
std::vector<Action*> _runningActions;
|
||||
|
|
@ -193,6 +183,9 @@ public:
|
|||
// 销毁实例
|
||||
static void destroyInstance();
|
||||
|
||||
// 强制清除所有碰撞体
|
||||
void clearAll();
|
||||
|
||||
private:
|
||||
ColliderManager();
|
||||
|
||||
|
|
|
|||
129
core/e2dtool.h
129
core/e2dtool.h
|
|
@ -246,6 +246,9 @@ public:
|
|||
// 停止所有音乐
|
||||
void stopAll();
|
||||
|
||||
// 清空音乐缓存
|
||||
void clearCache();
|
||||
|
||||
// 获取 IXAudio2 对象
|
||||
IXAudio2 * getXAudio2();
|
||||
|
||||
|
|
@ -267,63 +270,119 @@ private:
|
|||
};
|
||||
|
||||
|
||||
// 定时器
|
||||
class Timer
|
||||
class Timer;
|
||||
|
||||
// 定时任务
|
||||
class Task :
|
||||
public Object
|
||||
{
|
||||
friend class Game;
|
||||
friend class Timer;
|
||||
|
||||
public:
|
||||
// 添加定时器(每帧执行一次)
|
||||
static void add(
|
||||
explicit Task(
|
||||
const Function& func, /* 执行函数 */
|
||||
const String& name = L"" /* 定时器名称 */
|
||||
);
|
||||
|
||||
// 添加定时器
|
||||
static void add(
|
||||
explicit Task(
|
||||
const Function& func, /* 执行函数 */
|
||||
double delay, /* 时间间隔(秒) */
|
||||
int times = -1, /* 执行次数(设 -1 为永久执行) */
|
||||
bool paused = false, /* 是否暂停 */
|
||||
const String& name = L"" /* 定时器名称 */
|
||||
);
|
||||
|
||||
// 在足够延迟后执行指定函数
|
||||
static void start(
|
||||
double timeout, /* 等待的时长(秒) */
|
||||
const Function& func /* 执行的函数 */
|
||||
);
|
||||
// 暂停任务
|
||||
void pause();
|
||||
|
||||
// 启动具有相同名称的定时器
|
||||
static void start(
|
||||
const String& name
|
||||
);
|
||||
// 继续任务
|
||||
void resume();
|
||||
|
||||
// 停止具有相同名称的定时器
|
||||
static void stop(
|
||||
const String& name
|
||||
);
|
||||
// 任务是否就绪
|
||||
bool isReady() const;
|
||||
|
||||
// 移除具有相同名称的定时器
|
||||
static void remove(
|
||||
const String& name
|
||||
);
|
||||
// 任务是否正在执行
|
||||
bool isRunning() const;
|
||||
|
||||
// 启动所有定时器
|
||||
static void startAll();
|
||||
// 获取任务名称
|
||||
String getName() const;
|
||||
|
||||
// 停止所有定时器
|
||||
static void stopAll();
|
||||
// 执行任务
|
||||
void update();
|
||||
|
||||
// 移除所有定时器
|
||||
static void removeAll();
|
||||
// 刷新任务计时
|
||||
void updateTime();
|
||||
|
||||
private:
|
||||
// 更新定时器
|
||||
static void __update();
|
||||
bool _running;
|
||||
bool _stopped;
|
||||
int _runTimes;
|
||||
int _totalTimes;
|
||||
double _delay;
|
||||
double _lastTime;
|
||||
String _name;
|
||||
Function _callback;
|
||||
};
|
||||
|
||||
// 重置定时器状态
|
||||
static void __resetAll();
|
||||
|
||||
// 定时器
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
// 获取定时器实例
|
||||
static Timer * getInstance();
|
||||
|
||||
// 销毁实例
|
||||
static void destroyInstance();
|
||||
|
||||
// 添加任务
|
||||
void addTask(
|
||||
Task * task
|
||||
);
|
||||
|
||||
// 继续具有相同名称的任务
|
||||
void resumeTasks(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 暂停具有相同名称的任务
|
||||
void pauseTasks(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 移除具有相同名称的任务
|
||||
void removeTasks(
|
||||
const String& name
|
||||
);
|
||||
|
||||
// 继续所有任务
|
||||
void resumeAllTasks();
|
||||
|
||||
// 暂停所有任务
|
||||
void pauseAllTasks();
|
||||
|
||||
// 移除所有任务
|
||||
void removeAllTasks();
|
||||
|
||||
// 强制清空所有任务
|
||||
void clearAllTasks();
|
||||
|
||||
// 更新定时器
|
||||
void update();
|
||||
|
||||
// 刷新所有任务计时
|
||||
void updateTime();
|
||||
|
||||
private:
|
||||
Timer();
|
||||
|
||||
~Timer();
|
||||
|
||||
E2D_DISABLE_COPY(Timer);
|
||||
|
||||
private:
|
||||
std::vector<Task*> _tasks;
|
||||
|
||||
static Timer * _instance;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -258,6 +258,7 @@
|
|||
<ClCompile Include="..\..\core\Tool\Path.cpp" />
|
||||
<ClCompile Include="..\..\core\Tool\Player.cpp" />
|
||||
<ClCompile Include="..\..\core\Tool\Random.cpp" />
|
||||
<ClCompile Include="..\..\core\Tool\Task.cpp" />
|
||||
<ClCompile Include="..\..\core\Tool\Timer.cpp" />
|
||||
<ClCompile Include="..\..\core\Transition\Transition.cpp" />
|
||||
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp" />
|
||||
|
|
|
|||
|
|
@ -237,6 +237,9 @@
|
|||
<ClCompile Include="..\..\core\Common\Collider.cpp">
|
||||
<Filter>Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\core\Tool\Task.cpp">
|
||||
<Filter>Tool</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\core\easy2d.h" />
|
||||
|
|
|
|||
Loading…
Reference in New Issue