{"id":30,"date":"2020-10-25T20:45:00","date_gmt":"2020-10-26T01:45:00","guid":{"rendered":"https:\/\/cynthiamcmahon.ca\/blog\/index.php\/2020\/10\/frames-per-second-counter\/"},"modified":"2020-10-25T20:45:00","modified_gmt":"2020-10-26T01:45:00","slug":"frames-per-second-counter","status":"publish","type":"post","link":"https:\/\/cynthiamcmahon.ca\/blog\/2020\/10\/frames-per-second-counter\/","title":{"rendered":"Frames Per Second Counter"},"content":{"rendered":"<p>So, I&#8217;m going to try something new. I&#8217;m not happy with the way it is rendering 100% but it should copy\/paste fine. Most probably have one by now but I thought I&#8217;d share my frames per second component outside of a tutorial on its own. It renders the FPS into the title bar of the window and the output log in Visual Studio. I recommend using the base constructor and adding it to the list of game components in the constructor using <span style=\"color: #9fc5e8;\">Components.Add(new FramesPerSecond(this))<\/span>.<\/p>\n<p><b>UPDATE:<\/b> I fixed the issues I had with code.<\/p>\n<pre><code><br \/>using System;<br \/>using Microsoft.Xna.Framework;<br \/><br \/>namespace Psilibrary<br \/>{<br \/>    public sealed 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 \/><br \/>        public FramesPerSecond(Game game)<br \/>            : this(game, false, false, game.TargetElapsedTime)<br \/>        {<br \/>        }<br \/><br \/>        public FramesPerSecond(<br \/>            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 = synchWithVerticalRetrace;<br \/>            Game.IsFixedTimeStep = isFixedTimeStep;<br \/>            Game.TargetElapsedTime = targetElapsedTime;<br \/>        }<br \/><br \/>        public sealed override void Draw(GameTime gameTime)<br \/>        {<br \/>            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;<br \/>            _frameCount++;<br \/>            _timeSinceLastUpdate += elapsed;<br \/><br \/>            if (_timeSinceLastUpdate &gt; _updateInterval)<br \/>            {<br \/>                _fps = _frameCount \/ _timeSinceLastUpdate;<br \/>                System.Diagnostics.Debug.WriteLine(\"FPS: \" + _fps.ToString());<br \/>                Game.Window.Title = \"FPS: \" + ((int)_fps).ToString();<br \/>                _frameCount = 0;<br \/>                _timeSinceLastUpdate -= _updateInterval;<br \/>            }<br \/><br \/>            base.Draw(gameTime);<br \/>        }<br \/>    }<br \/>}<br \/><\/code><br \/><pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, I&#8217;m going to try something new. I&#8217;m not happy with the way it is rendering 100% but it should copy\/paste fine. Most probably have one by now but I thought I&#8217;d share my frames per second component outside of a tutorial on its own. It renders the FPS into the title bar of the &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,5],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-code","category-general"],"_links":{"self":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts\/30","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=30"}],"version-history":[{"count":0,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"wp:attachment":[{"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cynthiamcmahon.ca\/blog\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}