{"id":15,"date":"2020-11-10T09:28:00","date_gmt":"2020-11-10T14:28:00","guid":{"rendered":""},"modified":"2020-12-26T20:14:59","modified_gmt":"2020-12-27T01:14:59","slug":"updated-frames-per-second-counter","status":"publish","type":"post","link":"https:\/\/cynthiamcmahon.ca\/blog\/2020\/11\/updated-frames-per-second-counter\/","title":{"rendered":"Updated Frames Per Second Counter"},"content":{"rendered":"<p>I made an update to my FPS counter. It is great to know the current FPS but the average FPS is also of great value. It gives you a better sense of how your game will perform over time. I also included a reset in case I need to test different areas of the game I&#8217;m working on. I thought I&#8217;d share the updates.<\/p>\n<p><\/p>\n<pre><br \/><code><br \/>public class FramesPerSecond : DrawableGameComponent<br \/>{<br \/>    private float _fps;<br \/>    private float _updateInterval = 1.0f;<br \/>    private float _timeSinceLastUpdate = 0.0f;<br \/>    private float _frameCount = 0;<br \/>    private float _totalSeconds;<br \/>    private float _afps;<br \/>    private float _totalFrames;<br \/><br \/>    public FramesPerSecond(Game game)<br \/>        : this(game, false, false, game.TargetElapsedTime)<br \/>    {<br \/>    }<br \/><br \/>    public FramesPerSecond(Game game, <br \/>        bool synchWithVerticalRetrace, <br \/>        bool isFixedTimeStep, <br \/>        TimeSpan targetElapsedTime)<br \/>        : base(game)<br \/>    {<br \/>        GraphicsDeviceManager graphics = <br \/>            (GraphicsDeviceManager)Game.Services.GetService(<br \/>                typeof(IGraphicsDeviceManager));<br \/><br \/>        graphics.SynchronizeWithVerticalRetrace = <br \/>            synchWithVerticalRetrace;<br \/>        Game.IsFixedTimeStep = isFixedTimeStep;<br \/>        Game.TargetElapsedTime = targetElapsedTime;<br \/>    }<br \/><br \/>    public sealed override void Initialize()<br \/>    {<br \/>        base.Initialize();<br \/>    }<br \/><br \/>    public sealed override void Update(GameTime gameTime)<br \/>    {<br \/>        KeyboardState ks = Keyboard.GetState();<br \/><br \/>        if (ks.IsKeyDown(Keys.F1))<br \/>        {<br \/>            _fps = 0;<br \/>            _afps = 0;<br \/>            _timeSinceLastUpdate = 0;<br \/>            _totalFrames = 0;<br \/>            _frameCount = 0;<br \/>            _totalSeconds = 0;<br \/>        }<br \/><br \/>        base.Update(gameTime);<br \/>    }<br \/><br \/>    public sealed override void Draw(GameTime gameTime)<br \/>    {<br \/>        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;<br \/>        _frameCount++;<br \/>        _timeSinceLastUpdate += elapsed;<br \/>        _totalFrames++;<br \/><br \/>        if (_timeSinceLastUpdate &gt; _updateInterval)<br \/>        {<br \/>            _totalSeconds++;<br \/>            _fps = _frameCount \/ _timeSinceLastUpdate;<br \/>            _afps = _totalFrames \/ _totalSeconds;<br \/><br \/>            System.Diagnostics.Debug.WriteLine(\"FPS: \" + _fps.ToString());<br \/>#if !ANDROID<br \/>            Game.Window.Title = \"FPS: \" + _fps.ToString();<br \/>#endif<br \/>            _frameCount = 0;<br \/>            _timeSinceLastUpdate -= _updateInterval;<br \/>            System.Diagnostics.Debug.WriteLine(\"AFPS: \" + _afps.ToString());<br \/>#if !ANDROID<br \/>            Game.Window.Title += \" - AFPS: \" + _afps.ToString();<br \/>#endif<br \/>        }<br \/><br \/>        base.Draw(gameTime);<br \/>    }<br \/>}<br \/><\/code><br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I made an update to my FPS counter. It is great to know the current FPS but the average FPS is also of great value. It gives you a better sense of how your game will perform over time. I also included a reset in case I need to test different areas of the game &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":1,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}