Handmade Hero»Forums»Code
Bryan Davis
3 posts
Simple FPS Counter for HandmadeHero
Edited by Bryan Davis on
Hey guys, I was looking into the performance impact of passing by reference vs passing by value. I know it's not necessary right now, but I was curious. The easiest way for me to do this was informally making a simple FPS counter. I have it display to the title bar rather than going through the process of putting a child window on the screen to display the text.

Windows implementation, as I haven't started cross platforming yet:

1
#include <stdio.h>


Before the GlobalRunning loop, declare some variables to hold frames and times. Initialize to 0.

1
2
3
4
5
DWORD Frames = 0;
DWORD CurrentTime = 0;
DWORD PreviousTime = 0;
DWORD ElapsedTime = 0;
TCHAR FPS[32] = {};


Then inside the GlobalRunning loop, after doing everything else in it (or before doing everything, just not in the middle of the loop), update the frames and do calculations to see if it's been a second. If it has, create a string that labels the FPS and pass it to the window.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Frames++;
CurrentTime = timeGetTime(); // More reliable than GetTickCount(); [1]
ElapsedTime = CurrentTime - PreviousTime;

if (ElapsedTime >= 1000) // In milliseconds, so 1000 == 1 second
{
  sprintf(FPS, "FPS: %u\n", (UINT)(Frames * 1000.0 / ElapsedTime));
  Frames = 0;
  PreviousTime = CurrentTime;
}

SetWindowText(Window, TEXT(FPS));


Lastly, you need to add winmm.lib as a linked library to your compile line.


[1]: http://blogs.msdn.com/b/larryoste...gettickcount-and-timegettime.aspx

NOTE: I am not fully caught up on the episodes, so if this has been implemented in the main stream then I apologize.

PS: First post, hello everyone.
Mārtiņš Možeiko
2562 posts / 2 projects
Simple FPS Counter for HandmadeHero
Edited by Mārtiņš Možeiko on
Yes, FPS was implemented in episode 10 with QueryPerformanceCounter, not timeGetTime.

To measure performance of something it should be measured in seconds (mili/micro/nano) or cycles. Not frames/second. If you ask why, it is answered in episode 10.
Andrew Chronister
194 posts / 1 project
Developer, administrator, and style wrangler
Simple FPS Counter for HandmadeHero
Edited by Andrew Chronister on
mmozeiko

To measure performance of something it should be measured in seconds (mili/micro/nano) or cycles. Not frames/second. If you ask why, it is answered in episode 10.


Just to qualify that a little bit, it's Casey's (and probably many others') opinion that milliseconds per frame is a more useful measurement to a game developer than frames per second. This is because a step from 32 fps to 30fps is a much larger amount of time than a step from 62 fps to 60fps, even though its the same difference in literal numbers. On the other hand, a change from 32ms to 30ms is exactly the same as a change from 18ms to 16ms for your code to run, and gives you a much better idea of when you actually manage to improve something or make something worse.

Frames Per Second is more of a marketing/general-public way of looking at things, and isn't necessarily as useful during the development process of the game.