Game Development Reference
In-Depth Information
{
CurrentFPS = (double)_numberOfFrames / _timePassed;
_timePassed = 0;
_numberOfFrames = 0;
}
}
}
This class calculates the frames per second. Its process method must be called
every frame. Add
FramesPerSecond
to the
FPSTestState
so we can render
this value to the screen using the
Text
class.
class FPSTestState : IGameObject
{
TextureManager _textureManager;
Font _font;
Text _fpsText;
Renderer _renderer = new Renderer();
FramesPerSecond _fps = new FramesPerSecond();
// Constructor and Render have been ommitted.
public void Update(double elapsedTime)
{
_fps.Process(elapsedTime);
}
}
When the state is run, the fps is now recorded. Generally, the
FramesPerSe-
cond
class would not exist in a game state; instead, it probably would be in the
Form
class. In this case, it's easier to test in the
FPSTestState
.
public void Render()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
_fpsText = new Text("FPS: " + _fps.CurrentFPS.ToString("00.0"), _font);
_renderer.DrawText(_fpsText);
}
The render loop renders the text ''FPS:'' followed by the fps value converted to a
string. The
ToString
method is given some formatting information. This
causes the double that represents the fps to only have one decimal place when in
string form and to have two or more digits before the decimal point.
Search WWH ::
Custom Search