How to fix fps rate

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
warren

How to fix fps rate

Post by warren »

Hi,
i´m trying to do some events synchronized with music, so i need the same speed on every computer. How can i set the frames per second to 30, for example???.

Thx in advance
CuteAlien
Admin
Posts: 9682
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Capping the framerate ain't necessarly a good idea (with animation stuff interpolating the positions is usually better), but basicly you just don't call the updates for drawing more often than you need it. Somthing like this (not compiled or checked):

const int timeStep = 33; // 1000 / fps
static int timeRest = 0; // using a membervariable would be cleaner
static int lastTime = 0; // using a membervariable would be cleaner
int timeNow =irrlichtDevice->getTimer()->getRealTime();
int timeSinceLastUpdate = timeNow - lastTime;
lastTime = timeNow;

// this could lead to very long times if your game was in background or in startup
// so usually as soon as your game starts "running" you should set lastTime
// to the getRealTime().
// The following lines just ignores very big timeStep (drops them completly)
if ( timeSinceLastUpdate > 5000 ) timeSinceLastUpdate = 0;

// instead of dropping the timeSinceLastUpdate you could also do somthing like
// the following if here:
// if ( IsGameRunning() )
timeRest += timeSinceLastUpdate;

// this code will not only cap the framerate but will also try to catch up on missed
// framerates.
while ( timeRest >= timeStep )
{
timeRest -= timeStep;

// do your update stuff here, like beginScene, drawAll, endScene
}
Post Reply