Guest User

XNA 4.0 FPS Counter

a guest
Jan 20th, 2012
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1.     public class FrameRateCounter : DrawableGameComponent
  2.     {
  3.         ContentManager content;
  4.         SpriteBatch spriteBatch;
  5.         SpriteFont spriteFont;
  6.  
  7.         int frameRate = 0;
  8.         int frameCounter = 0;
  9.         TimeSpan elapsedTime = TimeSpan.Zero;
  10.  
  11.         public bool show = true;
  12.  
  13.  
  14.         public FrameRateCounter(Game game, ContentManager setContent)
  15.             : base(game)
  16.         {
  17.             content = setContent;
  18.         }
  19.  
  20.  
  21.         protected override void LoadContent()
  22.         {
  23.                 spriteBatch = new SpriteBatch(GraphicsDevice);
  24.                 spriteFont = content.Load<SpriteFont>("fpsFont");
  25.         }
  26.  
  27.  
  28.         protected override void UnloadContent()
  29.         {
  30.                 content.Unload();
  31.         }
  32.  
  33.  
  34.         public override void Update(GameTime gameTime)
  35.         {
  36.             elapsedTime += gameTime.ElapsedGameTime;
  37.  
  38.             if (elapsedTime > TimeSpan.FromSeconds(1))
  39.             {
  40.                 elapsedTime -= TimeSpan.FromSeconds(1);
  41.                 frameRate = frameCounter;
  42.                 frameCounter = 0;
  43.             }
  44.         }
  45.  
  46.  
  47.         public override void Draw(GameTime gameTime)
  48.         {
  49.             frameCounter++;
  50.             if (!show) return;
  51.  
  52.             string fps = string.Format("{0}", frameRate);
  53.  
  54.             spriteBatch.Begin();
  55.  
  56.             spriteBatch.DrawString(spriteFont, fps, new Vector2(3, 3), Color.Black);
  57.             spriteBatch.DrawString(spriteFont, fps, new Vector2(2, 2), Color.White);
  58.  
  59.             spriteBatch.End();
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment