Skip to content

Instantly share code, notes, and snippets.

@MrSoundless
Created July 9, 2011 16:46
Show Gist options
  • Select an option

  • Save MrSoundless/1073730 to your computer and use it in GitHub Desktop.

Select an option

Save MrSoundless/1073730 to your computer and use it in GitHub Desktop.
XNA FPS Meter
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
namespace eindproject
{
public class FPSMeter : DrawableGameComponent
{
public FPSMeter(Game game)
: base (game)
{
_updateTimes = new List<float>();
_drawTimes = new List<float>();
}
public float DrawFPS
{
get { return _drawTimes.Count() / _drawTimes.Sum(); }
}
public float UpdateFPS
{
get { return _updateTimes.Count() / _updateTimes.Sum(); }
}
public override void Update(GameTime gameTime)
{
_updateTimes.Add((float)gameTime.ElapsedGameTime.TotalSeconds);
while (_updateTimes.Sum() > 1f)
_updateTimes.RemoveAt(0);
}
public override void Draw(GameTime gameTime)
{
_drawTimes.Add((float)gameTime.ElapsedGameTime.TotalSeconds);
while (_drawTimes.Sum() > 1f)
_drawTimes.RemoveAt(0);
}
private List<float> _updateTimes;
private List<float> _drawTimes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment