aj1204
aj1204

Reputation: 57

SFML Events not displaying?

The Events feature in my SFML program is not working. General structure of main() is below. I want to register the last key pressed and switch screens accordingly. I know below would require the key remainpressed, but even doing so as a test isn't working.

What syntax am I missing? The tutorials and sample code I've seen on forums have made it seem as straightforward as below. I have seen switch used on keycodes, but that seemed more for instantaneous response in video games.

int main()
{

    srand (time(NULL));

    sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Curling Party");

    window.setFramerateLimit(30);

   ...

    while(window.isOpen())
    {

        sf::Event event;

        window.pollEvent(event);

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Q)

        {

            window.close();
            break;
        }

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)

        {


            ...

            window.display();

        }


        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::B)

        {

            ...

            window.display();

        }

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
        {
           ...
        }


        window.clear(sf::Color::White);

        window.draw(house[0]);
        window.draw(house[1]);
        window.draw(house[2]);
        window.draw(house[3]);

        window.display();

    }

    return 0;

}

Upvotes: 1

Views: 513

Answers (1)

Jean Pierre Dudey
Jean Pierre Dudey

Reputation: 665

window.pollEvent(event) should be in a while loop as the documentation says:

A mistake that people often make is to forget the event loop, simply because they don't yet care about handling events (they use real-time inputs instead). Without an event loop, the window will become unresponsive. It is important to note that the event loop has two roles: in addition to providing events to the user, it gives the window a chance to process its internal events too, which is required so that it can react to move or resize user actions.

Altought window.display() should be called only at the end of the window loop, you don't need display the window after every key is pressed.

Upvotes: 2

Related Questions