diff --git a/core/Base/Time.cpp b/core/Base/Time.cpp index 79aafd27..a2b57872 100644 --- a/core/Base/Time.cpp +++ b/core/Base/Time.cpp @@ -59,7 +59,7 @@ void e2d::Time::__updateLast() s_tNow = steady_clock::now(); s_nInterval = static_cast(duration_cast(s_tNow - s_tLastUpdate).count()); - s_fTotalTime = static_cast(duration_cast(s_tNow - s_tStart).count()) / 1000.0f; + s_fTotalTime = static_cast(duration_cast(s_tNow - s_tStart).count()) / 1000.0; } void e2d::Time::__sleep() diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index 95882ce5..7b43e60c 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -16,15 +16,43 @@ e2d::Timer::Timer() TimerManager::add(this); } -e2d::Timer::Timer(const String & name, const TimerCallback & callback, double interval /* = 0 */, int repeatTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) - : Timer() +e2d::Timer::Timer(const TimerCallback & callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) + : m_bRunning(false) + , m_nRunTimes(0) + , m_Callback(nullptr) + , m_fInterval(0) + , m_fLast(0) + , m_nUpdateTimes(-1) + , m_bAtOnce(false) + , m_bAutoRelease(false) + , m_bClear(false) { - this->setName(name); this->setCallback(callback); - this->setUpdateTimes(repeatTimes); + this->setUpdateTimes(updateTimes); this->setInterval(interval); m_bAutoRelease = autoRelease; m_bAtOnce = atOnce; + TimerManager::add(this); +} + +e2d::Timer::Timer(const String & name, const TimerCallback & callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) + : m_bRunning(false) + , m_nRunTimes(0) + , m_Callback(nullptr) + , m_fInterval(0) + , m_fLast(0) + , m_nUpdateTimes(-1) + , m_bAtOnce(false) + , m_bAutoRelease(false) + , m_bClear(false) +{ + this->setName(name); + this->setCallback(callback); + this->setUpdateTimes(updateTimes); + this->setInterval(interval); + m_bAutoRelease = autoRelease; + m_bAtOnce = atOnce; + TimerManager::add(this); } bool e2d::Timer::isRunning() const @@ -69,9 +97,10 @@ void e2d::Timer::setCallback(const TimerCallback & callback) m_Callback = callback; } -void e2d::Timer::setUpdateTimes(int repeatTimes) +void e2d::Timer::setUpdateTimes(int updateTimes) { - m_nUpdateTimes = repeatTimes; + m_nUpdateTimes = updateTimes; + m_bClear = (m_nUpdateTimes == 0); } void e2d::Timer::setRunAtOnce(bool bAtOnce) @@ -81,7 +110,7 @@ void e2d::Timer::setRunAtOnce(bool bAtOnce) void e2d::Timer::update() { - if (m_Callback && m_nRunTimes < m_nUpdateTimes) + if (m_Callback) { m_Callback(); } @@ -89,7 +118,7 @@ void e2d::Timer::update() m_nRunTimes++; m_fLast += m_fInterval; - if (m_nRunTimes >= m_nUpdateTimes) + if (m_nRunTimes == m_nUpdateTimes) { if (m_bAutoRelease) { @@ -104,7 +133,7 @@ void e2d::Timer::update() bool e2d::Timer::isReady() const { - if (m_bRunning) + if (m_bRunning && !m_bClear) { if (m_bAtOnce && m_nRunTimes == 0) return true; @@ -113,9 +142,7 @@ bool e2d::Timer::isReady() const return true; if ((Time::getTotalTime() - m_fLast) >= m_fInterval) - { return true; - } } return false; } diff --git a/core/etools.h b/core/etools.h index f18682ea..b0e41fea 100644 --- a/core/etools.h +++ b/core/etools.h @@ -49,6 +49,14 @@ class Timer : public: Timer(); + Timer( + const TimerCallback &callback, /* 定时器回调函数 */ + double interval = 0, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ + bool atOnce = false, /* 是否立即执行 */ + bool autoRelease = false /* 自动清除 */ + ); + Timer( const String &name, /* 定时器名称 */ const TimerCallback &callback, /* 定时器回调函数 */ diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index c9a82584..fd82e125 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -54,7 +54,9 @@ + + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index cd68fa89..aeb84b3b 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -191,5 +191,11 @@ Shape + + Common + + + Common + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 54264b33..3b4c867b 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -193,7 +193,9 @@ + + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index f12aa1dd..778a987a 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -192,5 +192,11 @@ Shape + + Common + + + Common + \ No newline at end of file