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;
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
{

View File

@ -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<int>(ms.count());
return static_cast<int>(_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));
}

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
{
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());
}
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<int>(duration_cast<milliseconds>(time - other.time).count());
auto& ms = duration_cast<milliseconds>(_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()));
}

View File

@ -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;
};