Search code examples
c++sfml

Variable don't update from sfml event loop


I am using sfml library for my graphics. During paused I want to avoid drawing on the screen. I want to intercept a spacebar press using sf::Event::MouseButtonPressed. In the debug mode, the event is successfully intercepted but failed in normal execution, why? The bool variable 'paused' don't seemed to update.

while (window.isOpen()) {
    Event event;
    while (window.pollEvent(event)) {
                /*..*/              
                if (event.type = Event::KeyPressed) {
                    if (event.key.code == Keyboard::Space) {
                        if (!paused) {
                            paused = true;// UDATE HERE
                        }
                        else
                            if (paused)
                            {
                                paused = false; // UDATE HERE
                            }
                    }
                }
    }    
    if (!paused)  //// UDATE DONT SEEM TO AFFECT HERE
    {
        /* draw here */
    }    
}

Edited: Even tried by declaring the variable paused as atomic, but still don't work.


Solution

  • Use the Event::KeyReleased instead of the Event::KeyPressed as the key press gets called every loop that a key is down leading to many enters while the Release event is only called once per click.