Links of Interest
Main
Download
Prerequisites
Screenshots

Articles/Code Snippets
DBP Warmup Entries
Blog feed icon
Contact/Feedback

Quick Downloads:
bullet Diaspora
bullet XNA Requirements Installer

External Links
Heather [layout designer]
XNA

Statistics Component
This component prints out the FPS and the average number of milliseconds it takes to update and draw a frame. It also supports one custom measurement, but it is easy to extend the class to have multiple custom measurements. The usage is simple. However, you want to retain a reference to the component to call the methods later.

statisticsComponent = new StatisticsComponent(this);
this.Components.Add(statisticsComponent);

protected override void Update(GameTime gameTime)
{
    statisticsComponent.BeginUpdate();

    // Process the game update statements.

    statisticsComponent.EndUpdate();
}
 

Repeat for the Draw method and/or your custom measurement. And here is the actual component:

    class StatisticsComponent : DrawableGameComponent
    {
        Stopwatch stopwatch;
        Stopwatch customWatch;
        long updateMSPerSecond;
        long drawMSPerSecond;
        int frames;
        int totalframes;
        float elapsedSeconds;
        float totalElapsedSeconds;
        bool updateMSChecked;
        bool drawMSChecked;

        string customName;
        float elapsedCustom;
        bool customCheckEnabled;

        public StatisticsComponent(Game game)
            : base(game)
        {
            stopwatch = new Stopwatch();
            customWatch = new Stopwatch();
            game.Exiting += new EventHandler(game_Exiting);
        }

        public void BeginUpdate()
        {
            stopwatch.Start();
        }

        public void EndUpdate()
        {
            updateMSPerSecond += stopwatch.ElapsedMilliseconds;
            stopwatch.Stop();
            stopwatch.Reset();
            updateMSChecked = true;   
        }

        public void BeginDraw()
        {
            stopwatch.Start();
        }

        public override void Draw(GameTime gameTime)
        {
            frames++;
            totalframes++;
            elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;
            totalElapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;
            if (elapsedSeconds >= 1)
            {
                elapsedSeconds -= 1;
                string str = "FPS: " + frames;
                if (updateMSChecked)
                    str += ", UpdateMS/Frame: " + (updateMSPerSecond / (float)frames);
                if (drawMSChecked)
                    str += ", DrawMS/Frame: " + (drawMSPerSecond / (float)frames);
                if (customCheckEnabled)
                    str += ", " + customName + " MS/Frame: " + (elapsedCustom / (float)frames);
                Console.WriteLine(str);
                Game.Window.Title = str;

                frames = 0;
                drawMSPerSecond = 0;
                updateMSPerSecond = 0;
                elapsedCustom = 0;
            }
            base.Draw(gameTime);
        }

        public void EndDraw()
        {
            drawMSPerSecond += stopwatch.ElapsedMilliseconds;
            stopwatch.Stop();
            stopwatch.Reset();
            drawMSChecked = true;
        }

        public void EnabledCustom(string name)
        {
            customName = name;
            customCheckEnabled = true;
        }

        public void DisableCustom()
        {
            customCheckEnabled = false;
        }

        public void BeginCustom()
        {
            customWatch.Start();
        }

        public void EndCustom()
        {
            elapsedCustom += stopwatch.ElapsedMilliseconds;
            customWatch.Stop();
            customWatch.Reset();
        }

        void game_Exiting(object sender, EventArgs e)
        {
            Console.WriteLine("Average FPS: " + totalframes / totalElapsedSeconds);
        }
    }

This website and its content are the property of ascii reversal of the ascii of the hex reversal of the hex of the bit inverse of 11001111 11001111 11001000 11001010 11000110 11001001 10111100 11001001 10111100 11001001 11000110 11001001 11001110 11001001 10111011 11001001 11001111 11001101 10111100 11001011 11000110 11001001.