重做Timer单例模式,增加Task定时任务类

This commit is contained in:
Nomango 2018-07-05 22:05:23 +08:00
parent 95a811cc17
commit 2d2c70c05d
19 changed files with 408 additions and 276 deletions

View File

@ -12,6 +12,7 @@ e2d::GC::~GC()
Renderer::destroyInstance(); Renderer::destroyInstance();
Input::destroyInstance(); Input::destroyInstance();
Window::destroyInstance(); Window::destroyInstance();
Timer::destroyInstance();
Player::destroyInstance(); Player::destroyInstance();
SceneManager::destroyInstance(); SceneManager::destroyInstance();
ActionManager::destroyInstance(); ActionManager::destroyInstance();
@ -24,7 +25,7 @@ e2d::GC::~GC()
// 它记录了对象被使用的次数,当计数为 0 时GC 会自动释放这个对象 // 它记录了对象被使用的次数,当计数为 0 时GC 会自动释放这个对象
// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中) // 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中)
// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数 // 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数
void e2d::GC::__update() void e2d::GC::update()
{ {
if (!_notifyed) return; if (!_notifyed) return;
@ -53,11 +54,11 @@ void e2d::GC::clear()
_pool.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; _notifyed = true;
} }
void e2d::GC::flush()
{
GC::notify();
GC::__update();
}

View File

@ -43,15 +43,16 @@ void e2d::Game::start(bool cleanup)
auto input = Input::getInstance(); auto input = Input::getInstance();
auto window = Window::getInstance(); auto window = Window::getInstance();
auto renderer = Renderer::getInstance(); auto renderer = Renderer::getInstance();
auto actionManager = ActionManager::getInstance(); auto timer = Timer::getInstance();
auto sceneManager = SceneManager::getInstance(); auto sceneManager = SceneManager::getInstance();
auto actionManager = ActionManager::getInstance();
// 显示窗口 // 显示窗口
::ShowWindow(window->getHWnd(), SW_SHOWNORMAL); ::ShowWindow(window->getHWnd(), SW_SHOWNORMAL);
// 刷新窗口内容 // 刷新窗口内容
::UpdateWindow(window->getHWnd()); ::UpdateWindow(window->getHWnd());
// 处理窗口消息 // 处理窗口消息
window->__poll(); window->poll();
// 初始化计时 // 初始化计时
Time::__init(); Time::__init();
@ -60,7 +61,7 @@ void e2d::Game::start(bool cleanup)
while (!_ended) while (!_ended)
{ {
// 处理窗口消息 // 处理窗口消息
window->__poll(); window->poll();
// 刷新时间 // 刷新时间
Time::__updateNow(); Time::__updateNow();
@ -73,18 +74,18 @@ void e2d::Game::start(bool cleanup)
_config->_update(); _config->_update();
} }
input->__update(); // 获取用户输入 input->update(); // 获取用户输入
Timer::__update(); // 更新定时器 timer->update(); // 更新定时器
actionManager->__update(); // 更新动作管理器 actionManager->update(); // 更新动作管理器
sceneManager->__update(); // 更新场景内容 sceneManager->update(); // 更新场景内容
renderer->__render(); // 渲染游戏画面 renderer->render(); // 渲染游戏画面
Time::__updateLast(); // 刷新时间信息 Time::__updateLast(); // 刷新时间信息
} }
else else
{ {
Time::__sleep(); // 挂起线程 Time::__sleep(); // 挂起线程
gc->__update(); // 刷新内存池 gc->update(); // 刷新内存池
} }
} }
@ -106,8 +107,8 @@ void e2d::Game::resume()
if (_paused && !_ended) if (_paused && !_ended)
{ {
Time::__reset(); Time::__reset();
Timer::__resetAll(); Timer::getInstance()->updateTime();
ActionManager::getInstance()->__resetAll(); ActionManager::getInstance()->updateTime();
} }
_paused = false; _paused = false;
} }
@ -147,14 +148,20 @@ void e2d::Game::cleanup()
{ {
// 删除所有场景 // 删除所有场景
SceneManager::getInstance()->clear(); SceneManager::getInstance()->clear();
// 清空定时器
Timer::getInstance()->clearAllTasks();
// 清除所有动作
ActionManager::getInstance()->clearAll();
// 清除所有碰撞体
ColliderManager::getInstance()->clearAll();
// 删除碰撞监听器 // 删除碰撞监听器
Collision::removeAllListeners(); Collision::clearAllListeners();
// 删除输入监听器 // 删除输入监听器
Input::removeAllListeners(); Input::clearAllListeners();
// 清空图片缓存 // 清空图片缓存
Image::clearCache(); Image::clearCache();
// 清空定时器 // 清空音乐缓存
Timer::removeAll(); Player::getInstance()->clearCache();
// 删除所有对象 // 删除所有对象
GC::getInstance()->clear(); GC::getInstance()->clear();
} }

