/* * Util.FrameRateDisplayComponent.cs * Authors: Adam Nabinger * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System.Globalization; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Util { /// /// A Component to display the current FPS on the screen. /// public sealed class FrameRateDisplayComponent : DrawableGameScreenComponent { #region Constants // String constant for default file name for menu font private const string defaultfontname = @"Fonts\menufont"; #endregion #region Fields // Reference to the current gamescreen //private readonly GameScreen mScreen; // The sprite batch used for rendering textures private SpriteBatch mSB; // The sprite font used for rendering text private SpriteFont mFont; // Position on screen for the frame rate display component private Vector2 mPosition = Vector2.Zero; // The color of the frame rate display component private Color mColor = Color.White; // Whether or not the frame rate display component has been loaded private bool loaded; // The path name of the sprite font to use private readonly string fontname; // The current count of frames per second private int framecount; // The total time from each update private double time; // The amount of seconds between each update private double mUpdateInterval = 1; // The text to display in the frame rate display component private string displaytext = string.Empty; #endregion #region Properties /// /// Gets/Sets the Upper Left corner of where the FPS will be drawn to the screen /// public Vector2 Position { get { return mPosition; } set { mPosition = value; } } /// /// Gets/Sets the Color the Text will be drawn in. /// public Color Color { get { return mColor; } set { mColor = value; } } /// /// Gets/Sets how often, in seconds, the count will be updated. /// public double UpdateInterval { get { return mUpdateInterval; } set { mUpdateInterval = value; } } #endregion #region Creation /// /// Constructor /// internal FrameRateDisplayComponent() {} /// /// Constructor /// /// Path name for the sprite font to use internal FrameRateDisplayComponent(string font) { fontname = font; } #endregion #region Initialization /// /// Initialize this component. /// public override void Initialize() { DrawOrder = parent.DrawOrder + 5; UpdateOrder = parent.UpdateOrder + 5; base.Initialize(); } #endregion #region Management /// /// Loads in all necessary information for content dependent objects /// protected override void LoadContent() { if (!loaded) { mSB = new SpriteBatch(GraphicsDevice); mFont = fontname == null ? Game.Content.Load(defaultfontname) : parent.Content.Load(fontname); loaded = true; } base.LoadContent(); } /// /// Unload any content dependent information /// protected override void UnloadContent() { if (loaded) { loaded = false; } base.UnloadContent(); } /// /// Disposes of all unmanaged objects /// /// Whether or not this is currently being disposed protected override void Dispose(bool disposing) { if (loaded && disposing) { mSB.Dispose(); } base.Dispose(disposing); } #endregion #region Update /// /// Update this component. /// /// Time passed in game public override void Update(GameTime gameTime) { time += gameTime.ElapsedGameTime.TotalSeconds; if (time > mUpdateInterval) { displaytext = (framecount / time).ToString("F0", CultureInfo.CurrentCulture); time = 0; framecount = 0; } base.Update(gameTime); } #endregion #region Render /// /// Draw this component. /// /// Time passed in game public override void Draw(GameTime gameTime) { mSB.Begin(); mSB.DrawString(mFont, displaytext, mPosition, mColor); mSB.End(); ++framecount; base.Draw(gameTime); } #endregion } }