Jump to content
  • Advertisement

Archived

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

johnE

Framerate counter?!?

This topic is 6255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I remembered seeing a demo on this site having a framerate counter, but now I cannot find it anymore. Is there any copy/paste code available?! (I would like to do some tuning on my engine). Thanx JohnE (A clean desk is a sure sign of a sick mind, Albert Einstein)

Share this post


Link to post
Share on other sites
Advertisement
Copy paste......not good!!

Make on your self....it''s not that hard....!!


Take Care!

- -- ---[XaOs]--- -- -

Share this post


Link to post
Share on other sites
here''s some pseudo-code for a frame-counter -

void Render(void)
{
DWORD t = GetTickCount()
/* whatever your API timing function is */

// all your rendering and stuff
DWORD fps = (1000 / (GetTickCount() - t));

char* fps_buffer;
sprintf(fps_buffer, "Frame Rate: %d", fps);

// imaginary function that prints a string in the window
PrintString(fps_buffer);
}

Share this post


Link to post
Share on other sites
Here''s how I did it. Couple of notes: 1st is that division by zero errors that some algorithms are prone to due to inaccurate timers at too high a framerate is avoided here as mine has no division at all.

Second note to make is that this counter is only accurate to 1 frame per second. So you won''t see 30.4873 fps instead 30 fps. but do you even care how many 10ths or hundredths of frames per second you''re running at? Sure the accuracy may look nice but quite franklyi couldn''t care less about less than 1 frame per second. Your needs may be different.

Note that if you want those decimal places of accuracy you''ll have to use QueryPerfomanceTimer() or some such and use the 1/frequency method of calculating rather than this algorithm.

//initialize these
DWORD currenttime;
DWORD starttime;
int frames;
int fps;

//stick this at the beginning of your draw code
currenttime=GetTickCount();
frames++;
if ((currenttime-1000)>=starttime) //after 1 second
{
fps=frames; //set fps=# of frames after 1 second
starttime=currenttime; //reset the timer
frames=0; //set frame counter back to zero
}
...
glPrint(50,16,1,"FPS=%4i", fps);

Share this post


Link to post
Share on other sites
At the start of the function your main game loop is in, declare 3 LARGE_INTEGER variables, eg:

LARGE_INTEGER start_time, tmp_time, freq;

Then record the frequency of the timer with:

QueryPerformanceFrequency(&freq);

Now during your game loop, add to the top:

QueryPerformanceCounter(&start_time);

Then at the end, add:

QueryPerformanceCounter(&tmp_time);
framedelta = (float)(tmp_time.QuadPart - start_time.QuadPart) / freq.QuadPart;

Your FPS will be equal to 1/framedelta. Simple

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi there!!!

this is taken straight out of nehe''s game tutorial #4.

#include

static FLOAT fps = 0.0f;
static FLOAT last_time = 0.0f;
static DWORD frames = 0L;
float times;


/*then in the game loop*/
times= timeGetTime()*0.001f; // Get current time in seconds
++frames;

// Update the frame rate once per second
if(times-last_time>1.0f)
{
fps = frames/(times-last_time);
last_time= times;
frames = 0L;
}

(fps = current frames per second)

simple! you dont even have to understand it if you dont want to


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ignore the "#include" in the last reply, it was a typo

Share this post


Link to post
Share on other sites
Ah I hadn''t gotten to the game tuts yet.

I notice a multiplication by 0.001f instead of a division by 1000, very clever seeing as multiplication is faster on most processors than division!

thanks for that post

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!