From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/Screens/FpsCounter.cs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 V3/Screens/FpsCounter.cs (limited to 'V3/Screens/FpsCounter.cs') 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 +{ + /// + /// 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. + /// + public sealed class FpsCounter + { + /// + /// The current frames per second. + /// + public int Fps { get; private set; } + + private int mFrameCount; + private TimeSpan mTimeSpan = TimeSpan.Zero; + + /// + /// Updates the elapsed time and -- once every second -- the fps value. + /// + /// the elapsed game time + public void Update(GameTime gameTime) + { + mTimeSpan += gameTime.ElapsedGameTime; + + if (mTimeSpan > TimeSpan.FromSeconds(1)) + { + mTimeSpan -= TimeSpan.FromSeconds(1); + Fps = mFrameCount; + mFrameCount = 0; + } + } + + /// + /// Registers that a frame has been drawn. Should be called once for + /// every Draw. + /// + public void AddFrame() + { + mFrameCount++; + } + } +} -- cgit v1.2.1