[XNA Framework] Get Frames Per Second

lxXTaCoXxl 0 Tallied Votes 255 Views Share

I haven't tested this yet but it should work. The implementation is fairly simple. I don't have my laptop so I can debug the code at the present time. This basically increments the frame count everytime the game draws itself (allowing us to calculate frames per second considering the draw method is called as needed not by update). I'm over at a friend's house at the present time and was showing her how XNA Framework operates, then the idea came to me to develop a FPS Monitor. I will be developing a few other ideas I have when I get home. The current time is 19:46PM CST (Central Standard Time); My ETA is around 21:00PM.

// Implementation

// This is supposed to be your Game1.cs file.
// As I stated, I don't have my laptop so nothing is exact. But this should work 100%.
// If it doesn't then I will update it around 9pm Central Time tonight when I get home to my laptop.

FPSMonitor fps;

public override void Initialize()
{
fps = new FPSMonitor(this);
fps.Initialize();
}

public override void Update(GameTime gameTime)
{
fps.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();

spriteBatch.DrawString(YourSpriteFont, "FPS: " + fps.Value.ToString(), new Vector2(5, 5), Color.White);

spriteBatch.End();
}
public class FPSMonitor
{
	public float Value {get; private set;}
	public TimeSpan Sample {get; set;}

	private Stopwatch sw;
	private int Frames;

	public FPSMonitor(Game Game) : base(Game)
	{
		this.Sample = TimeSpan.FromSeconds(1);
	}

	public void Initialize()
	{
		this.Value = 0;
		this.Frames = 0;
		this.sw = Stopwatch.StartNew();
	}

	public void Update(GameTime gameTime)
	{
		if (sw.Elapsed > Sample)
		{
			this.Value = (float)Frames / sw.Elapsed.TotalSeconds;

			this.sw.Reset();
			this.sw.Start();
			this.Frames = 0;
		}
	}

	public void Draw(GameTime gameTime)
	{
		this.Frames++;
	}
}
lxXTaCoXxl 26 Posting Whiz in Training

Fixed all problems within the code, as I stated in the original post I typed it up in notepad so I wasn't 100% sure; and the code was as it turns out was not accurate. Had a few errors in it. So here you go, the new release. I've changed the initialization of it though, I've initialized all the variables within the constructor; the rest is the same.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Dev9.Xna.Performance
{
    public class FpsMonitor
    {
        public float Value { get; private set; }
        public TimeSpan Sample { get; set; }

        private Stopwatch sw;
        private int Frames;

        public FpsMonitor()
        {
            this.Sample = TimeSpan.FromSeconds(1);
            this.Value = 0;
            this.Frames = 0;
            this.sw = Stopwatch.StartNew();
        }

        public void Update()
        {
            if (sw.Elapsed > Sample)
            {
                this.Value = (float)(Frames / sw.Elapsed.TotalSeconds);

                this.sw.Reset();
                this.sw.Start();
                this.Frames = 0;
            }
        }

        public void Draw(SpriteBatch SpriteBatch, SpriteFont Font, Vector2 Location, Color Color)
        {
            this.Frames++;
            SpriteBatch.DrawString(Font, "FPS: " + this.Value.ToString(), Location, Color);
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.