The first thing you'll need to do is declare variables to store time and frame data:
// Variables used to calculate frames per second: DWORD dwFrames; DWORD dwCurrentTime; DWORD dwLastUpdateTime; DWORD dwElapsedTime; TCHAR szFPS[32];
Next, it's very important that you initialize the variables:
// Zero out the frames per second variables: dwFrames = 0; dwCurrentTime = 0; dwLastUpdateTime = 0; dwElapsedTime = 0; szFPS[0] = '\0';
And finally, here's the code that makes the magic happen!
// Calculate the number of frames per one second:
dwFrames++;
dwCurrentTime = GetTickCount(); // Even better to use timeGetTime()
dwElapsedTime = dwCurrentTime - dwLastUpdateTime;
if(dwElapsedTime >= 1000)
{
wsprintf(szFPS, _T("FPS = %u"), (UINT)(dwFrames * 1000.0 / dwElapsedTime));
dwFrames = 0;
dwLastUpdateTime = dwCurrentTime;
}
// Write the FPS onto the Memory DC.
TextOut(hdcMemory, 0, rClient.bottom - 20, szFPS, lstrlen(szFPS));
Naturally, you'll only want to declare and initialize the variables once. You'll want to put the last snippet of code in the function where you're printing to the screen (probably your render function). And that's it!
Happy coding





MultiQuote


|