我希望我的小游戏在标题中显示FPS,但它不应该重新计算每个帧和单帧的FPS。我想每秒刷新一次FPS计数器,所以我尝试使用SetTimer
。问题是只要我不移动鼠标或按住键,定时器就会起作用。据我所知WM_TIMER
是一个低优先级的消息,所以它最后被处理。 有没有办法在任何其他用户输入消息之前处理WM_TIMER
消息,或者至少是另一种创建第二个滴答计时器的方式?
我还尝试使用TimerProc
而不是等待WM_TIMER
,但这也没有用。
答案 0 :(得分:1)
使用单独的后台线程如何测量它的简短示例。
int iCount;
int iFramesPerSec;
std::mutex mtx;
// this function runs in a separate thread
void frameCount()
{
while(true){
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lg{mtx}; // synchronize access
iFramesPerSec = iCount; // frames per second during last second that passed
iCount = 0;
}
}
// inside window procedure
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
...
std::lock_guard<std::mutex> lg{mtx}; // synchronize access
++iCount;
EndPaint(hwnd, &ps);
return 0;