Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is better sf::sleep(sf::milliseconds()); or window.setFramelimit(); ?  (Read 2629 times)

0 Members and 1 Guest are viewing this topic.

sezium

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hello everyone, I'm trying to understand delta time once again, and with this code:

#include <chrono>
#include <SFML/Graphics.hpp>


int main() {

    constexpr unsigned char MAX_FPS = 60;

    constexpr std::chrono::microseconds FRAME_DURATION(1000000 / MAX_FPS);
        std::chrono::steady_clock::time_point previous_time;
        std::chrono::microseconds lag(0);

        sf::Event event;

        sf::RenderWindow window(sf::VideoMode(500, 500), "SFML", sf::Style::Close);
    //window.setFramerateLimit(MAX_FPS);

        previous_time = std::chrono::steady_clock::now();

        while (window.isOpen()) {

                std::chrono::microseconds dt = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - previous_time);

                lag += dt;

                previous_time += dt;

                while (FRAME_DURATION <= lag) {

                        lag -= FRAME_DURATION;

                        while (window.pollEvent(event)) {
                                switch (event.type) {

                                        case sf::Event::Closed: {
                                                window.close();

                                                break;
                                        }
                                }
                        }



                        if (FRAME_DURATION > lag) {

                                window.clear();

                                window.display();

                //sf::sleep(sf::milliseconds(50));
                        }
                }
        }
}
 

Is better to use sleep or setFramelimit? what are the differences in this case? Btw when I use none of them, my cpu usage is high.
« Last Edit: April 06, 2024, 09:11:41 pm by sezium »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Is better sf::sleep(sf::milliseconds()); or window.setFramelimit(); ?
« Reply #1 on: April 07, 2024, 01:34:29 am »
Internally, setFrameLimit uses sf::sleep anyway.
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();
    }
}

The problem with either is sf::sleep uses the operating system sleep, which can be inaccurate. For example on windows the sleep will be at least what you specify, but may also be longer. So you may not get a consistent framerate. Vsync is more reliable since the drivers are waiting for a fixed rate hardware event.

A basic busy loop will use more cpu, but will be more reliable without vsync.

You could try a combo, check how long is remaining until the next frame, then sleep for a small amount (like 1ms) if the remaining is greater than maybe 2ms. Otherwise busy loop. So a sleep that's too short or long probably won't affect it.

 

anything