Archived

This topic is now archived and is closed to further replies.

Possibility

How do you make a frame rate counter?

Recommended Posts

Hi, I am programming with VC++ and i was wondering how I can tell what the frame rate of my tile based games is. Is there an external program I can use, or better yet, some code I can put into my program that will display the real time frame rate while the game is running. So can anyone help me with this? Thanks Possibility

Share this post


Link to post
Share on other sites
I think you can use QueryPerformanceClock (or is it QueryPerformanceCounter?) to get a super-accurate clock reading, to a little better than 1/1000 of a second. There are a few ways to calculate FPS, but an easy way is to find the difference in clock ticks over a number of frames. Once that's found, you can get an average reading, and use the constants and definitions provided in VC++ to work out the number of frames you have per second. There's a definition somewhere in the help file for how many clock ticks QueryPerformanceCounter (or whatever it is) registers per second. With a little maths you should be able to figure out your FPS from there.

Sorry I can't help further; I don't have my code with me right now.

-----------

C++ is the language of the not-so-ancients.
Learn to speak it well.


BeamMeUp

Edited by - BeamMeUp on 4/5/00 10:23:57 PM

Share this post


Link to post
Share on other sites
There''s a couple ways to calculate frames, first you need some way to get time and the above post is the way to go.

Then you can either calculate the time it takes to render a single frame and divide a second by it, giving you frames per second, or you can count the number of frames and grab the exact clocktime after a certain time has passed. Then again, timeelapsed/countedframes.

the second one is a little bit more accurate (gives more of the same answer)

Share this post


Link to post
Share on other sites
GetTickCount() is the one with the frequency of 1,000 ticks per second.

QueryPerformanceCounter() is more like 1,000,000 ticks/sec.
Use QueryPerformanceFrequency() to get the exact number...it will return the number of ticks per second that QueryPerformanceCounter() is registering.

To calculate your frames per second real time for example:

/* Note, I use 32-bit long...but the return value is actually 64 bits I beleive. */

long start_time=0;
long frames=0;
long fps=0;

start_time = GetTickCount();
do
{
getinput();
movestuff();
drawstuff();

frames++;
if ((GetTickCount() - start_time) >= 1000)
{
//1000 ticks = 1 second, if 1000 ticks
//have passed since start_time was recorded,
//we can get the frames per second.
fps = frames;
//reset frames counter...
frames=0;
//update the start_time to the current "tick".
start_time = GetTickCount();
}
output(fps);
}while(gamerunning == 1);

I haven''t tested this code, I''m sure it gets the job done basically though.
Once 1000 ticks have passed, you know the fps...if you want a faster update, wait 500 (.5 seconds) instead of a 1000, then just double it. Want a more averaged fps, wait 5000 ( 5 seconds)then divide by 5. I think I''ve really dumbed this down...I''m sure you can figure it all out.
Have fun.

When life hands you lemons, throw them at God's head.

Share this post


Link to post
Share on other sites
Cool, I am already using that QueryPerformanceCounter in my game, but I completely forgot about it. I am at work right now so I cant test it out, but I think this would work:

Right before I do the final flip to the primary surface, I can do:
PreviousTime = NewTime;
NewTime = QueryPerformanceCounter();
NumTicks = QueryPerformanceFrequency();
fps = 1 / (NewTime/NumTicks - PreviousTime/NumTicks);

and then just do a textout of the fps and then flip to the primary surface.

Thanks for the info!
Possibility

Share this post


Link to post
Share on other sites
If you''re using DirectX, by the way, you can have DirectDraw output the frame rate to your debug window periodically... you just have to select that option in Control Panel -> DirectX Properties -> DirectDraw -> Advanced Settings. This is assuming you have the debug version of DirectX installed. I''ll be honest: I just started reading my DirectX book (Inside DirectX) today, and I haven''t actually tried this yet. I''m just quoting them.

Share this post


Link to post
Share on other sites