View File

@ -112,7 +112,7 @@ void e2d::Input::destroyInstance()
} }
} }
void e2d::Input::__update() void e2d::Input::update()
{ {
Input::__updateDeviceState(); Input::__updateDeviceState();
Input::__updateListeners(); Input::__updateListeners();
@ -330,7 +330,15 @@ void e2d::Input::removeAllListeners()
{ {
for (auto listener : s_vListeners) 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(); s_vListeners.clear();
} }

View File

@ -118,7 +118,7 @@ void e2d::Renderer::__discardDeviceResources()
SafeRelease(_roundStrokeStyle); SafeRelease(_roundStrokeStyle);
} }
void e2d::Renderer::__render() void e2d::Renderer::render()
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -131,7 +131,7 @@ void e2d::Renderer::__render()
_renderTarget->Clear(_clearColor); _renderTarget->Clear(_clearColor);
// äÖȾ³¡¾° // äÖȾ³¡¾°
SceneManager::getInstance()->__render(); SceneManager::getInstance()->render();
// äÖȾ FPS // äÖȾ FPS
if (_showFps) if (_showFps)

View File

@ -172,14 +172,12 @@ HWND e2d::Window::__create()
return hWnd; 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); ::TranslateMessage(&_msg);
::DispatchMessage(&msg); ::DispatchMessage(&_msg);
} }
} }
@ -429,7 +427,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
// ÖØ»æ´°¿Ú // ÖØ»æ´°¿Ú
case WM_PAINT: case WM_PAINT:
{ {
e2d::Renderer::getInstance()->__render(); e2d::Renderer::getInstance()->render();
ValidateRect(hWnd, nullptr); ValidateRect(hWnd, nullptr);
} }
result = 0; result = 0;

View File

@ -215,7 +215,15 @@ void e2d::Collision::removeAllListeners()
{ {
for (auto listener : s_vListeners) 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(); s_vListeners.clear();
} }

View File

