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

Author Topic: setFramerateLimit causes major stuttering  (Read 2359 times)

0 Members and 1 Guest are viewing this topic.

PixelMuffin

  • Newbie
  • *
  • Posts: 18
    • View Profile
setFramerateLimit causes major stuttering
« on: July 16, 2012, 02:46:35 am »
I know there are other threads about this but I haven't found it resolved at all.

Ideally, I would like the game not to use 100% of its allotted CPU budget so that's why I would like to limit the FPS to 60. Doing so however cause a ton of stuttering where the object will stall and hang. It's very minor but obvious. How do big companies get away with completely smooth rendering? Have any of you found a way to make it incredibly smooth.

Here is a VERY simple example that stutters on the latest Mac OSX 10.7.4.

Let me know how it runs for you and what system you are on.

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window( sf::VideoMode( 800, 600 ), "Test" );
    window.setVerticalSyncEnabled( true );
    window.setFramerateLimit(60);
   
    sf::Vector2f player1(380, 280);
    sf::RectangleShape paddle1(sf::Vector2f(40, 40));
   
    sf::Clock clock;
    sf::Time time;
   
    float dt = 0;
    const int speed = 100;
   
    // 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;
       
       
        // 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;
}

If we can get this fixed, that would be great. Let's pool information if we can.

Edit: Seems that this is fixed on Windows 7 by making it full screen. Going to full screen on OSX makes the screen shift up by 1/4-1/3. Not sure why. I still see the stuttering though.
« Last Edit: July 16, 2012, 04:10:09 am by PixelMuffin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: setFramerateLimit causes major stuttering
« Reply #1 on: July 16, 2012, 10:20:32 am »
It's quite hard to tell what mean exactly by studdering. I have never nogiced any studdering with setFramerateLimit, then again I'm not using a Mac.
With a speed of 100 and an average dt of 0.166666... you get a total movement of 1.6units. If you the default view this is 1.6 pixels. Since a screen can only display full pixels, OpenGL has to decide if it should round up or down. This means that it is quite possible that from time to time the sprite might move 2pixels and often just 1px. Thos could probably look like a studdering.
So a solution to this would be to match the speed withe fps which isn't that nice.
Maybe it's a totally diffrent problem...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: setFramerateLimit causes major stuttering
« Reply #2 on: July 16, 2012, 04:33:10 pm »
Going to full screen on OSX makes the screen shift up by 1/4-1/3. Not sure why.
This bug was fixed after the RC was built.

BTW, thanks for you code, I tested it and found a bug in sf::Keyboard. It's now fixed too.  :)

SFML / OS X developer

 

anything