SFML community forums

Help => General => Topic started by: utan on January 30, 2021, 01:23:37 am

Title: setFramerateLimit() && setVerticalSyncEnabled()
Post by: utan on January 30, 2021, 01:23:37 am
Hi all,

So I am getting weird results when moving my character is not smooth.. it actually skips at the middle center of the window.. When I disable both functions it doesn't appear to skip anymore..

I try to do bare bone to see if it was something I did in the code, I found this in the forum and would like to know if you see frame skipped using the example below because in my computer it seems to do..

thanks..

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    window.setVerticalSyncEnabled(true);

    sf::Vector2f player1(10, 10);
    sf::RectangleShape paddle1(sf::Vector2f(10, 30));

    sf::Clock clock;
    sf::Time time;

    float dt = 0;
    const int speed = 500;

    // Start the game loop
    while (window.isOpen())
    {

        // Poll for events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();

            // Escape pressed : exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
        }


        // Check for user input
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            player1.y += speed * dt;

        // Check for user input
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            player1.y -= speed * dt;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            player1.x -= speed * dt;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            player1.x += speed * dt;


        // Restart the clock and get the delta time
        time = clock.restart();
        dt = time.asSeconds();


        // Update the object positions
        paddle1.setPosition(player1.x, player1.y);

        // Clear screen
        window.clear();

        // Draw the sprites
        window.draw(paddle1);

        // Update the window
        window.display();
    }

    return EXIT_SUCCESS;
}
 

see in youtube
https://youtu.be/SlMg7JRVcx0
Title: Re: setFramerateLimit() && setVerticalSyncEnabled()
Post by: Stauricus on January 30, 2021, 02:23:14 am
setFramerateLimit() and setVerticalSyncEnabled() don't get along very well.
https://www.sfml-dev.org/tutorials/2.5/window-window.php#controlling-the-framerate

although you're not seting the framerate limit in the code you posted...  ???
Title: Re: setFramerateLimit() && setVerticalSyncEnabled()
Post by: utan on January 30, 2021, 03:10:38 am
well,

I formulated badly my post, if I use any of them I get the frame skipping ..

I have tested same example in AMD PC and an Intel one.. same result..

If I don't use any of them it works fine...

 I am don't know what is it..
Title: Re: setFramerateLimit() && setVerticalSyncEnabled()
Post by: utan on January 30, 2021, 03:43:56 pm
Hi,

Anyone able to duplicate the issue?
Title: Re: setFramerateLimit() && setVerticalSyncEnabled()
Post by: Stauricus on February 03, 2021, 01:09:27 pm
hello
to be honest, I don't notice this skipping you're talking about, even in your video
I tried it, seems fine to me

http://youtu.be/j8Q5_vZKG3o
(ignore the sound, I forgot the recorder was connected to the mic)

maybe its related to CPU's frequency. notice that you are also using time as seconds (and I'm not sure, but I think even 0.9 seconds may be interpreted as 0). so maybe you could use time as milliseconds (and adjust the calculations of the speed, of course)
Title: Re: setFramerateLimit() && setVerticalSyncEnabled()
Post by: eXpl0it3r on February 12, 2021, 11:23:56 am
"Skipping" or "stutters" are preceived when the movement is not perfectly linear, meaning that every frame is a) consistent in the timing and b) moves the same distance.

To solve this problem you need to Fix Your Timestep (https://gafferongames.com/post/fix_your_timestep/) so that your position calculation is consistent and independent of the framerate.
Essentially your physics should operate at a fixed time step, so that the position is moving linear and the rendering can happen at any time and vary in stability, yet the object will still appear at the correct position every rendered framed.

VSync doesn't guarantee a stable framerate and video recordings are framerate limited as well, so they may cause additional confusion.