Simple framerate counter for MonoGame games

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.

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:

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.