Archived

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

johnnyBravo

Best way to calculate frames per second, and how to do it?

Recommended Posts

Hey, I want to put up a frames counter per second for my program. My attempt at it failed, even though i don''t see why. But anyway I''m wondering what the best timer function would be and how to use it with a frames counter per second? Thanks

Share this post


Link to post
Share on other sites
"But anyway I'm wondering what the best timer function would be and how to use it with a frames counter per second?"

on windows? unix?

I don't know if that's "the best", but...

if it's on windows, I'd use QueryPerformanceCounter() and QueryPerformanceFrequency()...
if it's on unix, have a look at gettimeofday() manual pages...


[edited by - sBibi on July 26, 2003 9:44:44 PM]

Share this post


Link to post
Share on other sites

mytime = GetSystemTime() ''Milliseconds
framecounter = 0
lastFps = 0
do

DoFrame()
print lastFps
framecounter+1

if (GetSystemTime() - mytime >= 1000) '' one second
mytime = GetSystemTime()
lastFps = framecounter
framecounter = 0
end if
loop


This may pseudo work...

Share this post


Link to post
Share on other sites
My FPS update code (C#), part of class FrameCounter:


public void Update()
{
// number of frames since game start
++m_totalFrameCount;
// number of frames since last fps update
++m_framesSinceUpdate;

// m_method is a certain timing method (exchangable)
// Time returns the current time in milliseconds
float now = m_method.Time;
float delta = now - m_lastUpdate;

// m_frameDuration is the length of last frame
m_frameDuration = now - m_lastFrameTime;
m_lastFrameTime = now;

// m_updateInterval is the number of
// milliseconds between two updates
if(delta > m_updateInterval)
{
// m_currentFps should explain itself
m_currentFps = 1000.0f * ((float)m_framesSinceUpdate) / delta;
// m_samplesForAvgFps: number of samples used
// to calculate an average frame rate
++m_samplesForAvgFps;
// calculate average FPS
m_avgFps += (m_currentFps - m_avgFps) / (float)m_samplesForAvgFps;
// reset some vars
m_framesSinceUpdate = 0;
m_lastUpdate = now;
// fire .NET event
OnUpdated();
}
}


Hope you understand it

Regards, VizOne

-----------------------------
Andre Loker
GentleStorm Studios - Germany
http://www.gentlestorm.de

[edited by - VizOne on July 27, 2003 11:02:32 AM]

Share this post


Link to post
Share on other sites