-1
\$\begingroup\$

I've been working on this little framework for my game today and realized that moving delays when moving around. I've used SFML before and have had the same problem, but never bothered to fix it. I'm wondering if anyone has had a similar issue and has found a solution. Here's an example of the code which is giving me the slight pause in movement:

int main(){
    sf::RenderWindow window (sf::VideoMode(640, 480), "Window");
    //window.setVerticalSyncEnabled(true);

    sf::Texture tex;
    tex.loadFromFile("Assets/Textures/player.png");
    sf::Sprite s;
    s.setTexture(tex);

    while (window.isOpen()){
        sf::Event event;

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

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            s.move(0.01f, 0.0f);
        }

        window.clear();
        window.draw(s);
        window.display();
    }
}

The delay/pause/whatever I'm experience is very slight but I can notice it and it's really bugging me. Just seeing the slight break in movement when none of the values alter makes my programming mind extremely sad.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you better define "pause", "delay", and "slight"? What actually happens, specifically? Is it just this particular sprite which pauses while the rest of the game continues to animate? Or does the whole game freeze up for a moment? How long a delay are we talking about, here? A second? A millisecond? When does it happen -- just when you press the button, or continuously, or what? \$\endgroup\$ – Trevor Powell Apr 15 '14 at 0:12
0
\$\begingroup\$

I've had the same type of quirk, and after some experimentation I've found that it's caused by the while (window.pollEvent(event)) loop.

I re-worked my method just to read the message

(window.pollEvent(event));

at the beginning of the

while (window.isOpen()) {

and then go from there. This causes the frame rate to skyrocket, and you'd definitely want to put in a tick System, but unfortunately I'm in class at the moment. When I get home I'll post my code snippet.

\$\endgroup\$
  • \$\begingroup\$ What exactly do you mean? This sounds more likely like a bug, either in SFML or in your code? Do you mean you no longer check for all events and only read one? \$\endgroup\$ – Mario Jun 15 '14 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.