Cubi73
Cubi73

Reputation: 1951

WM_TIMER with higher priority?

I want my little game show FPS in the title, but it shouldn't recalculate the FPS for each and single frame. I want to refresh the FPS counter only every second, so I tried to use SetTimer. The problem is that the timer only works as long as I don't move the mouse or hold down a key. As far as I know WM_TIMER is a low-priority message, so it gets processed last. Is there a way to process WM_TIMER messages before any other user input message or at least another way of creating a secondly ticking timer?

I also tried to use a TimerProc instead of waiting for a WM_TIMER, but this didn't work either.

Upvotes: 1

Views: 1104

Answers (1)

jensa
jensa

Reputation: 2890

Short example of how it could be measured using a separate background thread.

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;

Upvotes: 1

Related Questions