Game Development Reference
Events can be polled from the window by bool Win-
dow::pollEvent(sf::Event& event) . If there is an event waiting to be handled,
the function will return true , and the event variable will be filled with the event data. If
not, the function returns false . It is also important to note that there might be more than
one event at a time; so we have to be sure to capture every event possible. Here is what a
typical event loop looks like:
Running the code now produces more satisfactory results—we can move the window
around, resize, and minimize it. However, there is still one problem—the close button
doesn't work. SFML doesn't assume that the window should close after the user clicks on
the close button. Maybe we want to save the player's progress or ask them if they are sure
first. This means that we have to implement the close button functionality ourselves.
Before we proceed, it is important to note that the Event class in C++ contains a union.
This means that only one of its members is valid. Accessing any other member will result
in undefined behavior. We can get the valid member by looking at the type of the event.
The event types can be logically split into four sections, depending on what they are related