Search code examples
c++sfmlframe-rate

setFramerateLimit() function not working in sfml


I am trying learn SFML and I want to limit the frame rate . here is my code:-

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

int main()
{
    sf::Window win (sf::VideoMode(200,200),"SDSDefgwre");
    sf::Clock clock;
    win.setFramerateLimit(30);

    sf::Time t;
    while(win.isOpen())
    {
        sf::Event e;
        clock.restart().asSeconds();
        while(win.pollEvent(e))
        {
            if(e.type == sf::Event::Closed)
                win.close();
        }    
        t = clock.getElapsedTime();    
        std::cout << 1.f/t.asSeconds() <<'\n';    
    }   
    return 0;
}

Where I run this code I get 200000 FPS. which means

    win.setFramerateLimit(60);

is not working. Please tell me where am going wrong ?


Solution

  • From the documentation of setFrameLimit:

    If a limit is set, the window will use a small delay after each call to display() to ensure that the current frame lasted long enough to match the framerate limit.

    You do not render anything, and you never actually swap the draw buffers (which is what the call to win.display() would do).