完善Time功能
This commit is contained in:
		
							parent
							
								
									35ed2427c1
								
							
						
					
					
						commit
						180d2067fa
					
				|  | @ -3,8 +3,6 @@ | ||||||
| #include "..\e2dtool.h" | #include "..\e2dtool.h" | ||||||
| #include <thread> | #include <thread> | ||||||
| 
 | 
 | ||||||
| using namespace std::chrono; |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| e2d::Game * e2d::Game::_instance = nullptr; | e2d::Game * e2d::Game::_instance = nullptr; | ||||||
| 
 | 
 | ||||||
|  | @ -42,13 +40,8 @@ void e2d::Game::destroyInstance() | ||||||
| 
 | 
 | ||||||
| void e2d::Game::start() | void e2d::Game::start() | ||||||
| { | { | ||||||
| 	HWND hWnd = Window::getInstance()->getHWnd(); |  | ||||||
| 	if (hWnd == nullptr) |  | ||||||
| 	{ |  | ||||||
| 		throw SystemException(L"无法创建窗口"); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// ÏÔʾ´°¿Ú
 | 	// ÏÔʾ´°¿Ú
 | ||||||
|  | 	HWND hWnd = Window::getInstance()->getHWnd(); | ||||||
| 	::ShowWindow(hWnd, SW_SHOWNORMAL); | 	::ShowWindow(hWnd, SW_SHOWNORMAL); | ||||||
| 	::UpdateWindow(hWnd); | 	::UpdateWindow(hWnd); | ||||||
| 
 | 
 | ||||||
|  | @ -70,21 +63,6 @@ void e2d::Game::start() | ||||||
| 		if (_config.isVSyncEnabled() || _frameInterval < interval) | 		if (_config.isVSyncEnabled() || _frameInterval < interval) | ||||||
| 		{ | 		{ | ||||||
| 			_last = _now; | 			_last = _now; | ||||||
| 			__update(); |  | ||||||
| 		} |  | ||||||
| 		else |  | ||||||
| 		{ |  | ||||||
| 			wait = (_frameInterval - interval).milliseconds() - 1; |  | ||||||
| 			if (wait > 1) |  | ||||||
| 			{ |  | ||||||
| 				std::this_thread::sleep_for(milliseconds(wait)); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| void e2d::Game::__update() |  | ||||||
| { |  | ||||||
| 			Input::getInstance()->update(); | 			Input::getInstance()->update(); | ||||||
| 			Timer::getInstance()->update(); | 			Timer::getInstance()->update(); | ||||||
| 			ActionManager::getInstance()->update(); | 			ActionManager::getInstance()->update(); | ||||||
|  | @ -93,6 +71,12 @@ void e2d::Game::__update() | ||||||
| 			Window::getInstance()->poll(); | 			Window::getInstance()->poll(); | ||||||
| 			GC::getInstance()->flush(); | 			GC::getInstance()->flush(); | ||||||
| 		} | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			std::this_thread::yield(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| void e2d::Game::pause() | void e2d::Game::pause() | ||||||
| { | { | ||||||
|  | @ -133,7 +117,6 @@ void e2d::Game::setConfig(const Config& config) | ||||||
| 	if (_config.isVSyncEnabled() != config.isVSyncEnabled()) | 	if (_config.isVSyncEnabled() != config.isVSyncEnabled()) | ||||||
| 	{ | 	{ | ||||||
| 		Renderer::getInstance()->discardDeviceResources(); | 		Renderer::getInstance()->discardDeviceResources(); | ||||||
| 		_last = _now; |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	_config = config; | 	_config = config; | ||||||
|  |  | ||||||
|  | @ -66,3 +66,15 @@ e2d::Duration e2d::Duration::operator-(Duration const & other) const | ||||||
| { | { | ||||||
| 	return std::move(Duration(_ms - other._ms)); | 	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); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -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) | 	: _timePoint(time) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| time_t e2d::Time::getTimeStamp() const | time_t e2d::Time::getTimeStamp() const | ||||||
| { | { | ||||||
| 	auto& now = time_point_cast<milliseconds>(_timePoint).time_since_epoch(); | 	auto& duration = time_point_cast<milliseconds>(_timePoint).time_since_epoch(); | ||||||
| 	return static_cast<time_t>(now.count()); | 	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 | e2d::Time e2d::Time::operator+(Duration const & other) const | ||||||
|  | @ -29,6 +34,17 @@ e2d::Time & e2d::Time::operator+=(Duration const & other) | ||||||
| 	return (*this); | 	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 | e2d::Duration e2d::Time::operator-(Time const & other) const | ||||||
| { | { | ||||||
| 	auto& ms = duration_cast<milliseconds>(_timePoint - other._timePoint); | 	auto& ms = duration_cast<milliseconds>(_timePoint - other._timePoint); | ||||||
|  |  | ||||||
|  | @ -148,8 +148,6 @@ private: | ||||||
| 
 | 
 | ||||||
| 	E2D_DISABLE_COPY(Game); | 	E2D_DISABLE_COPY(Game); | ||||||
| 
 | 
 | ||||||
| 	void __update(); |  | ||||||
| 
 |  | ||||||
| private: | private: | ||||||
| 	bool		_quit; | 	bool		_quit; | ||||||
| 	bool		_paused; | 	bool		_paused; | ||||||
|  |  | ||||||
|  | @ -434,8 +434,11 @@ public: | ||||||
| 	bool operator< (const Duration &) const; | 	bool operator< (const Duration &) const; | ||||||
| 	bool operator<= (const Duration &) const; | 	bool operator<= (const Duration &) const; | ||||||
| 
 | 
 | ||||||
| 	Duration operator + (Duration const & other) const; | 	Duration operator + (Duration const &) const; | ||||||
| 	Duration operator - (Duration const & other) const; | 	Duration operator - (Duration const &) const; | ||||||
|  | 
 | ||||||
|  | 	Duration& operator += (Duration const &); | ||||||
|  | 	Duration& operator -= (Duration const &); | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
| 	std::chrono::milliseconds _ms; | 	std::chrono::milliseconds _ms; | ||||||
|  | @ -455,10 +458,16 @@ public: | ||||||
| 	// 获取时间戳
 | 	// 获取时间戳
 | ||||||
| 	time_t getTimeStamp() const; | 	time_t getTimeStamp() const; | ||||||
| 
 | 
 | ||||||
| 	Time operator + (Duration const & other) const; | 	// ÊÇ·ñÊÇ
 | ||||||
| 	Time& operator += (Duration const &); | 	bool isZero() const; | ||||||
| 
 | 
 | ||||||
| 	Duration operator - (Time const & other) const; | 	Time operator + (Duration const &) const; | ||||||
|  | 	Time operator - (Duration const &) const; | ||||||
|  | 
 | ||||||
|  | 	Time& operator += (Duration const &); | ||||||
|  | 	Time& operator -= (Duration const &); | ||||||
|  | 	 | ||||||
|  | 	Duration operator - (Time const &) const; | ||||||
| 
 | 
 | ||||||
| 	// 获取当前时间
 | 	// 获取当前时间
 | ||||||
| 	static Time now(); | 	static Time now(); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue