Duration和Time细节修复

This commit is contained in:
Nomango 2018-07-22 20:09:14 +08:00
parent c3ebe2545a
commit e3cce89552
4 changed files with 46 additions and 44 deletions

View File

@ -82,14 +82,14 @@ void e2d::Game::start()
{ {
_last = _now; _last = _now;
input->update(); // 获取用户输入 input->update();
timer->update(); // 更新定时器 timer->update();
actionManager->update(); // 更新动作管理器 actionManager->update();
sceneManager->update(); // 更新场景内容 sceneManager->update();
_config->_update(); // 更新游戏配置 _config->_update();
renderer->render(); // 渲染游戏画面 renderer->render();
window->poll(); // 处理窗口消息 window->poll();
GC::flush(); // 刷新内存池 GC::flush();
} }
else else
{ {

View File

@ -3,70 +3,66 @@
using namespace std::chrono; using namespace std::chrono;
e2d::Duration::Duration() e2d::Duration::Duration()
: ms() : _ms()
{ {
} }
e2d::Duration::Duration(int ms) e2d::Duration::Duration(int ms)
: ms(ms) : _ms(ms)
{
}
e2d::Duration::Duration(std::chrono::milliseconds ms)
: _ms(ms)
{ {
} }
int e2d::Duration::milliseconds() const int e2d::Duration::milliseconds() const
{ {
return static_cast<int>(ms.count()); return static_cast<int>(_ms.count());
} }
double e2d::Duration::seconds() const double e2d::Duration::seconds() const
{ {
return ms.count() / 1000.0; 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 bool e2d::Duration::operator==(const Duration & other) const
{ {
return ms == other.ms; return _ms == other._ms;
} }
bool e2d::Duration::operator!=(const Duration & other) const bool e2d::Duration::operator!=(const Duration & other) const
{ {
return ms != other.ms; return _ms != other._ms;
} }
bool e2d::Duration::operator>(const Duration & other) const bool e2d::Duration::operator>(const Duration & other) const
{ {
return ms > other.ms; return _ms > other._ms;
} }
bool e2d::Duration::operator>=(const Duration & other) const bool e2d::Duration::operator>=(const Duration & other) const
{ {
return ms >= other.ms; return _ms >= other._ms;
} }
bool e2d::Duration::operator<(const Duration & other) const bool e2d::Duration::operator<(const Duration & other) const
{ {
return ms < other.ms; return _ms < other._ms;
} }
bool e2d::Duration::operator<=(const Duration & other) const 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 e2d::Duration e2d::Duration::operator+(Duration const & other) const
{ {
Duration result; return std::move(Duration(_ms + other._ms));
result.ms = ms + other.ms;
return std::move(result);
} }
e2d::Duration e2d::Duration::operator-(Duration const & other) const e2d::Duration e2d::Duration::operator-(Duration const & other) const
{ {
Duration result; return std::move(Duration(_ms - other._ms));
result.ms = ms - other.ms;
return std::move(result);
} }

View File

@ -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 time_t e2d::Time::getTimeStamp() const
{ {
auto now = time_point_cast<milliseconds>(time).time_since_epoch(); auto& now = time_point_cast<milliseconds>(_timePoint).time_since_epoch();
return static_cast<time_t>(now.count()); return static_cast<time_t>(now.count());
} }
e2d::Time e2d::Time::operator+(Duration const & other) const e2d::Time e2d::Time::operator+(Duration const & other) const
{ {
Time result; return std::move(Time(_timePoint - milliseconds(other.milliseconds())));
result.time = time - other.ms;
return std::move(result);
} }
e2d::Duration e2d::Time::operator-(Time const & other) const e2d::Duration e2d::Time::operator-(Time const & other) const
{ {
int ms = static_cast<int>(duration_cast<milliseconds>(time - other.time).count()); auto& ms = duration_cast<milliseconds>(_timePoint - other._timePoint);
return std::move(Duration(ms)); return std::move(Duration(ms));
} }
e2d::Time e2d::Time::now() e2d::Time e2d::Time::now()
{ {
Time now; return std::move(Time(steady_clock::now()));
now.time = steady_clock::now();
return std::move(now);
} }

View File

@ -414,15 +414,16 @@ public:
int ms int ms
); );
explicit Duration(
std::chrono::milliseconds ms
);
// 获取毫秒数 // 获取毫秒数
int milliseconds() const; int milliseconds() const;
// 获取秒数 // 获取秒数
double seconds() 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;
@ -433,8 +434,8 @@ public:
Duration operator + (Duration const & other) const; Duration operator + (Duration const & other) const;
Duration operator - (Duration const & other) const; Duration operator - (Duration const & other) const;
public: protected:
std::chrono::milliseconds ms; std::chrono::milliseconds _ms;
}; };
@ -444,6 +445,10 @@ class Time
public: public:
Time(); Time();
explicit Time(
std::chrono::steady_clock::time_point time
);
// 获取时间戳 // 获取时间戳
time_t getTimeStamp() const; time_t getTimeStamp() const;
@ -455,8 +460,8 @@ public:
// 获取当前时间 // 获取当前时间
static Time now(); static Time now();
public: protected:
std::chrono::steady_clock::time_point time; std::chrono::steady_clock::time_point _timePoint;
}; };