aboutsummaryrefslogtreecommitdiff
path: root/V3/Screens/FpsCounter.cs
blob: 5c3ac0cd48db4c0630e351d9104052b7ed35e904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using Microsoft.Xna.Framework;

namespace V3.Screens
{
    /// <summary>
    /// Counts the frames per second based on the elapsed time and the number
    /// of frames that have been drawn.  Call Update in each Update, and
    /// AddFrame in each Draw.
    /// </summary>
    public sealed class FpsCounter
    {
        /// <summary>
        /// The current frames per second.
        /// </summary>
        public int Fps { get; private set; }

        private int mFrameCount;
        private TimeSpan mTimeSpan = TimeSpan.Zero;

        /// <summary>
        /// Updates the elapsed time and -- once every second -- the fps value.
        /// </summary>
        /// <param name="gameTime">the elapsed game time</param>
        public void Update(GameTime gameTime)
        {
            mTimeSpan += gameTime.ElapsedGameTime;

            if (mTimeSpan > TimeSpan.FromSeconds(1))
            {
                mTimeSpan -= TimeSpan.FromSeconds(1);
                Fps = mFrameCount;
                mFrameCount = 0;
            }
        }

        /// <summary>
        /// Registers that a frame has been drawn. Should be called once for
        /// every Draw.
        /// </summary>
        public void AddFrame()
        {
            mFrameCount++;
        }
    }
}