Return std::optional<Event> from sf::WindowBase::pollEvent

This commit is contained in:
Chris Thrasher 2023-11-09 21:47:53 -07:00
parent 1c873112ec
commit 068ae6f575
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
21 changed files with 80 additions and 49 deletions

View File

@ -57,8 +57,10 @@ body:
while (window.isOpen()) while (window.isOpen())
{ {
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();
} }

View File

@ -54,8 +54,10 @@ body:
while (window.isOpen()) while (window.isOpen())
{ {
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();
} }

View File

@ -39,8 +39,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();
} }

View File

@ -44,8 +44,10 @@
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Process events /// // Process events
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// // Close window: exit /// // Close window: exit
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -113,8 +113,10 @@ int main(int argc, char* argv[])
while (window.isOpen()) while (window.isOpen())
{ {
for (sf::Event event; active ? window.pollEvent(event) : window.waitEvent(event);) while (const auto maybeEvent = active ? window.pollEvent() : window.waitEvent())
{ {
const auto& event = *maybeEvent;
switch (event.getType()) switch (event.getType())
{ {
case sf::Event::Type::Closed: case sf::Event::Type::Closed:

View File

@ -179,8 +179,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Window closed or escape key pressed: exit // Window closed or escape key pressed: exit
if (event.is<sf::Event::Closed>() || if (event.is<sf::Event::Closed>() ||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape)) (event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))

View File

@ -159,8 +159,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Window closed or escape key pressed: exit // Window closed or escape key pressed: exit
if (event.is<sf::Event::Closed>() || if (event.is<sf::Event::Closed>() ||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape)) (event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))

View File

@ -204,8 +204,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
{ {

View File

@ -390,8 +390,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();

View File

@ -111,8 +111,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Window closed or escape key pressed: exit // Window closed or escape key pressed: exit
if (event.is<sf::Event::Closed>() || if (event.is<sf::Event::Closed>() ||
(event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape)) (event.is<sf::Event::KeyPressed>() && event.get<sf::Event::KeyPressed>().code == sf::Keyboard::Escape))

View File

@ -2541,8 +2541,10 @@ public:
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();

View File

@ -141,8 +141,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
window.close(); window.close();

View File

@ -214,9 +214,11 @@ private:
/// // The main loop - ends as soon as the window is closed /// // The main loop - ends as soon as the window is closed
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -91,8 +91,10 @@ SFML_WINDOW_API void setString(const String& text);
/// sf::String string = sf::Clipboard::getString(); /// sf::String string = sf::Clipboard::getString();
/// ///
/// // or use it in the event loop /// // or use it in the event loop
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// if(event.is<sf::Event::Closed>()) /// if(event.is<sf::Event::Closed>())
/// window.close(); /// window.close();
/// if(event.is<sf::Event::KeyPressed>()) /// if(event.is<sf::Event::KeyPressed>())

View File

@ -441,8 +441,10 @@ const T* Event::getIf() const
/// statement to process events. /// statement to process events.
/// ///
/// \code /// \code
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// switch (event.getType()) /// switch (event.getType())
/// { /// {
/// // Request for closing the window /// // Request for closing the window
@ -475,8 +477,10 @@ const T* Event::getIf() const
/// process events in a series of if/else if blocks. /// process events in a series of if/else if blocks.
/// ///
/// \code /// \code
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -321,8 +321,10 @@ private:
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -167,7 +167,7 @@ public:
/// thus you should always call this function in a loop /// thus you should always call this function in a loop
/// to make sure that you process every pending event. /// to make sure that you process every pending event.
/// \code /// \code
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// // process event... /// // process event...
/// } /// }
@ -180,7 +180,7 @@ public:
/// \see waitEvent /// \see waitEvent
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] bool pollEvent(Event& event); [[nodiscard]] std::optional<Event> pollEvent();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Wait for an event and return it /// \brief Wait for an event and return it
@ -193,8 +193,7 @@ public:
/// is dedicated to events handling: you want to make this thread /// is dedicated to events handling: you want to make this thread
/// sleep as long as no new event is received. /// sleep as long as no new event is received.
/// \code /// \code
/// sf::Event event; /// if (window.waitEvent())
/// if (window.waitEvent(event))
/// { /// {
/// // process event... /// // process event...
/// } /// }
@ -207,7 +206,7 @@ public:
/// \see pollEvent /// \see pollEvent
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] bool waitEvent(Event& event); [[nodiscard]] std::optional<Event> waitEvent();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the position of the window /// \brief Get the position of the window
@ -532,8 +531,10 @@ private:
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto maybeEvent = window.pollEvent())
/// { /// {
/// const auto& event = *maybeEvent;
///
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -157,36 +157,30 @@ bool WindowBase::isOpen() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool WindowBase::pollEvent(Event& event) std::optional<Event> WindowBase::pollEvent()
{ {
if (!m_impl) if (!m_impl)
return false; return std::nullopt;
if (const auto maybeEvent = m_impl->popEvent(false)) const auto event = m_impl->popEvent(false);
{ if (event)
filterEvent(*maybeEvent); filterEvent(*event);
event = *maybeEvent;
return true;
}
return false; return event;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool WindowBase::waitEvent(Event& event) std::optional<Event> WindowBase::waitEvent()
{ {
if (!m_impl) if (!m_impl)
return false; return std::nullopt;
if (const auto maybeEvent = m_impl->popEvent(true)) const auto event = m_impl->popEvent(true);
{ if (event)
filterEvent(*maybeEvent); filterEvent(*event);
event = *maybeEvent;
return true;
}
return false; return event;
} }

View File

@ -81,15 +81,13 @@ TEST_CASE("[Window] sf::WindowBase", runDisplayTests())
SECTION("pollEvent()") SECTION("pollEvent()")
{ {
sf::WindowBase windowBase; sf::WindowBase windowBase;
sf::Event event; CHECK(!windowBase.pollEvent());
CHECK(!windowBase.pollEvent(event));
} }
SECTION("waitEvent()") SECTION("waitEvent()")
{ {
sf::WindowBase windowBase; sf::WindowBase windowBase;
sf::Event event; CHECK(!windowBase.waitEvent());
CHECK(!windowBase.waitEvent(event));
} }
SECTION("Set/get position") SECTION("Set/get position")

View File

@ -65,8 +65,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
{ {

View File

@ -63,8 +63,10 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto maybeEvent = window.pollEvent())
{ {
const auto& event = *maybeEvent;
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
{ {