124 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			124 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\ebase.h"
 | |||
|  | 
 | |||
|  | // <20><>һ֡<D2BB>뵱ǰ֡<C7B0><D6A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | static int s_nInterval = 0; | |||
|  | // <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
 | |||
|  | static float s_fTotalTime = 0; | |||
|  | 
 | |||
|  | 
 | |||
|  | float e2d::ETime::getTotalTime() | |||
|  | { | |||
|  | 	return s_fTotalTime; | |||
|  | } | |||
|  | 
 | |||
|  | int e2d::ETime::getDeltaTime() | |||
|  | { | |||
|  | 	return s_nInterval; | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | #if _MSC_VER > 1600
 | |||
|  | 
 | |||
|  | #include <thread>
 | |||
|  | #include <chrono>
 | |||
|  | using namespace std::chrono; | |||
|  | 
 | |||
|  | 
 | |||
|  | // <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
 | |||
|  | static steady_clock::time_point s_tStart; | |||
|  | // <20><>ǰʱ<C7B0><CAB1>
 | |||
|  | static steady_clock::time_point s_tNow; | |||
|  | // <20><>һ֡ˢ<D6A1><CBA2>ʱ<EFBFBD><CAB1>
 | |||
|  | static steady_clock::time_point s_tLast; | |||
|  | 
 | |||
|  | 
 | |||
|  | void e2d::ETime::__init() | |||
|  | { | |||
|  | 	s_tStart = s_tLast = s_tNow = steady_clock::now(); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__uninit() | |||
|  | { | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__updateNow() | |||
|  | { | |||
|  | 	s_tNow = steady_clock::now(); | |||
|  | 	s_fTotalTime = static_cast<float>(duration_cast<milliseconds>(s_tNow - s_tStart).count()) / 1000.0f; | |||
|  | 	s_nInterval = static_cast<int>(duration_cast<milliseconds>(s_tNow - s_tLast).count()); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__updateLast() | |||
|  | { | |||
|  | 	s_tLast = s_tNow; | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__sleep() | |||
|  | { | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
 | |||
|  | 	int nWaitMS = 16 - s_nInterval; | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3>ͷ<EFBFBD> CPU ռ<><D5BC>
 | |||
|  | 	if (nWaitMS > 1) | |||
|  | 	{ | |||
|  | 		std::this_thread::sleep_for(milliseconds(nWaitMS)); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | #else
 | |||
|  | 
 | |||
|  | 
 | |||
|  | #include <mmsystem.h>
 | |||
|  | #pragma comment(lib, "winmm.lib")
 | |||
|  | 
 | |||
|  | // ʱ<><CAB1>Ƶ<EFBFBD><C6B5>
 | |||
|  | static LARGE_INTEGER s_tFreq; | |||
|  | // <20><>ǰʱ<C7B0><CAB1>
 | |||
|  | static LARGE_INTEGER s_tNow; | |||
|  | // <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
 | |||
|  | static LARGE_INTEGER s_tStart; | |||
|  | // <20><>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
 | |||
|  | static LARGE_INTEGER s_tLast; | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | void e2d::ETime::__init() | |||
|  | { | |||
|  | 	::timeBeginPeriod(1);					// <20><EFBFBD>ʱ<EFBFBD>侫<EFBFBD><E4BEAB>
 | |||
|  | 	::QueryPerformanceFrequency(&s_tFreq);	// <20><>ȡʱ<C8A1><CAB1>Ƶ<EFBFBD><C6B5>
 | |||
|  | 	::QueryPerformanceCounter(&s_tNow);		// ˢ<>µ<EFBFBD>ǰʱ<C7B0><CAB1>
 | |||
|  | 	s_tStart = s_tLast = s_tNow; | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__uninit() | |||
|  | { | |||
|  | 	::timeEndPeriod(1);	// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD>侫<EFBFBD><E4BEAB>
 | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__updateNow() | |||
|  | { | |||
|  | 	::QueryPerformanceCounter(&s_tNow); | |||
|  | 	s_fTotalTime = static_cast<float>(s_tNow.QuadPart - s_tStart.QuadPart) / s_tFreq.QuadPart; | |||
|  | 	s_nInterval = static_cast<int>((s_tNow.QuadPart - s_tLast.QuadPart) * 1000LL / s_tFreq.QuadPart); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__updateLast() | |||
|  | { | |||
|  | 	s_tLast = s_tNow; | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::ETime::__sleep() | |||
|  | { | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
 | |||
|  | 	int nWaitMS = 16 - s_nInterval; | |||
|  | 	// <20><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3>ͷ<EFBFBD> CPU ռ<><D5BC>
 | |||
|  | 	if (nWaitMS > 1) | |||
|  | 	{ | |||
|  | 		::Sleep(nWaitMS); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | #endif // _MSC_VER > 1600
 |