ARJAV GARG
ARJAV GARG

Reputation: 19

SFML: waitEvent being called before pollEvent loop

This is my code in the recent SFML 2.4.2

#include <SFML/Graphics.hpp>
#include <iostream>

int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "My First SFML Game", sf::Style::Titlebar | sf::Style::Close);

std::cout << "Press a key to continue" << std::endl;
int i = 0;
sf::Event event;

while(window.isOpen()) {

    while(window.pollEvent(event)) {
        std::cout << "Polling event" << std::endl;
        if(event.type == sf::Event::Closed) {

            window.close();
        }
    }

    if(window.waitEvent(event)) {
        if(event.type == sf::Event::Closed)std::cout << "Event Activated" << i++ << std::endl;
    }
}
}

Output All the event Activated output you see was printed when I clicked the close button and all the poll event was printed when I moved in or out of the window. It did not print Polled Event even when I clicked in the window! My problem is, pollEvents, why you no get called first and close the window when you get the close event first (apparently) because you have been written first? It does not make sense why the wait events method would be called first and pop the que. Please help.

Upvotes: 0

Views: 950

Answers (1)

Rodolphe Chartier
Rodolphe Chartier

Reputation: 66

If there's no event, pollEvent will not return true and then it will not enter in your loop. But waitEvent stops the program until an event is received.

Read the documentation of SFML Events:

sf::Window::waitEvent - This function is blocking: if there's no pending event then it will wait until an event is received.
sf::Window::pollEvent - This function is not blocking: if there's no pending event then it will return false and leave event unmodified.

Upvotes: 1

Related Questions