Seeing as how to calculate the fps of a game, it occurred to me the idea to put all that code in a GameComponent so it can be reused as many times as I like, but I'm not sure if this is one case in which one must use the GameComponent or not worth it.
This is my GameComponent:
using System;
using System.Collections.Generic;
using System.Linq;
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 SpriteTest
{
///
/// This is a game component that implements IUpdateable.
///
public class FPS : Microsoft.Xna.Framework.DrawableGameComponent
{
private float fps;
private float updateInterval = 1.0f;
private float timeSinceLastUpdate = 0.0f;
private float framecount = 0;
private GameWindow Window;
public FPS(Game game, GameWindow Window) : base(game)
{
// TODO: Construct any child components here
this.Window = Window;
}
///
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
///
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
///
/// Allows the game component to update itself.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
framecount++;
timeSinceLastUpdate += elapsed;
if (timeSinceLastUpdate > updateInterval)
{
fps = framecount / timeSinceLastUpdate; //mean fps over updateIntrval
#if XBOX360
System.Diagnostics.Debug.WriteLine("FPS: " + fps.ToString() + " - RT: " + gameTime.ElapsedGameTime.TotalSeconds.ToString() + " - GT: " + gameTime.ElapsedGameTime.TotalSeconds.ToString());
#else
Window.Title = "FPS: " + fps.ToString();
#endif
framecount = 0;
timeSinceLastUpdate -= updateInterval;
}
base.Draw(gameTime);
}
}
}And inside my main program, I call it so:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
//graphics.SynchronizeWithVerticalRetrace = false;
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
//IsFixedTimeStep = false;
Content.RootDirectory = "Content";
Components.Add(new FPS(this, Window));
}






