Advertisement

What's the flaw in this timer logic

Started by January 14, 2001 03:46 PM
1 comment, last by Prairie 24 years ago
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
What you really wanna do is time 10 frames, divide by 10 then take the reciprical to get the frame rate.
Advertisement
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

This topic is closed to new replies.

Advertisement