完善Time功能

This commit is contained in:
Nomango 2018-07-29 01:43:15 +08:00
parent 35ed2427c1
commit 180d2067fa
5 changed files with 54 additions and 36 deletions

View File

@ -3,8 +3,6 @@
#include "..\e2dtool.h"
#include <thread>
using namespace std::chrono;
e2d::Game * e2d::Game::_instance = nullptr;
@ -42,13 +40,8 @@ void e2d::Game::destroyInstance()
void e2d::Game::start()
{
HWND hWnd = Window::getInstance()->getHWnd();
if (hWnd == nullptr)
{
throw SystemException(L"ÎÞ·¨´´½¨´°¿Ú");
}
// ÏÔʾ´°¿Ú
HWND hWnd = Window::getInstance()->getHWnd();
::ShowWindow(hWnd, SW_SHOWNORMAL);
::UpdateWindow(hWnd);
@ -61,7 +54,7 @@ void e2d::Game::start()
_quit = false;
_last = _now = Time::now();
while (!_quit)
{
_now = Time::now();
@ -70,30 +63,21 @@ void e2d::Game::start()
if (_config.isVSyncEnabled() || _frameInterval < interval)
{
_last = _now;
__update();
Input::getInstance()->update();
Timer::getInstance()->update();
ActionManager::getInstance()->update();
SceneManager::getInstance()->update();
Renderer::getInstance()->render();
Window::getInstance()->poll();
GC::getInstance()->flush();
}
else
{
wait = (_frameInterval - interval).milliseconds() - 1;
if (wait > 1)
{
std::this_thread::sleep_for(milliseconds(wait));
}
std::this_thread::yield();
}
}
}
void e2d::Game::__update()
{
Input::getInstance()->update();
Timer::getInstance()->update();
ActionManager::getInstance()->update();
SceneManager::getInstance()->update();
Renderer::getInstance()->render();
Window::getInstance()->poll();
GC::getInstance()->flush();
}
void e2d::Game::pause()
{
_paused = true;
@ -133,7 +117,6 @@ void e2d::Game::setConfig(const Config& config)
if (_config.isVSyncEnabled() != config.isVSyncEnabled())
{
Renderer::getInstance()->discardDeviceResources();
_last = _now;
}
_config = config;

View File

@ -66,3 +66,15 @@ e2d::Duration e2d::Duration::operator-(Duration const & other) const
{
return std::move(Duration(_ms - other._ms));
}
e2d::Duration & e2d::Duration::operator+=(Duration const &other)
{
_ms += other._ms;
return (*this);
}
e2d::Duration & e2d::Duration::operator-=(Duration const &other)
{
_ms -= other._ms;
return (*this);
}

View File

@ -7,15 +7,20 @@ e2d::Time::Time()
{
}
e2d::Time::Time(const std::chrono::steady_clock::time_point& time)
e2d::Time::Time(const steady_clock::time_point& time)
: _timePoint(time)
{
}
time_t e2d::Time::getTimeStamp() const
{
auto& now = time_point_cast<milliseconds>(_timePoint).time_since_epoch();
return static_cast<time_t>(now.count());
auto& duration = time_point_cast<milliseconds>(_timePoint).time_since_epoch();
return static_cast<time_t>(duration.count());
}
bool e2d::Time::isZero() const
{
return _timePoint.time_since_epoch().count() == 0LL;
}
e2d::Time e2d::Time::operator+(Duration const & other) const
@ -29,6 +34,17 @@ e2d::Time & e2d::Time::operator+=(Duration const & other)
return (*this);
}
e2d::Time e2d::Time::operator-(Duration const & other) const
{
return std::move(Time(_timePoint - milliseconds(other.milliseconds())));
}
e2d::Time & e2d::Time::operator-=(Duration const &other)
{
_timePoint -= milliseconds(other.milliseconds());
return (*this);
}
e2d::Duration e2d::Time::operator-(Time const & other) const
{
auto& ms = duration_cast<milliseconds>(_timePoint - other._timePoint);

View File

@ -148,8 +148,6 @@ private:
E2D_DISABLE_COPY(Game);
void __update();
private:
bool _quit;
bool _paused;

View File

@ -434,8 +434,11 @@ public:
bool operator< (const Duration &) const;
bool operator<= (const Duration &) const;
Duration operator + (Duration const & other) const;
Duration operator - (Duration const & other) const;
Duration operator + (Duration const &) const;
Duration operator - (Duration const &) const;
Duration& operator += (Duration const &);
Duration& operator -= (Duration const &);
protected:
std::chrono::milliseconds _ms;
@ -455,10 +458,16 @@ public:
// 获取时间戳
time_t getTimeStamp() const;
Time operator + (Duration const & other) const;
// ÊÇ·ñÊÇ
bool isZero() const;
Time operator + (Duration const &) const;
Time operator - (Duration const &) const;
Time& operator += (Duration const &);
Time& operator -= (Duration const &);
Duration operator - (Time const & other) const;
Duration operator - (Time const &) const;
// 获取当前时间
static Time now();