@ -29,11 +29,9 @@ e2d::ActionManager::ActionManager()
e2d::ActionManager::~ActionManager() e2d::ActionManager::~ActionManager()
{ {
_actions.clear();
_runningActions.clear();
} }
void e2d::ActionManager::__update() void e2d::ActionManager::update()
{ {
if (_runningActions.empty() || Game::getInstance()->isPaused()) if (_runningActions.empty() || Game::getInstance()->isPaused())
return; return;
@ -213,40 +211,14 @@ void e2d::ActionManager::clearAllBindedWith(Node * target)
} }
} }
void e2d::ActionManager::resumeAll() void e2d::ActionManager::clearAll()
{ {
auto scene = SceneManager::getInstance()->getCurrentScene(); for (auto action : _runningActions)
if (scene)
{ {
for (auto child : scene->getRoot()->getAllChildren()) action->release();
{
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);
}
} }
_actions.clear();
_runningActions.clear();
} }
std::vector<e2d::Action*> e2d::ActionManager::get(const String& name) std::vector<e2d::Action*> e2d::ActionManager::get(const String& name)
@ -267,7 +239,7 @@ const std::vector<e2d::Action*>& e2d::ActionManager::getAll()
return _actions; return _actions;
} }
void e2d::ActionManager::__resetAll() void e2d::ActionManager::updateTime()
{ {
for (auto action : _runningActions) for (auto action : _runningActions)
{ {

View File

@ -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) void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
{ {
// 判断碰撞监听是否打开 // 判断碰撞监听是否打开

View File

@ -115,7 +115,7 @@ bool e2d::SceneManager::isTransitioning()
return _transition != nullptr; return _transition != nullptr;
} }
void e2d::SceneManager::__update() void e2d::SceneManager::update()
{ {
if (_transition == nullptr) if (_transition == nullptr)
{ {
@ -168,7 +168,7 @@ void e2d::SceneManager::__update()
} }
} }
void e2d::SceneManager::__render() void e2d::SceneManager::render()
{ {
if (_transition) if (_transition)
{ {

View File

@ -499,7 +499,7 @@ bool e2d::Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wcha
if (nullptr == strFilename || nullptr == strDestPath || cchDest < 10) if (nullptr == strFilename || nullptr == strDestPath || cchDest < 10)
return false; return false;
// Get the exe name, and exe path // Get the exe _name, and exe path
wchar_t strExePath[MAX_PATH] = { 0 }; wchar_t strExePath[MAX_PATH] = { 0 };
wchar_t strExeName[MAX_PATH] = { 0 }; wchar_t strExeName[MAX_PATH] = { 0 };
wchar_t* strLastSlash = nullptr; 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]); 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; *strLastSlash = 0;
// Chop the .exe from the exe name // Chop the .exe from the exe _name
strLastSlash = wcsrchr(strExeName, TEXT('.')); strLastSlash = wcsrchr(strExeName, TEXT('.'));
if (strLastSlash) if (strLastSlash)
*strLastSlash = 0; *strLastSlash = 0;
@ -523,7 +523,7 @@ bool e2d::Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wcha
if (GetFileAttributes(strDestPath) != 0xFFFFFFFF) if (GetFileAttributes(strDestPath) != 0xFFFFFFFF)
return true; 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 }; wchar_t strLeafName[MAX_PATH] = { 0 };
wcscpy_s(strLeafName, MAX_PATH, strFilename); wcscpy_s(strLeafName, MAX_PATH, strFilename);

View File

@ -221,6 +221,11 @@ void e2d::Player::setVolume(double volume)
{ {
pair.second->setVolume(_volume); pair.second->setVolume(_volume);
} }
for (auto pair : _resList)
{
pair.second->setVolume(_volume);
}
} }
void e2d::Player::pauseAll() void e2d::Player::pauseAll()
@ -229,6 +234,11 @@ void e2d::Player::pauseAll()
{ {
pair.second->pause(); pair.second->pause();
} }
for (auto pair : _resList)
{
pair.second->pause();
}
} }
void e2d::Player::resumeAll() void e2d::Player::resumeAll()
@ -237,6 +247,11 @@ void e2d::Player::resumeAll()
{ {
pair.second->resume(); pair.second->resume();
} }
for (auto pair : _resList)
{
pair.second->resume();
}
} }
void e2d::Player::stopAll() void e2d::Player::stopAll()
@ -245,4 +260,24 @@ void e2d::Player::stopAll()
{ {
pair.second->stop(); 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();
} }

84
core/Tool/Task.cpp Normal file
View File

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

View File

