SFML community forums

Help => Graphics => Topic started by: tortoise74 on September 19, 2020, 03:30:29 pm

Title: Slow motion
Post by: tortoise74 on September 19, 2020, 03:30:29 pm
Hi,
    New user here. I have a very simple SFML test application on ubuntu 20.04 using SFML 2.5.1.
There are three circles. One under user control and one bouncing left to right.

Problems:

Put simply it feels clunky.
So I am assuming I've made a noob error of some kind.
However, this seems to be the same basic loop SFML commonly used.
How can I make this fast and slick feeling instead?


#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    const float FPS = 120.0f; //The desired FPS. (The number of updates each second).
    bool redraw = true;      //Do I redraw everything on the screen?
    window.setFramerateLimit(FPS);
    sf::CircleShape shape2(100.f);
    sf::CircleShape shape(10.f);
    sf::Clock clock;
    shape2.setFillColor(sf::Color::Green);
    shape.setFillColor(sf::Color::Red);

    sf::CircleShape alien(10.f);
    alien.setFillColor(sf::Color::Yellow);
    auto alienSpeed = 1.f;
   
    while (window.isOpen())
    {
        sf::Event event;
        float xDelta = 0;
        float yDelta = 0;
        bool moved = false;
        if (window.pollEvent(event))
        {
           if (event.type == sf::Event::Closed)
           {
              window.close();
           }
           if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
               xDelta -= 1.f;
               moved = true;
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
               xDelta += 1.f;
               moved = true;
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
               yDelta -= 1.f;
               moved = true;
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
               yDelta += 1.f;
               moved = true;
            }
        }

        if (xDelta !=0 || yDelta != 0)
        {
           shape.move(xDelta,yDelta);
        }

        alien.move(alienSpeed, 0.f);
        if (alien.getPosition().x >= 190)
        {
           alienSpeed = -1.f;
        }
        else if (alien.getPosition().x <= 0)
        {
           alienSpeed = 1.f;
        }

        if(redraw)
        {
           window.clear();
           window.draw(shape2);
           window.draw(shape);
           window.draw(alien);
           window.display();
        }
    }

    return 0;
}


Note: cross posted to https://gamedev.stackexchange.com/questions/185873/sfml-test-app-feels-slow-clunky
Title: Re: Slow motion
Post by: eXpl0it3r on September 19, 2020, 05:57:39 pm
You should not be mixing events and real-time inputs, see the dedicated tutorials.

Events: https://www.sfml-dev.org/tutorials/2.5/window-events.php
Real-time input: https://www.sfml-dev.org/tutorials/2.5/window-inputs.php
Title: Re: Slow motion
Post by: tortoise74 on September 19, 2020, 09:39:16 pm
That's much better. It is as I thought a noob error.
With isKeyPressed outside the loop its much better.
It should be:

        if (window.pollEvent(event))
        {
           if (event.type == sf::Event::Closed)
           {
              window.close();
           }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
               xDelta -= 1;
        }

Thank you.
Title: Re: Slow motion
Post by: eXpl0it3r on September 20, 2020, 08:38:50 am
Keep in mind though, as someone commented on your StackExchange question, you should use a time step/framerate independent calculation.
For example you can take the duration of the last frame and multiple the wanted velocity by it.