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