Home > other >  how does SFML internally maintain the frame rate limit when setFramerateLimit() function is used?
how does SFML internally maintain the frame rate limit when setFramerateLimit() function is used?

Time:10-30

window.setFramerateLimit(60);
while(window.isOpen())
{
    //manage events
}

this above while loop runs 60 times in 1 second after we set the frame rate limit, I want to know how does SFML internally manage to run the while loop in a controlled manner(i.e maintaining frame rate) by just using the "window.isOpen()" function in while loop condition. Or is there anything else working under the hood.

I am unable to understand how does setFramerateLimit works in SFML.

CodePudding user response:

Setting setFramerateLimit on the window will add a delay after you call window.display(). If you remove the call to this function your loop would iterate without the limit control set by SFML.

CodePudding user response:

SFML limits the frame rate by sleeping the thread for a short interval each time you call window.display(). This can be seen in the source code:

void Window::display()
{
    // Display the backbuffer on screen
    if (setActive())
        m_context->display();

    // Limit the framerate if needed
    if (m_frameTimeLimit != Time::Zero)
    {
        sleep(m_frameTimeLimit - m_clock.getElapsedTime());
        m_clock.restart();
    }
}

Without calling this function once per loop, there is no frame rate limiting.

  • Related