update Task & Timer

This commit is contained in:
Nomango 2018-08-12 13:24:31 +08:00
parent 4ef4715ea6
commit 91b7458766
4 changed files with 42 additions and 49 deletions

View File

@ -66,7 +66,7 @@ void e2d::Game::start()
} }
else else
{ {
// ID2D1HwndRenderTarget 在渲染时会等待显示器刷新,即开启了垂直同步 // ID2D1HwndRenderTarget 开启了垂直同步,在渲染时会等待显示器刷新,
// 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。 // 它起到了非常稳定的延时作用,所以大部分时候不需要手动挂起线程进行延时。
// 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。 // 下面的代码仅在一些情况下(例如窗口最小化时)挂起线程,防止占用过高 CPU 。
int wait = minInterval - dur.milliseconds(); int wait = minInterval - dur.milliseconds();

View File

@ -23,18 +23,18 @@ e2d::Task::Task(const Function & func, float delay, int times, const String & na
{ {
} }
void e2d::Task::pause() void e2d::Task::start()
{
_running = true;
_lastTime = Time::now();
}
void e2d::Task::stop()
{ {
_running = false; _running = false;
} }
void e2d::Task::resume() void e2d::Task::_update()
{
_running = true;
updateTime();
}
void e2d::Task::update()
{ {
if (_totalTimes == 0) if (_totalTimes == 0)
{ {
@ -57,7 +57,7 @@ void e2d::Task::update()
} }
} }
bool e2d::Task::isReady() const bool e2d::Task::_isReady() const
{ {
if (_running) if (_running)
{ {
@ -82,8 +82,3 @@ e2d::String e2d::Task::getName() const
{ {
return _name; return _name;
} }
void e2d::Task::updateTime()
{
_lastTime = Time::now();
}

View File

@ -35,30 +35,30 @@ void e2d::Timer::addTask(Task * task)
if (iter == _tasks.end()) if (iter == _tasks.end())
{ {
task->retain(); task->retain();
task->updateTime(); task->_lastTime = Time::now();
_tasks.push_back(task); _tasks.push_back(task);
} }
} }
} }
void e2d::Timer::pauseTasks(const String& name) void e2d::Timer::stopTasks(const String& name)
{ {
for (auto task : _tasks) for (auto task : _tasks)
{ {
if (task->getName() == name) if (task->getName() == name)
{ {
task->pause(); task->stop();
} }
} }
} }
void e2d::Timer::resumeTasks(const String& name) void e2d::Timer::startTasks(const String& name)
{ {
for (auto task : _tasks) for (auto task : _tasks)
{ {
if (task->getName() == name) if (task->getName() == name)
{ {
task->resume(); task->start();
} }
} }
} }
@ -74,19 +74,19 @@ void e2d::Timer::removeTasks(const String& name)
} }
} }
void e2d::Timer::pauseAllTasks() void e2d::Timer::stopAllTasks()
{ {
for (auto task : _tasks) for (auto task : _tasks)
{ {
task->pause(); task->stop();
} }
} }
void e2d::Timer::resumeAllTasks() void e2d::Timer::startAllTasks()
{ {
for (auto task : _tasks) for (auto task : _tasks)
{ {
task->resume(); task->start();
} }
} }
@ -126,13 +126,13 @@ void e2d::Timer::update()
} }
else else
{ {
// ¸üж¨Ê±Æ÷
if (task->isReady())
{
task->update();
}
++i; ++i;
// ¸üж¨Ê±Æ÷
if (task->_isReady())
{
task->_update();
}
} }
} }
} }
@ -141,6 +141,6 @@ void e2d::Timer::updateTime()
{ {
for (auto task : _tasks) for (auto task : _tasks)
{ {
task->updateTime(); task->_lastTime = Time::now();
} }
} }

View File

@ -245,14 +245,11 @@ public:
const String& name = L"" /* 任务名称 */ const String& name = L"" /* 任务名称 */
); );
// 暂停任务 // 启动任务
void pause(); void start();
// 继续任务 // 停止任务
void resume(); void stop();
// 任务是否就绪
bool isReady() const;
// 任务是否正在执行 // 任务是否正在执行
bool isRunning() const; bool isRunning() const;
@ -260,11 +257,12 @@ public:
// 获取任务名称 // 获取任务名称
String getName() const; String getName() const;
protected:
// 执行任务 // 执行任务
void update(); void _update();
// 刷新任务计时 // 任务是否就绪
void updateTime(); bool _isReady() const;
private: private:
bool _running; bool _running;
@ -293,13 +291,13 @@ public:
Task * task Task * task
); );
// 继续任务 // 启动任务
void resumeTasks( void startTasks(
const String& taskName const String& taskName
); );
// 停任务 // 任务
void pauseTasks( void stopTasks(
const String& taskName const String& taskName
); );
@ -308,11 +306,11 @@ public:
const String& taskName const String& taskName
); );
// 继续所有任务 // 启动所有任务
void resumeAllTasks(); void startAllTasks();
// 停所有任务 // 所有任务
void pauseAllTasks(); void stopAllTasks();
// 移除所有任务 // 移除所有任务
void removeAllTasks(); void removeAllTasks();