#define FRAME_RATE 60
int g_fps = 0; // FPS帧率值
char g_fpsStr[16] = {0}; // 存放帧率值
int g_fpsInt = 0;
float g_time = 0.0f; // 系统运行时间
float g_lastTime = 0.0f; // 持续的时间
int freq =1000/FRAME_RATE; //频率
// 计算帧率
void GetFPS()
{
// Get the second in millisecond then multiply it to convert to seconds.
// g_time获取操作系统启动到现在所经过的毫秒数,乘以0.001f得到秒数
g_time = GetTickCount() * 0.001f;
// If time - last time is > than 1, save fpt.
// 持续时间是否大于1秒
if(g_time - g_lastTime > 1.0f)
{
// Record last time.
// 记录新的持续时间
g_lastTime = g_time;
// Save FPS.
// 把整数g_fps格式化为一个字符串保存在g_fpsStr中,并输出该字符串
sprintf(g_fpsStr, "FPS: %d", g_fps);
g_fpsInt = g_fps;
// Reset the FPS counter.
// 重置帧率值为0
g_fps = 0;
}
else
{
// Add to the frames per second.
// 帧率值递增
g_fps++;
if(g_fps > (g_time - g_lastTime) * FRAME_RATE)
Sleep(freq);
}
}