If your game is running a little slow, or you are just curious about how many frames per second you are getting, you've come to the right tutorial. Here we will look at an extremely simple way to check the frame rate of your game!
Calculating the Frame Rate
In XNA we can calculate the frame rate of our game with one line of code in the Draw() method:
float frameRate = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;
This way of calculating the frame rate is about as simple as it gets!
Seeing the Frame Rate
Obviously, the line of code above isn't going to really do much good if you can't see the frame rate yourself, so here's a couple of suggestions for how to do that if you don't already know how:
- Draw it with a Sprite Font. It's pretty simple, and is worth learning about, if you don't know how. I've already made a tutorial for Drawing Text with SpriteFonts that you can look at to do this.
- A second valid option would be to write it out to the command line. I've also written a tutorial about making the command line visible in XNA.
Some Other Notes
This same code can be placed in the Update() method and you can see the number of updates that are being done each second as well.
The default frame rate is 60 frames per second. XNA allows you to have it update as frequently as possible, however. Many high end games run with a fixed frame rate around 30 frames per second. If your frame rate is down below 15 or 20 frames per second, you probably want to do something about it.
Also, this method doesn't do any sort of averaging, and it might be useful to calculate the average frame rate over a small span of time.
|Having problems with this tutorial? Try the troubleshooting page!|