Xblog.org

>> Tutorials >> XNA


Locations of visitors to this page

Visit Fancy Goth
My wifes site.

Tutorial001 - FPS Drawable Game Component

In Visual Studio create a new "Windows Game(2.0)" project or load your current project, I'll work with a new project called Tut001.

Right click on your XNA Game project and select "Add/New Item"

From the "Add New Item" dialog select "GameComponent" and call it dgcFPS.cs

You'll have the code below, I've just stripped out the comments to tidy it up:-

using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; namespace Tut001 { public class dgcFPS : Microsoft.Xna.Framework.GameComponent { public dgcFPS(Game game) : base(game) { } public override void Initialize() { base.Initialize(); } public override void Update(GameTime gameTime) { base.Update(gameTime); } } }

Change the

public class dgcFPS : Microsoft.Xna.Framework.GameComponent
to
public class dgcFPS : DrawableGameComponent

And add the following methods:-

protected override void LoadContent() { base.LoadContent(); } protected override void UnloadContent() { base.UnloadContent(); } public override void Draw(GameTime gameTime) { base.Draw(gameTime); }

Next add the following private variables:-

private SpriteBatch spriteBatch; private SpriteFont VideoFont; private float _ElapsedTime, _TotalFrames, _Fps; private bool _ShowFPS; private string _FontName = "DefaultFont";

Next Add a property for ShowFPS:-

public bool ShowFPS { get { return _ShowFPS; } set { _ShowFPS = value; } }

Now fill out the methods we've already added:-

Lets start with LoadContent, all we need here is to create a spritebatch for use in our draw method and the font that we are going to use I'm using a variable so we can change it at runtime.

protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(Game.GraphicsDevice); // Load the required font VideoFont = Game.Content.Load<SpriteFont>(_FontName); base.LoadContent(); }

The Update method is where we are going to do the math bit, keep track of the time taken and increment the TotalFrames by one then once a second has passed set the _Fps to TotalFrames and reset the ElapsedTime and TotalFrames ready for the next second.

public override void Update(GameTime gameTime) { base.Update(gameTime); _ElapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds; _TotalFrames++; if (_ElapsedTime >= 1.0f) { _Fps = _TotalFrames; _TotalFrames = 0; _ElapsedTime = 0; } }

Check that the Game wants to show the fps and call draw string to display it, Job done.

public override void Draw(GameTime gameTime) { if (ShowFPS) { spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.DrawString(VideoFont, "FPS=" + _Fps.ToString(), new Vector2(0, Game.GraphicsDevice.Viewport.Height - VideoFont.LineSpacing), Color.Red, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0); spriteBatch.End(); } base.Draw(gameTime); }

Now we'll add the constructor's I'm using multiple constructors to give me some flexibility in how I can use this component. To add a constructor to a class go the the line after the blank one that is added by default and type the following ctor then press tab twice hay presto another blank constructor just add args :-)

First up the blank constructor, the default isn't blank as such because it takes Game as it's parameter but it doesn't do anything and relies on the default values we set when we declared the variables.

public dgcFPS(Game game) : base(game) { }

Next up, are we showing by default? when I was testing this component it took me a few minuets to spot that it wasn't showing the FPS count because I had defaulted _ShowFPS to false so I added this one. I have added a parameter of type bool that sets the show property.

public dgcFPS(Game game, bool show) : base(game) { _ShowFPS = show; }

Now if you want a different font at start up rather than recompile the component, because you might not be able to, there's this one by passing the string to a SpriteFont file that you know will be with your Game you can set it here to avoid a run time error.

public dgcFPS(Game game, string FontName) : base(game) { _FontName = FontName; }

Now all that remains is to add the component to your Game add this code to you're Game1 constructor...

dgcFPS FPSComponent = new dgcFPS(this, "SpriteFont1"); this.Components.Add(FPSComponent); FPSComponent.ShowFPS = true;

MORT.


Disclaimer

This document and all files and code provided with it are provided "as is" and without any warranty at all, not even the implied warranty of fitness of use for any particular purpose. I am not responsible for any harm that might come from use or misuse of either this document or any of the files available for download from this site. Neither is there any guarantee that the information in this document is correct.