diff --git a/src/SFML/Window/WindowBase.cpp b/src/SFML/Window/WindowBase.cpp index d2bc2da2f..deb7d37c4 100644 --- a/src/SFML/Window/WindowBase.cpp +++ b/src/SFML/Window/WindowBase.cpp @@ -159,30 +159,34 @@ bool WindowBase::isOpen() const //////////////////////////////////////////////////////////// bool WindowBase::pollEvent(Event& event) { - if (m_impl && m_impl->popEvent(event, false)) + if (!m_impl) + return false; + + if (const auto maybeEvent = m_impl->popEvent(false)) { - filterEvent(event); + filterEvent(*maybeEvent); + event = *maybeEvent; return true; } - else - { - return false; - } + + return false; } //////////////////////////////////////////////////////////// bool WindowBase::waitEvent(Event& event) { - if (m_impl && m_impl->popEvent(event, true)) + if (!m_impl) + return false; + + if (const auto maybeEvent = m_impl->popEvent(true)) { - filterEvent(event); + filterEvent(*maybeEvent); + event = *maybeEvent; return true; } - else - { - return false; - } + + return false; } diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index d75d87751..a588b7268 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -171,7 +171,7 @@ void WindowImpl::setMaximumSize(const std::optional& maximumSize) //////////////////////////////////////////////////////////// -bool WindowImpl::popEvent(Event& event, bool block) +std::optional WindowImpl::popEvent(bool block) { // If the event queue is empty, let's first check if new events are available from the OS if (m_events.empty()) @@ -200,13 +200,13 @@ bool WindowImpl::popEvent(Event& event, bool block) // Pop the first event of the queue, if it is not empty if (!m_events.empty()) { - event = m_events.front(); + const auto event = m_events.front(); m_events.pop(); - return true; + return event; } - return false; + return std::nullopt; } diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 1cf6ddf11..ff08577ea 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -125,7 +125,7 @@ public: /// \param block Use true to block the thread until an event arrives /// //////////////////////////////////////////////////////////// - bool popEvent(Event& event, bool block); + std::optional popEvent(bool block); //////////////////////////////////////////////////////////// /// \brief Get the OS-specific handle of the window