Search code examples
c#xnaxna-4.0xnanimation

How do i display/show FPS on screen?


I have this code sample i need to find how to show/display the frames per second on the screen. Tried to use the class but its not working good im not sure how to use it.

protected override void Initialize()
{
    base.Initialize();

    fpsm = new FpsMonitor();
    fpsm.Update();
    fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);

}

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
        ButtonState.Pressed)
        this.Exit();

    modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
        MathHelper.ToRadians(0.1f);

    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    // Copy any parent transforms.
    Matrix[] transforms = new Matrix[myModel.Bones.Count];
    myModel.CopyAbsoluteBoneTransformsTo(transforms);

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in myModel.Meshes)
    {
        // This is where the mesh orientation is set, as well 
        // as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationY(modelRotation)
                * Matrix.CreateTranslation(modelPosition);
            effect.View = Matrix.CreateLookAt(cameraPosition,
                Vector3.Zero, Vector3.Up);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45.0f), aspectRatio,
                1.0f, 10000.0f);
        }
        // Draw the mesh, using the effects set above.
        mesh.Draw();
    }
    base.Draw(gameTime);
}

And the FPS class:

public class FpsMonitor
{
    public float Value { get; private set; }
    public TimeSpan Sample { get; set; }
    private Stopwatch sw;
    private int Frames;
    public FpsMonitor()
    {
        this.Sample = TimeSpan.FromSeconds(1);
        this.Value = 0;
        this.Frames = 0;
        this.sw = Stopwatch.StartNew();
    }
    public void Update()
    {
        if (sw.Elapsed > Sample)
        {
            this.Value = (float)(Frames / sw.Elapsed.TotalSeconds);
            this.sw.Reset();
            this.sw.Start();
            this.Frames = 0;
        }
    }
    public void Draw(SpriteBatch SpriteBatch, SpriteFont Font, Vector2 Location, Color Color)
    {
        this.Frames++;
        SpriteBatch.DrawString(Font, "FPS: " + this.Value.ToString(), Location, Color);
    }
}

I tried in the Game1.cs to use it in the constructor:

fpsm = new FpsMonitor();
fpsm.Update();
fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);

But thats not how to use it. How do i use it and where in my code ?


Solution

  • From what I see

    fpsm = new FpsMonitor(); // in constructor
    fpsm.Update();// in update I think first line
    
    spriteBatch.Begin();  // in draw Last lines probably even after base.Draw(gameTime);
    fpsm.Draw(spriteBatch, fonts, new Vector2(5,5), Color.Red);
    spriteBatch.End();
    

    That should work like a charm

    [Edit for spritefont explanation]

    You will need to create a SpriteFont. To do that right click Content chose add new and create spritefont. It is an XML file that will be converted when you compile. you can change the font and height I suggest Arial and 10.... you can always change that.

    Next you need to load it. I usually do it like this.

    in the class

    private Spritefont Arial10;
    

    in the LoadContent

    Arial10 = Content.Load<Spritefont>("Arial10");
    

    Now you can do this

    spriteBatch.Begin();  // in draw Last lines probably even after base.Draw(gameTime);
    fpsm.Draw(spriteBatch, Arial10 , new Vector2(5,5), Color.Red);
    spriteBatch.End();