@ -1,168 +1,132 @@
#include "..\e2dtool.h" #include "..\e2dtool.h"
#include "..\e2dnode.h"
namespace e2d e2d::Timer * e2d::Timer::_instance = nullptr;
e2d::Timer * e2d::Timer::getInstance()
{ {
class TimerEntity if (!_instance)
{ _instance = new (std::nothrow) Timer;
public: return _instance;
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;
};
} }
static std::vector<e2d::TimerEntity*> s_vTimers; void e2d::Timer::destroyInstance()
void e2d::Timer::add(const Function& func, double delay, int updateTimes, bool paused, const String& name)
{ {
auto timer = new (std::nothrow) TimerEntity(func, name, delay, updateTimes, paused); if (_instance)
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 (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; return;
for (size_t i = 0; i < s_vTimers.size();) for (size_t i = 0; i < _tasks.size();)
{ {
auto timer = s_vTimers[i]; auto task = _tasks[i];
// Çå³ýÒÑÍ£Ö¹µÄ¶¨Ê±Æ÷ // Çå³ýÒÑÍ£Ö¹µÄÈÎÎñ
if (timer->stopped) if (task->_stopped)
{ {
delete timer; task->release();
s_vTimers.erase(s_vTimers.begin() + i); _tasks.erase(_tasks.begin() + i);
} }
else else
{ {
// 更新定时器 // 更新定时器
if (timer->ready()) if (task->isReady())
{ {
timer->update(); task->update();
} }
++i; ++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();
} }
} }

View File

@ -67,8 +67,6 @@ private:
// 窗口控制 // 窗口控制
class Window class Window
{ {
friend class Game;
public: public:
// 鼠标指针样式 // 鼠标指针样式
enum class Cursor : int enum class Cursor : int
@ -156,6 +154,9 @@ public:
const String& title = L"Error" /* 窗口标题 */ const String& title = L"Error" /* 窗口标题 */
); );
// 处理窗口消息
void poll();
private: private:
Window(); Window();
@ -166,9 +167,6 @@ private:
// 注册窗口 // 注册窗口
HWND __create(); HWND __create();
// 处理窗口消息
void __poll();
// Win32 窗口消息回调程序 // Win32 窗口消息回调程序
static LRESULT CALLBACK WndProc( static LRESULT CALLBACK WndProc(
HWND hWnd, HWND hWnd,
@ -179,6 +177,7 @@ private:
private: private:
HWND _hWnd; HWND _hWnd;
MSG _msg;
Size _size; Size _size;
String _title; String _title;
int _iconID; int _iconID;
@ -232,8 +231,6 @@ class Listener;
// 输入设备 // 输入设备
class Input class Input
{ {
friend class Game;
public: public:
// 鼠标键值 // 鼠标键值
enum class Mouse : int enum class Mouse : int
@ -243,7 +240,6 @@ public:
Middle /* 鼠标中键 */ Middle /* 鼠标中键 */
}; };
// 键盘键值 // 键盘键值
enum class Key : int enum class Key : int
{ {
@ -357,6 +353,9 @@ public:
// 获得鼠标Z轴鼠标滚轮坐标增量 // 获得鼠标Z轴鼠标滚轮坐标增量
double getMouseDeltaZ(); double getMouseDeltaZ();
// 刷新输入设备状态
void update();
// 添加输入监听 // 添加输入监听
static Listener * addListener( static Listener * addListener(
const Function& func, /* 监听到用户输入时的执行函数 */ const Function& func, /* 监听到用户输入时的执行函数 */
@ -398,6 +397,9 @@ public:
// 移除所有监听器 // 移除所有监听器
static void removeAllListeners(); static void removeAllListeners();
// 强制清空所有监听器
static void clearAllListeners();
private: private:
Input(); Input();
@ -405,9 +407,6 @@ private:
E2D_DISABLE_COPY(Input); E2D_DISABLE_COPY(Input);
// 刷新输入信息
void __update();
// 刷新设备状态 // 刷新设备状态
void __updateDeviceState(); void __updateDeviceState();
@ -429,9 +428,6 @@ private:
// 渲染器 // 渲染器
class Renderer class Renderer
{ {
friend class Game;
friend class Window;
public: public:
// 获取渲染器实例 // 获取渲染器实例
static Renderer * getInstance(); static Renderer * getInstance();
@ -452,6 +448,9 @@ public:
bool show = true bool show = true
); );
// 渲染游戏画面
void render();
// 获取文字渲染器 // 获取文字渲染器
TextRenderer * getTextRenderer(); TextRenderer * getTextRenderer();
@ -489,9 +488,6 @@ private:
E2D_DISABLE_COPY(Renderer); E2D_DISABLE_COPY(Renderer);
// 渲染游戏画面
void __render();
// 创建设备相关资源 // 创建设备相关资源
bool __createDeviceResources(); bool __createDeviceResources();
@ -516,7 +512,7 @@ private:
}; };
// 垃圾回收装置 // 垃圾回收
class GC class GC
{ {
friend class Game; friend class Game;
@ -549,14 +545,14 @@ public:
// 通知 GC 回收垃圾内存 // 通知 GC 回收垃圾内存
void notify(); void notify();
// 手动回收垃圾内存 // 将对象放入释放池
void flush();
// 将对象放入 GC
void addObject( void addObject(
Object * pObject Object * object
); );
// 更新垃圾回收器状态
void update();
// 清空所有对象 // 清空所有对象
void clear(); void clear();
@ -567,9 +563,6 @@ private:
E2D_DISABLE_COPY(GC); E2D_DISABLE_COPY(GC);
// 更新 GC
void __update();
private: private:
bool _notifyed; bool _notifyed;
std::set<Object*> _pool; std::set<Object*> _pool;

View File

@ -109,6 +109,9 @@ public:
// 移除所有监听器 // 移除所有监听器
static void removeAllListeners(); static void removeAllListeners();
// 强制清除所有监听器
static void clearAllListeners();
private: private:
// 更新监听器 // 更新监听器
static void __update( static void __update(

View File

@ -9,7 +9,7 @@ class Game;
class Input; class Input;
class Renderer; class Renderer;
class Node; class Node;
class Timer; class Task;
class Action; class Action;
class Player; class Player;
class Collider; class Collider;
@ -19,9 +19,6 @@ class Transition;
// 场景管理器 // 场景管理器
class SceneManager class SceneManager
{ {
friend class Game;
friend class Renderer;
public: public:
// 获取场景管理器实例 // 获取场景管理器实例
static SceneManager * getInstance(); static SceneManager * getInstance();
@ -53,6 +50,12 @@ public:
// 是否正在进行转场动作 // 是否正在进行转场动作
bool isTransitioning(); bool isTransitioning();
// 更新场景内容
void update();
// 渲染场景画面
void render();
private: private:
SceneManager(); SceneManager();
@ -60,12 +63,6 @@ private:
E2D_DISABLE_COPY(SceneManager); E2D_DISABLE_COPY(SceneManager);
// 更新场景内容
void __update();
// 渲染场景画面
void __render();
private: private:
bool _saveCurrScene; bool _saveCurrScene;
Scene * _currScene; Scene * _currScene;
@ -80,7 +77,6 @@ private:
// 动作管理器 // 动作管理器
class ActionManager class ActionManager
{ {
friend class Game;
friend class Action; friend class Action;
public: public:
@ -120,15 +116,6 @@ public:
const String& name const String& name
); );
// 继续所有动作
void resumeAll();
// 暂停所有动作
void pauseAll();
// 停止所有动作
void stopAll();
// 继续绑定在节点上的所有动作 // 继续绑定在节点上的所有动作
void resumeAllBindedWith( void resumeAllBindedWith(
Node * target Node * target
@ -144,11 +131,20 @@ public:
Node * target Node * target
); );
// 清空绑定在节点上的所有动作 // 强制清除绑定在节点上的所有动作
void clearAllBindedWith( void clearAllBindedWith(
Node * target Node * target
); );
// 强制清除所有动作
void clearAll();
// 更新动作管理器状态
void update();
// 刷新所有动作计时
void updateTime();
private: private:
ActionManager(); ActionManager();
@ -156,9 +152,6 @@ private:
E2D_DISABLE_COPY(ActionManager); E2D_DISABLE_COPY(ActionManager);
// 更新动作状态
void __update();
// 添加动作 // 添加动作
void __add( void __add(
Action * action Action * action
@ -169,9 +162,6 @@ private:
Action * action Action * action
); );
// 重置所有动作状态
void __resetAll();
private: private:
std::vector<Action*> _actions; std::vector<Action*> _actions;
std::vector<Action*> _runningActions; std::vector<Action*> _runningActions;
@ -193,6 +183,9 @@ public:
// 销毁实例 // 销毁实例
static void destroyInstance(); static void destroyInstance();
// 强制清除所有碰撞体
void clearAll();
private: private:
ColliderManager(); ColliderManager();

View File

@ -246,6 +246,9 @@ public:
// 停止所有音乐 // 停止所有音乐
void stopAll(); void stopAll();
// 清空音乐缓存
void clearCache();
// 获取 IXAudio2 对象 // 获取 IXAudio2 对象
IXAudio2 * getXAudio2(); IXAudio2 * getXAudio2();
@ -267,63 +270,119 @@ private:
}; };
// 定时器 class Timer;
class Timer
// 定时任务
class Task :
public Object
{ {
friend class Game; friend class Timer;
public: public:
// 添加定时器(每帧执行一次) explicit Task(
static void add(
const Function& func, /* 执行函数 */ const Function& func, /* 执行函数 */
const String& name = L"" /* 定时器名称 */ const String& name = L"" /* 定时器名称 */
); );
// 添加定时器 explicit Task(
static void add(
const Function& func, /* 执行函数 */ const Function& func, /* 执行函数 */
double delay, /* 时间间隔(秒) */ double delay, /* 时间间隔(秒) */
int times = -1, /* 执行次数(设 -1 为永久执行) */ int times = -1, /* 执行次数(设 -1 为永久执行) */
bool paused = false, /* 是否暂停 */
const String& name = L"" /* 定时器名称 */ const String& name = L"" /* 定时器名称 */
); );
// 在足够延迟后执行指定函数 // 暂停任务
static void start( void pause();
double timeout, /* 等待的时长(秒) */
const Function& func /* 执行的函数 */
);
// 启动具有相同名称的定时器 // 继续任务
static void start( void resume();
const String& name
);
// 停止具有相同名称的定时器 // 任务是否就绪
static void stop( bool isReady() const;
const String& name
);
// 移除具有相同名称的定时器 // 任务是否正在执行
static void remove( bool isRunning() const;
const String& name
);
// 启动所有定时器 // 获取任务名称
static void startAll(); String getName() const;
// 停止所有定时器 // 执行任务
static void stopAll(); void update();
// 移除所有定时器 // 刷新任务计时
static void removeAll(); void updateTime();
private: private:
// 更新定时器 bool _running;
static void __update(); 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;
}; };

View File

@ -258,6 +258,7 @@
<ClCompile Include="..\..\core\Tool\Path.cpp" /> <ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Player.cpp" /> <ClCompile Include="..\..\core\Tool\Player.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" /> <ClCompile Include="..\..\core\Tool\Random.cpp" />
<ClCompile Include="..\..\core\Tool\Task.cpp" />
<ClCompile Include="..\..\core\Tool\Timer.cpp" /> <ClCompile Include="..\..\core\Tool\Timer.cpp" />
<ClCompile Include="..\..\core\Transition\Transition.cpp" /> <ClCompile Include="..\..\core\Transition\Transition.cpp" />
<ClCompile Include="..\..\core\Transition\BoxTransition.cpp" /> <ClCompile Include="..\..\core\Transition\BoxTransition.cpp" />

View File

@ -237,6 +237,9 @@
<ClCompile Include="..\..\core\Common\Collider.cpp"> <ClCompile Include="..\..\core\Common\Collider.cpp">
<Filter>Common</Filter> <Filter>Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Tool\Task.cpp">
<Filter>Tool</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" /> <ClInclude Include="..\..\core\easy2d.h" />