2010-04-04 00:12:01 +00:00
|
|
|
|
using System;
|
2010-07-16 09:36:41 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2010-04-04 00:12:01 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfish.Library
|
|
|
|
|
{
|
|
|
|
|
public sealed class FrameRateCounter : DrawableGameComponent
|
|
|
|
|
{
|
|
|
|
|
public FrameRateCounter(GameBase game) :
|
|
|
|
|
base(game)
|
|
|
|
|
{
|
|
|
|
|
FontColor = Color.White;
|
|
|
|
|
FontName = "Default";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadContent()
|
|
|
|
|
{
|
|
|
|
|
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
|
|
|
_spriteFont = Game.Content.Load<SpriteFont>(FontName);
|
|
|
|
|
|
|
|
|
|
var titleSafeArea = Game.GraphicsDevice.DisplayMode.TitleSafeArea;
|
|
|
|
|
Position = new Vector2(titleSafeArea.X, titleSafeArea.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 09:36:41 +00:00
|
|
|
|
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
|
2010-04-04 00:12:01 +00:00
|
|
|
|
public override void Draw(GameTime gameTime)
|
|
|
|
|
{
|
|
|
|
|
_frameCount++;
|
|
|
|
|
|
2010-05-28 10:48:08 +00:00
|
|
|
|
_frameRateBuilder.Clear().AppendWithoutGarbage(_frameRate).Append(" fps");
|
2010-04-04 00:12:01 +00:00
|
|
|
|
|
2010-05-19 23:42:10 +00:00
|
|
|
|
_spriteBatch.Begin();
|
|
|
|
|
//_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position - Vector2.UnitX, Color.Black); // rough outline
|
|
|
|
|
//_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position + Vector2.UnitX, Color.Black);
|
|
|
|
|
//_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position - Vector2.UnitY, Color.Black);
|
|
|
|
|
//_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position + Vector2.UnitY, Color.Black);
|
2010-04-04 00:12:01 +00:00
|
|
|
|
_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position, FontColor);
|
|
|
|
|
_spriteBatch.End();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 09:36:41 +00:00
|
|
|
|
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
|
2010-04-04 00:12:01 +00:00
|
|
|
|
public override void Update(GameTime gameTime)
|
|
|
|
|
{
|
2010-05-19 23:42:10 +00:00
|
|
|
|
if (gameTime == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("gameTime");
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-04 00:12:01 +00:00
|
|
|
|
_elapsedTime += gameTime.ElapsedGameTime.Ticks;
|
|
|
|
|
|
|
|
|
|
if (_elapsedTime >= TimeSpan.TicksPerSecond)
|
|
|
|
|
{
|
|
|
|
|
_elapsedTime -= TimeSpan.TicksPerSecond;
|
|
|
|
|
_frameRate = _frameCount;
|
|
|
|
|
_frameCount = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Color FontColor { get; set; }
|
|
|
|
|
public string FontName { get; set; }
|
|
|
|
|
public Vector2 Position { get; set; }
|
|
|
|
|
|
|
|
|
|
private SpriteBatch _spriteBatch;
|
|
|
|
|
private SpriteFont _spriteFont;
|
|
|
|
|
private long _elapsedTime;
|
|
|
|
|
private int _frameCount;
|
|
|
|
|
private int _frameRate;
|
|
|
|
|
private StringBuilder _frameRateBuilder = new StringBuilder(); // cache builder; avoids garbage
|
|
|
|
|
}
|
|
|
|
|
}
|