#region File Description
//-----------------------------------------------------------------------------
// FrameRateCounter.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace CollisionSample
{
///
/// General Timing and Frame Rate Display Component.
/// Add this to the GameComponentCollection to display the frame rate
///
public class FrameRateCounter : DrawableGameComponent
{
ContentManager content;
SpriteBatch spriteBatch;
SpriteFont spriteFont;
int frameRate = 0;
int frameCounter = 0;
TimeSpan elapsedTime = TimeSpan.Zero;
///
/// Constructor which initializes the Content Manager which is used later for loading the font for display.
///
///
public FrameRateCounter(Game game)
: base(game)
{
content = new ContentManager(game.Services);
}
///
/// Graphics device objects are created here including the font.
///
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteFont = content.Load("content\\Font");
}
///
///
///
protected override void UnloadContent()
{
content.Unload();
}
///
/// The Update function is where the timing is measured and frame rate is computed.
///
///
public override void Update(GameTime gameTime)
{
elapsedTime += gameTime.ElapsedGameTime;
if (elapsedTime > TimeSpan.FromSeconds(1))
{
elapsedTime -= TimeSpan.FromSeconds(1);
frameRate = frameCounter;
frameCounter = 0;
}
}
#region Draw
///
/// Frame rate display occurs during the Draw method and uses the Font and Sprite batch to render text.
///
///
public override void Draw(GameTime gameTime)
{
frameCounter++;
string fps = string.Format("fps: {0}", frameRate);
spriteBatch.Begin();
spriteBatch.DrawString(spriteFont, fps, new Vector2(32, 32), Color.White);
spriteBatch.End();
}
#endregion
}
}