SFML community forums

Help => Window => Topic started by: posva on July 22, 2013, 12:55:30 pm

Title: Framerate Limit not working properly on OSX
Post by: posva on July 22, 2013, 12:55:30 pm
Testing the same piece of code on Windows, Linux (ubuntu) and OS X (10.8.4) doesn't have the right FPS on OSX.
Here is the code sample (from base project)

#include <SFML/Graphics.hpp>

// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
#include "FPS.hpp"

int main(int, char const**)
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    window.setFramerateLimit(60);

    sf::Texture texture;
    if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
        return EXIT_FAILURE;
    }
    sf::Sprite sprite(texture);

    sf::Font font;
    font.loadFromFile(resourcePath()+"sansation.ttf");

    FPS fps(font);
    fps.setColor(sf::Color::Black);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
                window.close();
        }

        window.clear();
        window.draw(sprite);

        fps.step();
        window.draw(fps);

        window.display();
    }

    return EXIT_SUCCESS;
}

This gives a framerate of 57 while it should be 60

(https://f.cloud.github.com/assets/664177/834364/09971066-f2bc-11e2-9ff8-ca7013eb1262.png)

The FPS.hpp can be found here
https://gist.github.com/posva/6052868 (https://gist.github.com/posva/6052868)

I created a (rejected) issue here: https://github.com/SFML/SFML/issues/433 (https://github.com/SFML/SFML/issues/433)
Title: Re: Framerate Limit not working properly on OSX
Post by: eXpl0it3r on July 22, 2013, 01:06:59 pm
Quote from: SFML Tutorial - Window (http://www.sfml-dev.org/tutorials/2.0/window-window.php#controlling-the-framerate)
window.setFramerateLimit(30); // call it once, after creating the window
Unlike setVerticalSyncEnabled, this feature is implemented by SFML itself, using a combination of sf::Clock and sf::sleep. An important consequence is that it is not 100% reliable, especially for high framerates: sf::sleep's resolution depends on the underlying OS, and can be as high as 10 or 15 milliseconds. Don't rely on this feature to implement precise timing.

sf::sleep can be quite inaccurate. ;)
Title: Re: Framerate Limit not working properly on OSX
Post by: posva on July 22, 2013, 01:17:39 pm
Yeah, I know that, I actually tried to hard-code the way SFML2 does the framerate limit to find a workaround.
Still, it's quite annoying having this kind of difference between 2 OS
But thank you for the answer.

Dynamic framerate gives me headache >.<
Title: Re: Framerate Limit not working properly on OSX
Post by: Laurent on July 22, 2013, 01:44:45 pm
Quote
Still, it's quite annoying having this kind of difference between 2 OS
You're not using the right tools.

If you want a fixed timestep, read articles about it, there are well known implementations.

If you want a "good" framerate, to avoid huge or tiny update times, and thus potential graphical artifacts, enable vertical synchronization.

In any case, if your code is broken because of +-2 FPS, then it's badly written.
Title: Re: Framerate Limit not working properly on OSX
Post by: posva on July 22, 2013, 02:08:31 pm
If you want a fixed timestep, read articles about it, there are well known implementations.

Didn't know about that, thank you, I'll search a bit.

If you want a "good" framerate, to avoid huge or tiny update times, and thus potential graphical artifacts, enable vertical synchronization.
That's actually what I do :p But I remeber there were still some OS strange behaviour, I'll recheck this

In any case, if your code is broken because of +-2 FPS, then it's badly written.
No, it's not broken :P
The +-2FPS really change the game when it's FPS fixed :P And dynamic FPS make it even worse