高精度定时器

来源:百度知道 编辑:UC知道 时间:2024/06/21 18:24:22
读了一段代码,没看懂的地方用问号标记了,请行家解释一下。

#ifndef __TIMER_H_INCLUDED__
#define __TIMER_H_INCLUDED__

#include <windows.h>

class CHiResTimer
{
public:
CHiResTimer() {}
~CHiResTimer() {}

/*
Init()

If the hi-res timer is present, the tick rate is stored and the function
returns true. Otherwise, the function returns false, and the timer should
not be used.
*/
bool Init()
{
if (!QueryPerformanceFrequency(&m_ticksPerSecond))
{
// system doesn't support hi-res timer
return false;
}
else
{
QueryPerformanceCounter(&m_startTime);
return true;
}
} // end Init()

float GetElapsedSeconds(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;

QueryPerformanceCounter(¤tTime);

float seconds = (

很困难吗?很简单的!
// delay to maintain a constant frame rate 高精度延时
do {
QueryPerformanceCounter(¤tTime); //查询当前精确时钟
fps = (float)m_ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart));
//用64位大整数(保证到微秒级别,甚至兼容纳秒级别)求出目前距离0时刻经过的时间
} while (fps > (float)targetFPS); //一旦超时,才停止循环