From c3ebe2545ac90a6c38aa02a97ebb643350393911 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 22 Jul 2018 00:41:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8Time=E5=92=8CDuration?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Action/Action.cpp | 6 +-- core/Action/Animate.cpp | 5 ++- core/Action/Delay.cpp | 4 +- core/Action/FiniteTimeAction.cpp | 4 +- core/Base/Game.cpp | 24 ++++++------ core/Base/Renderer.cpp | 5 ++- core/Common/Duration.cpp | 63 ++++++++++++++++++++++++++++++-- core/Common/Time.cpp | 24 ++++++++++-- core/Tool/Task.cpp | 4 +- core/Transition/Transition.cpp | 7 ++-- core/e2dbase.h | 15 ++++---- core/e2dcommon.h | 44 ++++++++++++++++++---- core/e2dtransition.h | 2 +- 13 files changed, 155 insertions(+), 52 deletions(-) diff --git a/core/Action/Action.cpp b/core/Action/Action.cpp index a23b0974..8ddd2cdb 100644 --- a/core/Action/Action.cpp +++ b/core/Action/Action.cpp @@ -24,7 +24,7 @@ bool e2d::Action::isRunning() void e2d::Action::resume() { _running = true; - _last = Game::getInstance()->getTotalTime(); + _last = Game::getInstance()->getTotalDuration().seconds(); } void e2d::Action::pause() @@ -56,7 +56,7 @@ void e2d::Action::reset() { _initialized = false; _done = false; - _last = Game::getInstance()->getTotalTime(); + _last = Game::getInstance()->getTotalDuration().seconds(); } bool e2d::Action::_isDone() @@ -74,7 +74,7 @@ void e2d::Action::_startWithTarget(Node* target) void e2d::Action::_init() { _initialized = true; - _last = Game::getInstance()->getTotalTime(); + _last = Game::getInstance()->getTotalDuration().seconds(); } void e2d::Action::_update() diff --git a/core/Action/Animate.cpp b/core/Action/Animate.cpp index 5ce614aa..02f27fe5 100644 --- a/core/Action/Animate.cpp +++ b/core/Action/Animate.cpp @@ -56,7 +56,8 @@ void e2d::Animate::_update() return; } - while ((Game::getInstance()->getTotalTime() - _last) >= _animation->getInterval()) + auto game = Game::getInstance(); + while ((game->getTotalDuration().seconds() - _last) >= _animation->getInterval()) { auto& frames = _animation->getFrames(); auto target = dynamic_cast(_target); @@ -80,7 +81,7 @@ void e2d::Animate::_update() void e2d::Animate::_resetTime() { Action::_resetTime(); - _last = Game::getInstance()->getTotalTime(); + _last = Game::getInstance()->getTotalDuration().seconds(); } void e2d::Animate::reset() diff --git a/core/Action/Delay.cpp b/core/Action/Delay.cpp index 007f6e08..6becbd70 100644 --- a/core/Action/Delay.cpp +++ b/core/Action/Delay.cpp @@ -31,7 +31,7 @@ void e2d::Delay::_update() { Action::_update(); - _delta = Game::getInstance()->getTotalTime() - _last; + _delta = Game::getInstance()->getTotalDuration().seconds() - _last; if (_delta >= _delay) { @@ -42,5 +42,5 @@ void e2d::Delay::_update() void e2d::Delay::_resetTime() { Action::_resetTime(); - _last = Game::getInstance()->getTotalTime() - _delta; + _last = Game::getInstance()->getTotalDuration().seconds() - _delta; } diff --git a/core/Action/FiniteTimeAction.cpp b/core/Action/FiniteTimeAction.cpp index ae11a3eb..1c2d1ff3 100644 --- a/core/Action/FiniteTimeAction.cpp +++ b/core/Action/FiniteTimeAction.cpp @@ -28,7 +28,7 @@ void e2d::FiniteTimeAction::_update() } else { - _delta = std::min((Game::getInstance()->getTotalTime() - _last) / _duration, 1.0); + _delta = std::min((Game::getInstance()->getTotalDuration().seconds() - _last) / _duration, 1.0); if (_delta >= 1) { @@ -40,5 +40,5 @@ void e2d::FiniteTimeAction::_update() void e2d::FiniteTimeAction::_resetTime() { Action::_resetTime(); - _last = Game::getInstance()->getTotalTime() - _delta * _duration; + _last = Game::getInstance()->getTotalDuration().seconds() - _delta * _duration; } diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 93b51baa..2b1a79a6 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -15,7 +15,7 @@ e2d::Game::Game() { CoInitialize(nullptr); - _start = _last = _now = steady_clock::now(); + _start = _last = _now = Time::now(); } e2d::Game::~Game() @@ -67,16 +67,16 @@ void e2d::Game::start() window->poll(); // 开始游戏 - const milliseconds frameInterval(15LL); - milliseconds wait, interval; + Duration frameInterval(15), interval; + int wait = 0; _ended = false; - _last = _now = steady_clock::now(); + _last = _now = Time::now(); while (!_ended) { - _now = steady_clock::now(); - interval = duration_cast(_now - _last); + _now = Time::now(); + interval = _now - _last; if (frameInterval < interval) { @@ -93,10 +93,10 @@ void e2d::Game::start() } else { - wait = frameInterval - interval; - if (wait.count() > 1LL) + wait = (frameInterval - interval).milliseconds() - 1; + if (wait > 1) { - std::this_thread::sleep_for(wait - milliseconds(1LL)); + std::this_thread::sleep_for(milliseconds(wait)); } } } @@ -111,7 +111,7 @@ void e2d::Game::resume() { if (_paused && !_ended) { - _last = _now = steady_clock::now(); + _last = _now = Time::now(); Timer::getInstance()->updateTime(); ActionManager::getInstance()->updateTime(); } @@ -141,9 +141,9 @@ e2d::Config* e2d::Game::getConfig() return _config; } -double e2d::Game::getTotalTime() const +e2d::Duration e2d::Game::getTotalDuration() const { - return duration_cast(steady_clock::now() - _start).count() / 1000.0 / 1000.0; + return std::move(_now - _start); } void e2d::Game::quit() diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index 22c3b9fe..f1fcd229 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -161,11 +161,12 @@ void e2d::Renderer::_renderFps() { ++_renderTimes; - double fDelay = Game::getInstance()->getTotalTime() - _lastRenderTime; + double duration = Game::getInstance()->getTotalDuration().seconds(); + double fDelay = duration - _lastRenderTime; if (fDelay >= 0.1) { _fpsText = String::format(L"FPS: %.1lf", (1 / fDelay) * _renderTimes); - _lastRenderTime = Game::getInstance()->getTotalTime(); + _lastRenderTime = duration; _renderTimes = 0; } diff --git a/core/Common/Duration.cpp b/core/Common/Duration.cpp index 04f210b4..74d76e7b 100644 --- a/core/Common/Duration.cpp +++ b/core/Common/Duration.cpp @@ -3,15 +3,70 @@ using namespace std::chrono; e2d::Duration::Duration() + : ms() { } -e2d::Duration e2d::Duration::operator+(Duration const & size) const +e2d::Duration::Duration(int ms) + : ms(ms) { - return Duration(); } -e2d::Duration e2d::Duration::operator-(Duration const & size) const +int e2d::Duration::milliseconds() const { - return Duration(); + return static_cast(ms.count()); +} + +double e2d::Duration::seconds() const +{ + return ms.count() / 1000.0; +} + +double e2d::Duration::minutes() const +{ + return ms.count() / 1000.0 / 60.0; +} + +bool e2d::Duration::operator==(const Duration & other) const +{ + return ms == other.ms; +} + +bool e2d::Duration::operator!=(const Duration & other) const +{ + return ms != other.ms; +} + +bool e2d::Duration::operator>(const Duration & other) const +{ + return ms > other.ms; +} + +bool e2d::Duration::operator>=(const Duration & other) const +{ + return ms >= other.ms; +} + +bool e2d::Duration::operator<(const Duration & other) const +{ + return ms < other.ms; +} + +bool e2d::Duration::operator<=(const Duration & other) const +{ + return ms <= other.ms; +} + +e2d::Duration e2d::Duration::operator+(Duration const & other) const +{ + Duration result; + result.ms = ms + other.ms; + return std::move(result); +} + +e2d::Duration e2d::Duration::operator-(Duration const & other) const +{ + Duration result; + result.ms = ms - other.ms; + return std::move(result); } diff --git a/core/Common/Time.cpp b/core/Common/Time.cpp index 9016cab6..64855ac2 100644 --- a/core/Common/Time.cpp +++ b/core/Common/Time.cpp @@ -7,12 +7,28 @@ e2d::Time::Time() { } -e2d::Time e2d::Time::operator+(Duration const & size) const +time_t e2d::Time::getTimeStamp() const { - return Time(); + auto now = time_point_cast(time).time_since_epoch(); + return static_cast(now.count()); } -e2d::Duration e2d::Time::operator-(Time const & size) const +e2d::Time e2d::Time::operator+(Duration const & other) const { - return Duration(); + Time result; + result.time = time - other.ms; + return std::move(result); +} + +e2d::Duration e2d::Time::operator-(Time const & other) const +{ + int ms = static_cast(duration_cast(time - other.time).count()); + return std::move(Duration(ms)); +} + +e2d::Time e2d::Time::now() +{ + Time now; + now.time = steady_clock::now(); + return std::move(now); } diff --git a/core/Tool/Task.cpp b/core/Tool/Task.cpp index d1d6de0c..b6ec6ac9 100644 --- a/core/Tool/Task.cpp +++ b/core/Tool/Task.cpp @@ -60,7 +60,7 @@ bool e2d::Task::isReady() const { return true; } - if ((Game::getInstance()->getTotalTime() - _lastTime) >= _delay) + if ((Game::getInstance()->getTotalDuration().seconds() - _lastTime) >= _delay) { return true; } @@ -80,5 +80,5 @@ e2d::String e2d::Task::getName() const void e2d::Task::updateTime() { - _lastTime = Game::getInstance()->getTotalTime(); + _lastTime = Game::getInstance()->getTotalDuration().seconds(); } \ No newline at end of file diff --git a/core/Transition/Transition.cpp b/core/Transition/Transition.cpp index f528f2e1..a4e50f58 100644 --- a/core/Transition/Transition.cpp +++ b/core/Transition/Transition.cpp @@ -4,7 +4,7 @@ e2d::Transition::Transition(double duration) : _end(false) - , _last(0) + , _last() , _delta(0) , _outScene(nullptr) , _inScene(nullptr) @@ -45,7 +45,7 @@ void e2d::Transition::_init(Scene * prev, Scene * next) throw SystemException(L"场景过渡动画图层创建失败"); } - _last = Game::getInstance()->getTotalTime(); + _last = Game::getInstance()->getTotalDuration(); _outScene = prev; _inScene = next; GC::retain(_outScene); @@ -64,7 +64,8 @@ void e2d::Transition::_update() } else { - _delta = std::min((Game::getInstance()->getTotalTime() - _last) / _duration, 1.0); + _delta = (Game::getInstance()->getTotalDuration() - _last).seconds() / _duration; + _delta = std::min(_delta, 1.0); } this->_updateCustom(); diff --git a/core/e2dbase.h b/core/e2dbase.h index 011b4456..f076a327 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -46,7 +46,8 @@ public: // 获取游戏配置 Config* getConfig(); - double getTotalTime() const; + // 获取游戏总时长 + Duration getTotalDuration() const; private: Game(); @@ -56,12 +57,12 @@ private: E2D_DISABLE_COPY(Game); private: - bool _ended; - bool _paused; - Config* _config; - std::chrono::steady_clock::time_point _start; - std::chrono::steady_clock::time_point _now; - std::chrono::steady_clock::time_point _last; + bool _ended; + bool _paused; + Config* _config; + Time _start; + Time _now; + Time _last; static Game * _instance; }; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 3c182037..456f673c 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -410,11 +410,31 @@ class Duration public: Duration(); - Duration operator + (Duration const & size) const; - Duration operator - (Duration const & size) const; + explicit Duration( + int ms + ); -protected: - std::chrono::milliseconds _duration; + // 获取毫秒数 + int milliseconds() const; + + // 获取秒数 + double seconds() const; + + // 获取分钟数 + double minutes() const; + + bool operator== (const Duration &) const; + bool operator!= (const Duration &) const; + bool operator> (const Duration &) const; + bool operator>= (const Duration &) const; + bool operator< (const Duration &) const; + bool operator<= (const Duration &) const; + + Duration operator + (Duration const & other) const; + Duration operator - (Duration const & other) const; + +public: + std::chrono::milliseconds ms; }; @@ -424,11 +444,19 @@ class Time public: Time(); - Time operator + (Duration const & size) const; - Duration operator - (Time const & size) const; + // 获取时间戳 + time_t getTimeStamp() const; -protected: - std::chrono::steady_clock::time_point _time; + // 计算时间间隔后的时间点 + Time operator + (Duration const & other) const; + // 计算两时间点的时间间隔 + Duration operator - (Time const & other) const; + + // 获取当前时间 + static Time now(); + +public: + std::chrono::steady_clock::time_point time; }; diff --git a/core/e2dtransition.h b/core/e2dtransition.h index 22a8d5a0..7465613c 100644 --- a/core/e2dtransition.h +++ b/core/e2dtransition.h @@ -46,9 +46,9 @@ protected: protected: bool _end; - double _last; double _duration; double _delta; + Duration _last; Size _windowSize; Scene * _outScene; Scene * _inScene;