-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathFPSCounterComponent.cs
48 lines (36 loc) · 1.12 KB
/
FPSCounterComponent.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Microsoft.Xna.Samples.GameComponents
{
public class FPSCounterComponent : DrawableGameComponent
{
SpriteBatch spriteBatch;
SpriteFont spriteFont;
int frameRate = 0;
int frameCounter = 0;
TimeSpan elapsedTime = TimeSpan.Zero;
public FPSCounterComponent(Game game, SpriteBatch Batch, SpriteFont Font)
: base(game)
{
spriteFont = Font;
spriteBatch = Batch;
}
public override void Update(GameTime gameTime)
{
elapsedTime += gameTime.ElapsedGameTime;
if (elapsedTime > TimeSpan.FromSeconds(1))
{
elapsedTime -= TimeSpan.FromSeconds(1);
frameRate = frameCounter;
frameCounter = 0;
}
}
public override void Draw(GameTime gameTime)
{
frameCounter++;
string fps = string.Format("FPS: {0}", frameRate);
spriteBatch.DrawString(spriteFont, fps, new Vector2(0, 0), Color.White);
}
}
}