Game Development Community

Performance / FPS how to display

by Andy S. · in Torque X 2D · 10/24/2007 (12:23 pm) · 6 replies

Hi,
i know this is a totally noobish question, i already searched the forums, but did not find an answer...so here goes:
How can i put an FPS-counter in my game?Are there functions built in for this? Or does it have to be created in XNA? Maybe someone has a code example?

Thanks a ton
andi

#1
10/24/2007 (8:37 pm)
The following code will give you the FPS. You can then display it however you would like.
Just call CalculateFrameRate() each frame.

private static int lastTick;
private static int lastFrameRate;
private static int frameRate;

public static int CalculateFrameRate()
{
    if (System.Environment.TickCount - lastTick >= 1000)
    {
        lastFrameRate = frameRate;
        frameRate = 0;
        lastTick = System.Environment.TickCount;
    }
    frameRate++;
    return lastFrameRate;
}



For example, in your Game.cs file, put the above code and then add the following function.

protected override void Draw(GameTime gameTime)
{
    base.Draw(gameTime);

    this.Window.Title = "My Awesome Game - " + TorqueUtil.CalculateFrameRate() + "fps";
}
#2
10/24/2007 (11:30 pm)
Thanks a lot shawn, i am going to try this when i get home tonight ;-).
#3
10/25/2007 (6:05 am)
I get the following build error:

"GarageGames.Torque.Util.TorqueUtil" no definition for "CalculateFrameRate"

[EDIT]
oops- I must have been blind (or tired...):

this.Window.Title = "Strings - " + CalculateFrameRate() + "fps";
#4
10/25/2007 (12:09 pm)
Sorry, my faults!

I had it in TorqueUtil because I have TXPro and I threw it in there since it seemed like a utility function ;)
#5
10/25/2007 (1:05 pm)
No problem, works perfect ;-)
#6
10/25/2007 (2:11 pm)
Anytime.