From e3cce895526dd742d0fb274506b2acbacf9d4061 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 22 Jul 2018 20:09:14 +0800 Subject: [PATCH] =?UTF-8?q?Duration=E5=92=8CTime=E7=BB=86=E8=8A=82?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Game.cpp | 16 ++++++++-------- core/Common/Duration.cpp | 38 +++++++++++++++++--------------------- core/Common/Time.cpp | 17 +++++++++-------- core/e2dcommon.h | 19 ++++++++++++------- 4 files changed, 46 insertions(+), 44 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 2b1a79a6..4a13ac1f 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -82,14 +82,14 @@ void e2d::Game::start() { _last = _now; - input->update(); // 获取用户输入 - timer->update(); // 更新定时器 - actionManager->update(); // 更新动作管理器 - sceneManager->update(); // 更新场景内容 - _config->_update(); // 更新游戏配置 - renderer->render(); // 渲染游戏画面 - window->poll(); // 处理窗口消息 - GC::flush(); // 刷新内存池 + input->update(); + timer->update(); + actionManager->update(); + sceneManager->update(); + _config->_update(); + renderer->render(); + window->poll(); + GC::flush(); } else { diff --git a/core/Common/Duration.cpp b/core/Common/Duration.cpp index 74d76e7b..375b4754 100644 --- a/core/Common/Duration.cpp +++ b/core/Common/Duration.cpp @@ -3,70 +3,66 @@ using namespace std::chrono; e2d::Duration::Duration() - : ms() + : _ms() { } e2d::Duration::Duration(int ms) - : ms(ms) + : _ms(ms) +{ +} + +e2d::Duration::Duration(std::chrono::milliseconds ms) + : _ms(ms) { } int e2d::Duration::milliseconds() const { - return static_cast(ms.count()); + 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; + return _ms.count() / 1000.0; } bool e2d::Duration::operator==(const Duration & other) const { - return ms == other.ms; + return _ms == other._ms; } bool e2d::Duration::operator!=(const Duration & other) const { - return ms != other.ms; + return _ms != other._ms; } bool e2d::Duration::operator>(const Duration & other) const { - return ms > other.ms; + return _ms > other._ms; } bool e2d::Duration::operator>=(const Duration & other) const { - return ms >= other.ms; + return _ms >= other._ms; } bool e2d::Duration::operator<(const Duration & other) const { - return ms < other.ms; + return _ms < other._ms; } bool e2d::Duration::operator<=(const Duration & other) const { - return ms <= other.ms; + return _ms <= other._ms; } e2d::Duration e2d::Duration::operator+(Duration const & other) const { - Duration result; - result.ms = ms + other.ms; - return std::move(result); + return std::move(Duration(_ms + other._ms)); } e2d::Duration e2d::Duration::operator-(Duration const & other) const { - Duration result; - result.ms = ms - other.ms; - return std::move(result); + return std::move(Duration(_ms - other._ms)); } diff --git a/core/Common/Time.cpp b/core/Common/Time.cpp index 64855ac2..8ccbdc0f 100644 --- a/core/Common/Time.cpp +++ b/core/Common/Time.cpp @@ -7,28 +7,29 @@ e2d::Time::Time() { } +e2d::Time::Time(std::chrono::steady_clock::time_point time) + : _timePoint(time) +{ +} + time_t e2d::Time::getTimeStamp() const { - auto now = time_point_cast(time).time_since_epoch(); + auto& now = time_point_cast(_timePoint).time_since_epoch(); return static_cast(now.count()); } e2d::Time e2d::Time::operator+(Duration const & other) const { - Time result; - result.time = time - other.ms; - return std::move(result); + return std::move(Time(_timePoint - milliseconds(other.milliseconds()))); } e2d::Duration e2d::Time::operator-(Time const & other) const { - int ms = static_cast(duration_cast(time - other.time).count()); + auto& ms = duration_cast(_timePoint - other._timePoint); return std::move(Duration(ms)); } e2d::Time e2d::Time::now() { - Time now; - now.time = steady_clock::now(); - return std::move(now); + return std::move(Time(steady_clock::now())); } diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 456f673c..fb58e1a4 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -414,15 +414,16 @@ public: int ms ); + explicit Duration( + std::chrono::milliseconds ms + ); + // 获取毫秒数 int milliseconds() const; // 获取秒数 double seconds() const; - // 获取分钟数 - double minutes() const; - bool operator== (const Duration &) const; bool operator!= (const Duration &) const; bool operator> (const Duration &) const; @@ -433,8 +434,8 @@ public: Duration operator + (Duration const & other) const; Duration operator - (Duration const & other) const; -public: - std::chrono::milliseconds ms; +protected: + std::chrono::milliseconds _ms; }; @@ -444,6 +445,10 @@ class Time public: Time(); + explicit Time( + std::chrono::steady_clock::time_point time + ); + // 获取时间戳 time_t getTimeStamp() const; @@ -455,8 +460,8 @@ public: // 获取当前时间 static Time now(); -public: - std::chrono::steady_clock::time_point time; +protected: + std::chrono::steady_clock::time_point _timePoint; };