FPS display error

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Terry
Posts: 55
Joined: Wed Jan 27, 2010 6:16 am
Location: Peking/China

FPS display error

Post 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:
CuteAlien
Admin
Posts: 9716
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Maybe now-last is zero? Then you would try dividing by that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Terry
Posts: 55
Joined: Wed Jan 27, 2010 6:16 am
Location: Peking/China

Post 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.
Valmond
Posts: 308
Joined: Thu Apr 12, 2007 3:26 pm

Post 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.)?
Terry
Posts: 55
Joined: Wed Jan 27, 2010 6:16 am
Location: Peking/China

Post by Terry »

wow,i mean it displays like "…………FPS:@#!"
19 could not be displayed.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post 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.
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post 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.
Post Reply