Game Development Community

Frames Per Second counter

by Tim Newsome-Ward · in Torque X 2D · 09/30/2010 (3:55 pm) · 10 replies

Hey guys,

I wasn't going to ask this but I've trawled through the forums and haven't really found a definitive answer.

What's the best practice to add an FPS counter into a project? Is the functionality already built in or will I need to add an additional component?

Cheers

#1
09/30/2010 (5:04 pm)
I have one built I'll post when I get home. :)
#2
09/30/2010 (6:12 pm)
@Matthew - Thanks!
#3
10/01/2010 (6:37 pm)
Dude, forgot about this will do tonight.
#4
10/01/2010 (6:52 pm)
Hey man, no worries, whenever you have chance! ;-)
#5
10/02/2010 (3:50 am)
So here is the code. Just put this in the "Update" function of your game.cs

elapsedFrameTime += gameTime.ElapsedGameTime;

if (elapsedFrameTime > TimeSpan.FromSeconds(1))
             {
                 elapsedFrameTime -= TimeSpan.FromSeconds(1);
                 if (guiDebug != null)
                 {
                     guiDebug.p1State = framerate.ToString();
                 }
                 framerate = 0;
             }

Then put this in your "Draw" method of the Game.cs
framerate += 1;

The above should tell you how many times your game has drawn in a sec. The guiDebug.p1State = framerate.ToString() line just writes text to the screen.

The above code probably creates a bunch of garbage so make sure you rip it out at ship. :)
#6
10/02/2010 (10:09 am)
Awesome! Cheers
#7
10/02/2010 (10:51 am)
Do I need to add some additional references to the xna framework? I'm building from a starter game template which dosen't have the update and draw methods, I've added those in though.

#8
10/03/2010 (1:37 am)
I didn't realize I added those in. :).

You just need to override em.

add this:
protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
}

and this:
protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
}


And just put the code in those methods below base.

Let me know if you run into any issues.
#9
10/03/2010 (1:21 pm)
Still having a few issues! It’s frustrating not being a coder sometimes, but I'm learning ;-)

What do I declare the variables as? I’ve tried float and doubles but I get "Cannot implicitly convert type 'System.TimeSpan' to 'float'" and other various related float and TimeSpan errors.

Oh and I'm also not sure how to declare guiDebug ;-)

I bet you wish you hadn't replied to the post now lol!
#10
10/04/2010 (2:22 am)
No worries. I'm still learning too and am not really a "coder" yet so I feel your pain.

You will want to declare elapsedframe time like this:
private TimeSpan elapsedFrameTime = TimeSpan.Zero;

and framerate like this:
protected int framerate;

GUI debug is not a TorqueX class but just a class I created that writes text to the screen.

Check out the Starter GUI project I belive that has text that writes to the screen. All you need to do is just replace this line:
guiDebug.p1State = framerate.ToString();

with something like this:
<the GUITEXT you write to the screen with> = framerate.ToString();

Let me know if you have any other questions :)