To measure framerate you need two counts:
- How many frames (not draw calls) have passed, and,
- How much time has passed.
Your framerate is therefore calculated as:
frames / time
There are a few subtle complexities to this.
First thing is that you need a good, accurate, high resolution timer. You don't say what platform you're on so I'm not going to make any assumptions beyond:
- You have such a timer available, and,
- You're not using Sleep calls to control framerate (if you are, stop it now).
Let's say that you're storing the time in a variable called "timepassed". It's a double and it starts at 0 and counts the time (in seconds) since the game started. Our framerate counter begins like this:
static int frames = 0; static double starttime = 0; static bool first = TRUE; static float fps = 0.0f;
The first thing we do is check if this is the first time we've passed through the counter and set some stuff up:
if (first) { frames = 0; starttime = timepassed; first = FALSE; return; }
Next we increment the number of frames that have passed; I'm assuming here that you're updating the framerate counter once per frame only:
frames++;
And here we evaluate the actual FPS number.
if (timepassed - starttime > 0.25 && frames > 10) { fps = (double) frames / (timepassed - starttime); starttime = timepassed; frames = 0; }
Here we update the FPS count every 0.25 seconds. This isn't strictly speaking necessary but otherwise if you have a variable framerate you're going to be getting numbers wildly flashing on your screen and you won't be able to read them too good. (It's also important that timepassed - starttime is greater than 0.) After calculating the FPS we then update our static variables for the next pass through.
This is prety much my standard FPS counter (it's C heritage shows in some of the code) and I've cross-checked it with the values provided by FRAPS and found it highly accurate.