Archived

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

olle55

writing a fps counter.

Recommended Posts

Copy & Paste right from my Direct3D game:



static int framesThisSec=0;
static int framePerSec=0;
framesThisSec++;
static unsigned int lastSec=timeGetTime();

if (timeGetTime() - lastSec >= 1000) {
framePerSec=framesThisSec;
framesThisSec=0;
lastSec = timeGetTime();
}
AnsiString str = AnsiString(framePerSec);
myEngine->font1->DrawText(10,10,0xffff00ff,str.c_str());



Hope this helps!

Share this post


Link to post
Share on other sites
if I''m not misstaken timeGetTime() is only reliable down to around 57ms...which means if you want better precision, you could use QueryPerformanceFrequency and QueryPerformanceCounter which counts micro seconds, though since it takes some time to call the functions it''s not absolut either.

definitions:
__int64 m_Frequency;
__int64 m_StartTime;
__int64 m_CurrentTime;

__int64 m_Temp;

double m_LastFrameTime;
double m_FPS;

init:
QueryPerformanceFrequency((LARGE_INTEGER *)&m_Frequency);
QueryPerformanceCounter((LARGE_INTEGER *)&m_StartTime);
m_CurrentTime = m_StartTime;


render:
QueryPerformanceCounter((LARGE_INTEGER *)&m_Temp);
m_LastFrameTime = (double)(m_Temp - m_CurrentTime) / (double)m_Frequency * 1000000.0;
QueryPerformanceCounter((LARGE_INTEGER *)&m_CurrentTime);

m_FPS = 1000000.0 / m_LastFrameTime;

sprintf(m_Output, "FPS: %3.3lf fps", m_FPS);

this method could be good for fps count and the timeGetTime method could be used for animations and such.

Share this post


Link to post
Share on other sites
There's a full frame timer tutorial with full source code at my site... you can check that out if you want

The site is in my sig...Triple buffer




"I know sometimes I ask stupid questions...but i mean well "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]


[Edit] forgot to tell teh site

[edited by - alfmga on October 3, 2002 3:32:34 PM]

Share this post


Link to post
Share on other sites
Though, Kaviar, I dont't really like your method.. I mean your doing 2 function calls and divisions- whereas I'm only doing an incrementation and check to see if a second has passed.

Also timeGetTime() is certainly much more accurate than 57ms. I think the official figure is 1ms and worst case is 10ms (don't remember where I read this). Anyways timeGetTime() seems to to be accurate enough to perform smooth framerate independent animation on my P4 2.0.

[edited by - Beowulf on October 3, 2002 3:34:14 PM]

Share this post


Link to post
Share on other sites
check out the timer class on my site. if checks if queryperformancecounter can be used on the computer, if not it uses timeGetTime(). is can be used for frame-based modeling, or locking the framerate. it also has a function to find fps.

My Homepage
How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.

Share this post


Link to post
Share on other sites
quote:
Also timeGetTime() is certainly much more accurate than 57ms. I think the official figure is 1ms and worst case is 10ms (don''t remember where I read this).


Correct timeGetTime is accurate to 1ms always i think, and i can tell you that you read this here

That article compares timeGetTime, GetTickCount, and QueryPerformaceCounter.

GetTickCount is the worst and Query.. is the best of em




"I know sometimes I ask stupid questions...but i mean well "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]

Share this post


Link to post
Share on other sites
hehe I stand correct, I mixed it up with GetTickCount.
but since the precision for timeGetTime is 1ms on win95 and on nt/2000 it can be 5ms or more, if you have a frame rate of 2-3000 how would you count that in ms, then you need something that can count micro seconds instead.
So maybe my method is a bit overkill ín some situations but it does show another way to count.
and to what beowulf said "...2 function calls and divisions- whereas I''m only doing an incrementation and check to see if a second has passed..." maybe I''m wrong but it looks like you call timeGetTime three times. I don''t know how long timeGetTime needs to return the time, but if you really need accuracy then I''m not so sure that this is to way to go.
Well I guess the best way to find out is just to try them both.

Share this post


Link to post
Share on other sites
alfmga, nice article on timers. I may just look into whether to use query.

I'm not calling timeGetTime() three times per frame, only one. First call is only called the very first time the function is called (because it is assigned to the static unsigned int). The second call is performed each frame. Finally the third call is only done once per second.



[edited by - Beowulf on October 3, 2002 5:04:19 PM]

Share this post


Link to post
Share on other sites
The x86 opcode rdtsc is precise to the CPU tick.
There''s some code on AMD''s website that shows how to calculate the MHz of the machine, and then rdtsc gives you nanosecond or even sub-nanosecond timing (if the machine is faster than 1GHz).

For this method and for the queryperformance one (which is extremely slow, takes about 50us on *fast* computer) you can calculate the divisor once and remember it.

You can use a low-pass filter to make it smooth.

Share this post


Link to post
Share on other sites