Is there a proper way to calculate frame rate?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
WilsonHuang
Posts: 22
Joined: February 13th, 2019, 3:23 am

Is there a proper way to calculate frame rate?

Post by WilsonHuang » December 23rd, 2019, 4:18 am

The following code is how I calculate frame rate:

Code: Select all

[Game.h]
FrameTimer ft;
float totalFrameTime = 0.0f;
int frameCounter = 0;
float fps = 0.0f;

[Game.cpp]
void Game::Go()
{
	gfx.BeginFrame();	
	UpdateModel();
	ComposeFrame();
	gfx.EndFrame();
	frameCounter += 1;
}

void Game::UpdateModel()
{
	totalFrameTime += ft.GetFrameTime();
	fps = frameCounter / (totalFrameTime / 1.0f);
	std::wstringstream stream;
	stream << std::setprecision(4) << fps;
	wnd.SetWindowTitle(stream.str());
}
I notice a problem is that if I don't close the problem, the two variables "frameCounter" and "totalFrameTime" will be overflow one day.
How do I prevent this? Or is there a proper way to calculate frame rate? Any advice is welcome!

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Is there a proper way to calculate frame rate?

Post by albinopapa » December 23rd, 2019, 4:53 am

Well, your frame count will overflow way before the total frame time, but the easiest way would be to just reset the time and count every so often. Maybe every 10 seconds or every 1,000 frames.

You could also switch to using a uint64_t type ( the built in type or intrinsic type would be unsigned long long ) which would count to ‭‭18,446,744,073,709,551,616‬ before needing to roll back to 0.

Assuming 60 frames per second, you could get this to run for
‭9,749,040,289.251200541180449856249‬ years and never have rollover.

You'd also probably want to change the float totalFrameTime to a double totalFrameTime to maintain precision.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

WilsonHuang
Posts: 22
Joined: February 13th, 2019, 3:23 am

Re: Is there a proper way to calculate frame rate?

Post by WilsonHuang » December 23rd, 2019, 5:08 am

albinopapa wrote:
December 23rd, 2019, 4:53 am
Well, your frame count will overflow way before the total frame time, but the easiest way would be to just reset the time and count every so often. Maybe every 10 seconds or every 1,000 frames.

You could also switch to using a uint64_t type ( the built in type or intrinsic type would be unsigned long long ) which would count to ‭‭18,446,744,073,709,551,616‬ before needing to roll back to 0.

Assuming 60 frames per second, you could get this to run for
‭9,749,040,289.251200541180449856249‬ years and never have rollover.

You'd also probably want to change the float totalFrameTime to a double totalFrameTime to maintain precision.
Thanks for your advice, I'll take that.

Post Reply