Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FrameRateCounter : DrawableGameComponent
- {
- ContentManager content;
- SpriteBatch spriteBatch;
- SpriteFont spriteFont;
- int frameRate = 0;
- int frameCounter = 0;
- TimeSpan elapsedTime = TimeSpan.Zero;
- public bool show = true;
- public FrameRateCounter(Game game, ContentManager setContent)
- : base(game)
- {
- content = setContent;
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- spriteFont = content.Load<SpriteFont>("fpsFont");
- }
- protected override void UnloadContent()
- {
- content.Unload();
- }
- 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++;
- if (!show) return;
- string fps = string.Format("{0}", frameRate);
- spriteBatch.Begin();
- spriteBatch.DrawString(spriteFont, fps, new Vector2(3, 3), Color.Black);
- spriteBatch.DrawString(spriteFont, fps, new Vector2(2, 2), Color.White);
- spriteBatch.End();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment