1
\$\begingroup\$

I have a very simple event handeling system at the moment.

void LevelScreen::PollInput()
{
    sf::Event event;

    while (pWindow->pollEvent(event))
    {
        if (event.type == sf::Event::KeyPressed)
        {
            int x = 0;
            int y = 0;

            if (event.key.code == sf::Keyboard::Left)
            {
                x = -1;
            }
            else if (event.key.code == sf::Keyboard::Right)
            {
                x = 1;
            }
            else if (event.key.code == sf::Keyboard::Up)
            {
                y = -1;
            }   
            else if (event.key.code == sf::Keyboard::Down)
            {
                y = 1;
            }

            pCursor->Move(x, y);
        }
    }
}

The system gets updated every single cycle, yet many keypresses are not even registered eventhough there is no lag. Mouse movement is always registered, but when I press, for example, Down alot it fails to register from 30% to 50% of the time. Same counts for the mouse button, if I hold it down I can see that it registers it for 6 times in a row, then stops for about 5 seconds, continues, etc.

Putting it in the main loop doesn't change anything. I cannot find any reason for this input lag.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Which OS are you on? Relying on the key repeating events sent by the OS is not a good idea, as that can be very inconsistent. Are you actually missing events if you repeatedly push & release buttons, or does the inaccuracy happen if you hold the buttons down? \$\endgroup\$
    – user35344
    Jul 23, 2016 at 15:21

1 Answer 1

1
\$\begingroup\$

Have you tried using instead sf::Keyboard::isKeyPressed instead of an event based solution? (see http://www.sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php)

static bool     isKeyPressed (Key key)

You could use this in a way similar to how you have it, after your input method.

Sample code:

while (window.isOpen()) {
    // handle input

    // after pollEvent loop
    sf::Vector2f movement(0.f, 0.f);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
        movement.y = -1;
    } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
        movement.y = 1;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
        movement.x = -1;
    } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        movement.x = 1;
    }

    pCursor->move(movement); // assuming pCursor is sf::Transformable*
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .