////// The current frames during this second /// private int _currentFrameCount; ////// The total time that has passed. This is used to determine /// when a second has passed. /// private TimeSpan _timePassed; ////// Text representation of the current frame rate /// private SpriteText _frameCountText; ////// The spritebatch to draw the frame rate to /// private SpriteBatch _spriteBatch;
/// <summary> /// Constructor /// </summary> /// <param name="viewPort">The viewport that will determine the position of the frame counter</param> /// <param name="spriteBatch">The spritebatch the frame count will be drawn to</param> /// <param name="spriteFont">The font to draw the frame count in</param> /// <param name="alignment">The area of the screen the frame count should be fixed to</param> public FrameCounter(Viewport viewPort, SpriteBatch spriteBatch, SpriteFont spriteFont, Alignment alignment) { if (alignment != Alignment.TopLeft && alignment != Alignment.TopRight && alignment != Alignment.BottomLeft && alignment != Alignment.BottomRight) { throw new ArgumentException(String.Format("Alignment '{0}' is not supported", alignment)); } _spriteBatch = spriteBatch; _currentFrameCount = 0; _timePassed = new TimeSpan(); _frameCountText = new SpriteText(spriteFont, String.Format("FPS: 0")); _frameCountText.SetOrigin(alignment); switch (alignment) { case Alignment.TopLeft: _frameCountText.Position = new Vector2(viewPort.TitleSafeArea.Left, viewPort.TitleSafeArea.Top); break; case Alignment.TopRight: _frameCountText.Position = new Vector2(viewPort.TitleSafeArea.Right, viewPort.TitleSafeArea.Top); break; case Alignment.BottomLeft: _frameCountText.Position = new Vector2(viewPort.TitleSafeArea.Left, viewPort.TitleSafeArea.Bottom); break; case Alignment.BottomRight: _frameCountText.Position = new Vector2(viewPort.TitleSafeArea.Right, viewPort.TitleSafeArea.Bottom); break; } }
/// <summary> /// Draw the frame count /// </summary> /// <remarks> /// SpriteBatch.Begin must be called before this is /// </remarks> /// <param name="gameTime">A snapshot of time passed</param> public void Draw(GameTime gameTime) { _currentFrameCount++; _timePassed = _timePassed.Add(new TimeSpan(gameTime.ElapsedGameTime.Ticks)); if (_timePassed.Seconds >= 1) { _timePassed = _timePassed.Subtract(new TimeSpan(0, 0, 1)); _frameCountText.Text = String.Format("FPS: {0}", _currentFrameCount); _currentFrameCount = 0; } _frameCountText.Draw(_spriteBatch); }
David Kendall | Blog hosted by Blogger
Social media icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY