diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index be9d983d..33262ea7 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -3,8 +3,6 @@ #include "..\e2dtool.h" #include -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; diff --git a/core/Common/Duration.cpp b/core/Common/Duration.cpp index ef691efe..0e672f65 100644 --- a/core/Common/Duration.cpp +++ b/core/Common/Duration.cpp @@ -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); +} diff --git a/core/Common/Time.cpp b/core/Common/Time.cpp index a5c03744..6a2bfc4d 100644 --- a/core/Common/Time.cpp +++ b/core/Common/Time.cpp @@ -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(_timePoint).time_since_epoch(); - return static_cast(now.count()); + auto& duration = time_point_cast(_timePoint).time_since_epoch(); + return static_cast(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(_timePoint - other._timePoint); diff --git a/core/e2dbase.h b/core/e2dbase.h index c7df7890..223f0710 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -148,8 +148,6 @@ private: E2D_DISABLE_COPY(Game); - void __update(); - private: bool _quit; bool _paused; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 66ad39c5..7f5641f0 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -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();