piotrwalat.net

Simple framerate counter for MonoGame games

leave a comment

Fluid and smooth user experience is a key element of any good Windows Store app. If you are writing a game, you most likely will want to measure and display framerate related data (current framerate, min. framerate, etc.) . In this example I am going to show how to add a simple FPS counter to a MonoGame powered game.

XAML’s DebugSettings.EnableFrameRateCounter

If you are familiar with XAML development, you might have used DebugSettings.EnableFrameRateCounter flag to display performance and framerate related statistics. This can be enabled in App.xaml.cs by adding one line to the constructor.

It is a very useful feature, this however wont work when using full screen SwapChainBackgroundPanel(which is the default technique for MonoGame + XAML Windows Store apps).

Simple MonoGame framerate counter

Frame rate calculation algorithm we use is simple and is based on following principles:

  • every time a scene has been drawn using Draw() increase FrameCounter by 1,
  • in Update() method measure ElapsedTime which is the time (in milliseconds) since last frame rate update,
  • if ElapsedTime is more than 1 second (1000 ms) use FrameCounter as the new FPS value.
Here is a class that will store frame-rate related data.

Instead of using a monolithic DrawableGameComponent (which would, update, draw and store the state), I usually like to separate update and draw logic into separate classes

DrawingContext is just a placeholder for SpriteBatch (and GraphicsDevice if you need it). Feel free to pass in SpriteBatch directly – this will simplify IDraw signature a little bit

Here is the logic that will be executed in Game.Update() method.

Please note that instead of creating new Vector2 objects used to position the strings every time we call  SpriteBatch.DrawString(), we are reusing instances created earlier. This aims to reduce unnecessary garbage collection and even though this may seem obvious to many, such optimizations can be easily overlooked.

Draw method has twofold purpose – obviously it needs to draw frame-rate strings, but apart from that we need it to increase frame counter. To be more flexible you may want to measure the strings instead of using hard-coded position values.

Now, in your Game class you can add and instantiate the following fields:

Also don’t forget to load the sprite font and initialize content:

 

The end result will look somewhat like this, it is definitely a no frills solution, but its not something that is normally visible to the end-user anyway.

I imagine that frame-rate data (especially min/avg. FPS) could also be used to gather some performance data directly from the users running the game, especially if you cannot test it on all device types that the game is intended for (this shouldn’t be that hard with Windows 8/Windows RT). Although this is a material for another post :)

The code is not Windows 8 specific in any way and should work on all MonoGame supported platforms, but I have only tested it in a Windows 8 XAML scenario.

Written by Piotr Walat

November 6th, 2012 at 10:00 am