WM_TIMER具有更高的优先级?

时间:2014-11-18 20:29:50

标签: c++ winapi timer

我希望我的小游戏在标题中显示FPS,但它不应该重新计算每个帧和单帧的FPS。我想每秒刷新一次FPS计数器,所以我尝试使用SetTimer。问题是只要我不移动鼠标或按住键,定时器就会起作用。据我所知WM_TIMER是一个低优先级的消息,所以它最后被处理。 有没有办法在任何其他用户输入消息之前处理WM_TIMER消息,或者至少是另一种创建第二个滴答计时器的方式?

我还尝试使用TimerProc而不是等待WM_TIMER,但这也没有用。

1 个答案:

答案 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;