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

Author Topic: setFramerateLimit() && setVerticalSyncEnabled()  (Read 1618 times)

0 Members and 1 Guest are viewing this topic.

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
setFramerateLimit() && setVerticalSyncEnabled()
« 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
« Last Edit: January 30, 2021, 01:30:29 am by utan »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: setFramerateLimit() && setVerticalSyncEnabled()
« Reply #1 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...  ???
Visit my game site (and hopefully help funding it? )
Website | IndieDB

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: setFramerateLimit() && setVerticalSyncEnabled()
« Reply #2 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..

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: setFramerateLimit() && setVerticalSyncEnabled()
« Reply #3 on: January 30, 2021, 03:43:56 pm »
Hi,

Anyone able to duplicate the issue?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: setFramerateLimit() && setVerticalSyncEnabled()
« Reply #4 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


(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)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: setFramerateLimit() && setVerticalSyncEnabled()
« Reply #5 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 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything