diff options
author | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
---|---|---|
committer | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
commit | ced3d03bdb3ce866d832e03fb212865140905a9a (patch) | |
tree | 2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/Screens/FpsCounter.cs | |
parent | 0394dccaf06e1009e591a6ff4d645895574724c1 (diff) | |
download | V3-release.tar.gz V3-release.tar.bz2 |
Diffstat (limited to 'V3/Screens/FpsCounter.cs')
-rw-r--r-- | V3/Screens/FpsCounter.cs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/V3/Screens/FpsCounter.cs b/V3/Screens/FpsCounter.cs new file mode 100644 index 0000000..5c3ac0c --- /dev/null +++ b/V3/Screens/FpsCounter.cs @@ -0,0 +1,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++; + } + } +} |