XNA FPS COUNTER C#

oxide
11 years ago

0

``` 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();  
    }  
}```  

just some code i found via google on pastebin

0replies
1voice
193views
You must be logged in to reply to this discussion. Login
1 of 1

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss