Game Development Reference
In-Depth Information
9.1.3 Computing the Frames Rendered Per Second
The ID3DXFont and CFont samples for this chapter compute and dis-
play the frames rendered per second (FPS). This section explains how
to compute the FPS.
First we instantiate the following three global variables:
DWORD FrameCnt; // The number of frames that have occurred.
float TimeElapsed; // The time that has elapsed so far.
float FPS; // The frames rendered per second.
We compute the FPS every one second; this gives us a good average.
In addition, it keeps the FPS the same for one second, giving us enough
time to read it before it changes again.
So every frame we increment FrameCnt and add the time elapsed
from the last frame to TimeElapsed :
FrameCnt++;
TimeElapsed += timeDelta;
where timeDelta is the time it took between frames.
After one second has passed, we can compute the FPS with the fol-
lowing formula:
FPS = (float)FrameCnt / TimeElapsed;
We then reset FrameCnt and TimeElapsed and begin averaging the
FPS for the next second. Here is the code put together:
void CalcFPS(float timeDelta)
{
FrameCnt++;
TimeElapsed += timeDelta;
if(TimeElapsed >= 1.0f)
{
FPS = (float)FrameCnt / TimeElapsed;
TimeElapsed = 0.0f;
FrameCnt
= 0;
}
}
9.2 CD3DFont
The DirectX SDK provides some useful utility code located in the
\Samples\C++\Common folder of your DXSDK root directory. Among
that code is the CD3DFont class, which renders text using textured tri-
angles and Direct3D. Since CD3DFont uses Direct3D for rendering
instead of GDI, it is much faster than ID3DXFont . However,
CD3DFont does not support the complex fonts and formatting that
Search WWH ::




Custom Search