Page 1 of 1

Frames Per Second Counter Track your framerate! Used in Win32 apps. Rate Topic: -----

#1 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Posted 23 January 2009 - 05:33 PM

Printing out your frames/second is a three step process.

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 :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1