Frame Rate Lock

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
julescoder
Posts: 34
Joined: Thu Feb 18, 2010 5:24 pm

Frame Rate Lock

Post by julescoder »

Hi,
How can i lock the irrlicht frame rate to a chosen value ?
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Search for it, this question was posted many times. And it's not really a great idea to lock the framerate as some computers may run slower than the framerate you try to lock it.

But if you really need to lock it than in the update loop you should get delta time of the frame using irrlicht timer and yield it until the time you want another frame to begin.
Working on game: Marrbles (Currently stopped).
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post by cobra »

I suggest you use framerate-independent movement.

http://www.gamedev.net/reference/articl ... le1382.asp
Josiah Hartzell
Image
julescoder
Posts: 34
Joined: Thu Feb 18, 2010 5:24 pm

Post by julescoder »

I realize that using frame rate independent movement would be good.
But i am working on a networked game. So i want to make sure that all clients are running at same frame rates. Thats why i want to lock it.

I will try the yield function technique above. Any other suggestions would be welcome.

Thanx.
no_response
Posts: 26
Joined: Fri Aug 20, 2010 11:07 am
Location: Bulgaria

Post by no_response »

what I do is...

Code: Select all

unsigned int FPS=0;
    wchar_t* FPSwc = (wchar_t*) calloc(10, sizeof(wchar_t));
    unsigned short int time=0;
    unsigned short int dividor=2;
and in the main loop

Code: Select all

if (driver->getFPS()!=FPS){

         FPS = driver->getFPS();
         if((double)FPS/(double)desiredfps > 1.5 &&dividor>=4) dividor-=2;
         else if((double)FPS/(double)desiredfps > 1.15 &&dividor>2) dividor--;
         else if((double)FPS/(double)desiredfps < 0.5) dividor+=2;
         else if((double)FPS/(double)desiredfps < 0.85) dividor++;
            
            /* display the fps
            _itow(FPS, FPSwc, 10);
            fpsdisp->setText(FPSwc);
           */
         }
     time++;
     if(!(time%dividor)){ // on every dividor-1 frames, one Sleep will be called
        Sleep(10); //maintaining set FPS
        time=0;}
it may take several seconds to adjust to the desired FPS, but it pretty much does what it's supposed to.
also, by changing '1.15' and '0.85' to numbers, closer to 1, will make it more accurate.
yet this is not something you should rely on.
for example, I use it to lower the CPU usage.
julescoder
Posts: 34
Joined: Thu Feb 18, 2010 5:24 pm

Post by julescoder »

I will experiment with your code as well. But as u said the sleep function can waste CPU cylces.
Could there be a way without calling sleep .

Thanx for all your answers again.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

julescoder wrote:I will experiment with your code as well. But as u said the sleep function can waste CPU cylces.
False... Sleep actually frees up the CPU cycles that would normally be used by the current thread to other threads on the system.

If I were to lock the frame rate, I'd check the clock at the beginning of the frame and the end of the frame. If the difference between the two was smaller than the time per frame I was aiming for, I'd call sleep with the time the frame took to render subtracted from the time per frame you want.
Post Reply