Virtu/Library/Xna/FrameRateCounter.cs
Sean Fausett 2ba3146299 Replaced event handlers with direct delegates where appropriate.
Cosmetic changes including using C# 4 named and optional parameters.

--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4046806
2010-05-28 10:48:08 +00:00

76 lines
2.6 KiB
C#

using System;
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";
//game.IsFixedTimeStep = true; // fixed (default)
//game.TargetElapsedTime = TimeSpan.FromSeconds(1 / 60f);
//game.IsFixedTimeStep = false; // flatout
//game.GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false;
}
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);
}
public override void Draw(GameTime gameTime)
{
_frameCount++;
_frameRateBuilder.Clear().AppendWithoutGarbage(_frameRate).Append(" fps");
_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);
_spriteBatch.DrawString(_spriteFont, _frameRateBuilder, Position, FontColor);
_spriteBatch.End();
}
public override void Update(GameTime gameTime)
{
if (gameTime == null)
{
throw new ArgumentNullException("gameTime");
}
_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
}
}