Jump to content
  • Advertisement

Archived

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

Prairie

What's the flaw in this timer logic

This topic is 6340 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

This function is very short and seems logically sound, and yet isn''t working. My FPSdisplay keeps decreasing, and there doesn''t seem to be a leak anywhere else in my code. *notes* prev_time is initialized as timeGetTime() before entering game loop. this function is called before my render routine in the game loop, which uses the FPSdisplay to draw the text.
void UpdateFPS()
{
	FPScount++;
	current_time = timeGetTime();
	elapsed_time += current_time - prev_time;

	if(elapsed_time >= 1000)
	{
		FPSdisplay = FPScount;
		FPScount = 0;
		elapsed_time = 0;
	}
	prev_time = current_time;

}
 
Any suggestions are welcome. thanks; -Prairie

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
What you really wanna do is time 10 frames, divide by 10 then take the reciprical to get the frame rate.

Share this post


Link to post
Share on other sites
Here is the way I do it.

  
//This first part is just called 1 single time, like at the beggining

//of winmain after the initializations

//________________________________________________________________________________________

// is there a performace counter available?

LONGLONG Freq;
LONGLONG NewTime;
LONGLONG PreviousTime;
bool bPerformanceCounter;


if (QueryPerformanceFrequency(( LARGE_INTEGER *) &Freq))
{
QueryPerformanceCounter((LARGE_INTEGER *) &NewTime);
bPerformanceCounter = TRUE;
}
else
{
NewTime=timeGetTime();
Freq = 1000.0f;
bPerformanceCounter = FALSE;
}
//________________________________________________________________________________________




//then this part is called every frame.


PreviousTime = NewTime;
if (bPerformanceCounter)
QueryPerformanceCounter((LARGE_INTEGER *) &NewTime);
else
NewTime=timeGetTime();

// calculate elapsed time

g_fDeltaTime = (NewTime - PreviousTime);
g_fDeltaTime /= Freq;
if (g_fDeltaTime < 0.0f) //if its negative, make it positive

g_fDeltaTime *= -1.0f;



then your FPS is:
float fps = 1.0f / g_fDeltaTime;

Possibility

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!