Page 1 of 1

FPS display error

Posted: Thu Jun 10, 2010 10:52 am
by Terry
hello folks,

Maybe it's a easy problem,i'd like to compute fps by my own.but it displays error.

Right code:

Code: Select all

fps = driver->getFPS();
if (lastFPS != fps) 
{ 
core::stringw str = L"Collision detection example - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += (int)fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
} 
but if i change first sentence to:

Code: Select all

now = device->getTimer()->getTime();
fps = (int)(1000.0f/(now-last));
"last"has been computed before,it could not display the fps infor,
what's wrong?

Thank you very much! :lol:

Posted: Thu Jun 10, 2010 11:23 am
by CuteAlien
Maybe now-last is zero? Then you would try dividing by that.

Posted: Thu Jun 10, 2010 11:24 am
by Terry
CuteAlien wrote:Maybe now-last is zero? Then you would try dividing by that.
No i've traced the code step by step,it's not zero.and fps is 19.

Posted: Thu Jun 10, 2010 6:01 pm
by Valmond
If you want 'perfect' fps you need to save off the reminder of that division (so this frame when fps=19.5 and shows 19, the 0.5 should go to the next frames calculation).

Otherwise, what's the problem with the code (crashes, slightly wrong fps, not at all correct etc.)?

Posted: Fri Jun 11, 2010 12:35 am
by Terry
wow,i mean it displays like "…………FPS:@#!"
19 could not be displayed.

Posted: Fri Jun 11, 2010 12:55 am
by greenya
I see "now" assignment, but "last" -- i don't.
So, after this:

Code: Select all

now = device->getTimer()->getTime(); 
fps = (int)(1000.0f/(now-last));
you have to do also

Code: Select all

last = now;
But to make sure never have devision by zero, i would wrote: last = now - 1;

Maybe that is a problem.

Posted: Fri Jun 11, 2010 5:04 am
by DtD
Since you said it is displaing random characters, my guess is it is because you are adding the FPS in like it is text, do this instead:
str += irr::core::stringw(fps);

Edit: However, the divide by zero is an important issue as well. I output the time since the last frame along with the FPS in my game and I notice it is very common it is 0 on my machine.