MonoGame显示FPS
在MonoGame中显示实时FPS
帧计数器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class FrameCounter { public FrameCounter() { } public long TotalFrames { get; private set; } public float TotalSeconds { get; private set; } public float AverageFramesPerSecond { get; private set; } public float CurrentFramesPerSecond { get; private set; } public const int MAXIMUM_SAMPLES = 100; private Queue<float> _sampleBuffer = new Queue<float>(); public override bool Update(float deltaTime) { CurrentFramesPerSecond = 1.0f / deltaTime; _sampleBuffer.Enqueue(CurrentFramesPerSecond); if (_sampleBuffer.Count > MAXIMUM_SAMPLES) { _sampleBuffer.Dequeue(); AverageFramesPerSecond = _sampleBuffer.Average(i => i); } else { AverageFramesPerSecond = CurrentFramesPerSecond; } TotalFrames++; TotalSeconds += deltaTime; return true; } } |
使用方法:
1 |
private FrameCounter _frameCounter = new FrameCounter(); |
1 2 3 4 5 6 7 8 9 10 11 12 |
protected override void Draw(GameTime gameTime) { var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; _frameCounter.Update(deltaTime); var fps = string.Format("FPS: {0}", _frameCounter.AverageFramesPerSecond); _spriteBatch.DrawString(_spriteFont, fps, new Vector2(1, 1), Color.Black); } |
如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流
原创文章,转载请注明本文链接地址(违者必究):MonoGame显示FPS