Game Development Reference
In-Depth Information
Figure 7.4
The text FPS being rendered.
higher the frame count, the faster your game is running. FPS is a useful statistic
to have on screen because as you develop your game it's easy to notice if, after
adding a feature, the fps has suddenly dropped. Silly mistakes can be caught early
and avoided.
To count the number of frames each time the game loops, a _ numberOf-
Frames variable can be increased by one. The elapsedTime in the update
loop tells us how long each frame took; if all these elapsedTime values are
summed, how much time has passed can be measured. Once a second has
passed, then the _ numberOfFrames is the number of frames that were
rendered during that second. This can easily be wrapped up in a class, as
shown here.
public class FramesPerSecond
{
int _numberOfFrames = 0;
double _timePassed = 0;
public double CurrentFPS { get; set; }
public void Process(double timeElapsed)
{
_numberOfFrames++;
_timePassed = _timePassed + timeElapsed;
if (_timePassed > 1)
 
Search WWH ::




Custom Search