Add operator bool() to sf::Event for checking if the event type is not Empty

This commit is contained in:
trustytrojan 2024-05-01 21:43:24 -04:00 committed by Chris Thrasher
parent 9534c000f6
commit ca0a231b35
26 changed files with 89 additions and 71 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -113,7 +113,7 @@ 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 event = active ? window.pollEvent() : window.waitEvent())
{ {
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())
{ {

View File

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

View File

@ -158,7 +158,7 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto event = window.pollEvent())
{ {
// Window closed or escape key pressed: exit // Window closed or escape key pressed: exit
if (event.is<sf::Event::Closed>() || (event.is<sf::Event::KeyPressed>() && if (event.is<sf::Event::Closed>() || (event.is<sf::Event::KeyPressed>() &&

View File

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

View File

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

View File

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

View File

@ -40,7 +40,7 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto event = window.pollEvent())
{ {
// Window closed: exit // Window closed: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())

View File

@ -115,7 +115,7 @@ int main()
while (window.isOpen()) while (window.isOpen())
{ {
// Handle events // Handle events
for (sf::Event event; window.pollEvent(event);) while (const auto event = window.pollEvent())
{ {
// Window closed or escape key pressed: exit // Window closed or escape key pressed: exit
if (event.is<sf::Event::Closed>() || (event.is<sf::Event::KeyPressed>() && if (event.is<sf::Event::Closed>() || (event.is<sf::Event::KeyPressed>() &&

View File

@ -2542,7 +2542,7 @@ public:
while (window.isOpen()) while (window.isOpen())
{ {
// Process events // Process events
for (sf::Event event; window.pollEvent(event);) while (const auto event = window.pollEvent())
{ {
// Close window: exit // Close window: exit
if (event.is<sf::Event::Closed>()) if (event.is<sf::Event::Closed>())

View File

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

View File

@ -237,7 +237,7 @@ private:
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto event = window.pollEvent())
/// { /// {
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())

View File

@ -91,7 +91,7 @@ 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 event = window.pollEvent())
/// { /// {
/// if(event.is<sf::Event::Closed>()) /// if(event.is<sf::Event::Closed>())
/// window.close(); /// window.close();

View File

@ -276,6 +276,17 @@ public:
template <typename T> template <typename T>
[[nodiscard]] const T* getIf() const; [[nodiscard]] const T* getIf() const;
////////////////////////////////////////////////////////////
/// \brief Check if current event type is not `Empty`
///
/// \return True if current event type is not `Empty`
///
////////////////////////////////////////////////////////////
[[nodiscard]] explicit operator bool() const
{
return !is<Empty>();
}
private: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
@ -345,7 +356,7 @@ private:
/// any of the corresponding event data. /// any of the corresponding event data.
/// ///
/// \code /// \code
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto event = window.pollEvent())
/// { /// {
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())

View File

@ -347,7 +347,7 @@ private:
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto event = window.pollEvent())
/// { /// {
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())

View File

@ -180,52 +180,46 @@ public:
/// \brief Pop the next event from the front of the FIFO event queue, if any, and return it /// \brief Pop the next event from the front of the FIFO event queue, if any, and return it
/// ///
/// This function is not blocking: if there's no pending event then /// This function is not blocking: if there's no pending event then
/// it will return false and leave \a event unmodified. /// it will return an empty event. Note that more than one event
/// Note that more than one event may be present in the event queue, /// may be present in the event queue, thus you should always call
/// thus you should always call this function in a loop /// this function in a loop to make sure that you process every
/// to make sure that you process every pending event. /// pending event.
/// \code /// \code
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto event = window.pollEvent())
/// { /// {
/// // process event... /// // process event...
/// } /// }
/// \endcode /// \endcode
/// ///
/// \param event Event to be returned /// \return The event; will be `Empty` (convertible to `false`) if no events are pending
///
/// \return True if an event was returned, or false if the event queue was empty
/// ///
/// \see waitEvent /// \see waitEvent
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] bool pollEvent(Event& event); [[nodiscard]] Event pollEvent();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Wait for an event and return it /// \brief Wait for an event and return it
/// ///
/// This function is blocking: if there's no pending event then /// This function is blocking: if there's no pending event then
/// it will wait until an event is received. /// it will wait until an event is received. After this function
/// After this function returns (and no error occurred), /// returns if no error occurred, the returned event will not be
/// the \a event object is always valid and filled properly. /// empty. This function is typically used when you have a thread
/// This function is typically used when you have a thread that /// that is dedicated to events handling: you want to make this
/// is dedicated to events handling: you want to make this thread /// 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 (const auto event = window.waitEvent())
/// if (window.waitEvent(event))
/// { /// {
/// // process event... /// // process event...
/// } /// }
/// \endcode /// \endcode
/// ///
/// \param event Event to be returned /// \return The event
///
/// \return False if any error occurred
/// ///
/// \see pollEvent /// \see pollEvent
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] bool waitEvent(Event& event); [[nodiscard]] Event waitEvent();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the position of the window /// \brief Get the position of the window
@ -563,7 +557,7 @@ private:
/// while (window.isOpen()) /// while (window.isOpen())
/// { /// {
/// // Event processing /// // Event processing
/// for (sf::Event event; window.pollEvent(event);) /// while (const auto event = window.pollEvent())
/// { /// {
/// // Request for closing the window /// // Request for closing the window
/// if (event.is<sf::Event::Closed>()) /// if (event.is<sf::Event::Closed>())

View File

@ -136,32 +136,22 @@ bool WindowBase::isOpen() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool WindowBase::pollEvent(Event& event) Event WindowBase::pollEvent()
{ {
if (m_impl && m_impl->popEvent(event, false)) sf::Event event;
{ if (m_impl && (event = m_impl->popEvent(false)))
filterEvent(event); filterEvent(event);
return true; return event;
}
else
{
return false;
}
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool WindowBase::waitEvent(Event& event) Event WindowBase::waitEvent()
{ {
if (m_impl && m_impl->popEvent(event, true)) sf::Event event;
{ if (m_impl && (event = m_impl->popEvent(true)))
filterEvent(event); filterEvent(event);
return true; return event;
}
else
{
return false;
}
} }

View File

@ -176,7 +176,7 @@ void WindowImpl::setMaximumSize(const std::optional<Vector2u>& maximumSize)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool WindowImpl::popEvent(Event& event, bool block) Event WindowImpl::popEvent(bool block)
{ {
// If the event queue is empty, let's first check if new events are available from the OS // If the event queue is empty, let's first check if new events are available from the OS
if (m_events.empty()) if (m_events.empty())
@ -202,16 +202,16 @@ bool WindowImpl::popEvent(Event& event, bool block)
} }
} }
sf::Event event;
// Pop the first event of the queue, if it is not empty // Pop the first event of the queue, if it is not empty
if (!m_events.empty()) if (!m_events.empty())
{ {
event = m_events.front(); event = m_events.front();
m_events.pop(); m_events.pop();
return true;
} }
return false; return event;
} }

View File

@ -128,13 +128,14 @@ public:
/// The \a block parameter controls the behavior of the function /// The \a block parameter controls the behavior of the function
/// if no event is available: if it is true then the function /// if no event is available: if it is true then the function
/// doesn't return until a new event is triggered; otherwise it /// doesn't return until a new event is triggered; otherwise it
/// returns false to indicate that no event is available. /// returns an empty event to indicate that no event is available.
/// ///
/// \param event Event to be returned
/// \param block Use true to block the thread until an event arrives /// \param block Use true to block the thread until an event arrives
/// ///
/// \return The event; can be `Empty` (convertible to `false`) if not blocking
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool popEvent(Event& event, bool block); Event popEvent(bool block);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the OS-specific handle of the window /// \brief Get the OS-specific handle of the window

View File

@ -19,6 +19,7 @@ TEST_CASE("[Window] sf::Event")
SECTION("Default constructor") SECTION("Default constructor")
{ {
const sf::Event event; const sf::Event event;
CHECK(!event);
CHECK(event.is<sf::Event::Empty>()); CHECK(event.is<sf::Event::Empty>());
CHECK(event.getIf<sf::Event::Empty>()); CHECK(event.getIf<sf::Event::Empty>());
} }
@ -26,6 +27,7 @@ TEST_CASE("[Window] sf::Event")
SECTION("Template constructor") SECTION("Template constructor")
{ {
const sf::Event event = sf::Event::Resized{{1, 2}}; const sf::Event event = sf::Event::Resized{{1, 2}};
CHECK(event);
CHECK(event.is<sf::Event::Resized>()); CHECK(event.is<sf::Event::Resized>());
CHECK(event.getIf<sf::Event::Resized>()); CHECK(event.getIf<sf::Event::Resized>());
const auto& resized = *event.getIf<sf::Event::Resized>(); const auto& resized = *event.getIf<sf::Event::Resized>();
@ -37,30 +39,36 @@ TEST_CASE("[Window] sf::Event")
{ {
sf::Event event; sf::Event event;
event = sf::Event::Closed{}; event = sf::Event::Closed{};
CHECK(event);
CHECK(event.is<sf::Event::Closed>()); CHECK(event.is<sf::Event::Closed>());
CHECK(event.getIf<sf::Event::Closed>()); CHECK(event.getIf<sf::Event::Closed>());
event = sf::Event::Resized{{1, 2}}; event = sf::Event::Resized{{1, 2}};
CHECK(event);
CHECK(event.is<sf::Event::Resized>()); CHECK(event.is<sf::Event::Resized>());
CHECK(event.getIf<sf::Event::Resized>()); CHECK(event.getIf<sf::Event::Resized>());
const auto& resized = *event.getIf<sf::Event::Resized>(); const auto& resized = *event.getIf<sf::Event::Resized>();
CHECK(resized.size == sf::Vector2u(1, 2)); CHECK(resized.size == sf::Vector2u(1, 2));
event = sf::Event::FocusLost{}; event = sf::Event::FocusLost{};
CHECK(event);
CHECK(event.is<sf::Event::FocusLost>()); CHECK(event.is<sf::Event::FocusLost>());
CHECK(event.getIf<sf::Event::FocusLost>()); CHECK(event.getIf<sf::Event::FocusLost>());
event = sf::Event::FocusGained{}; event = sf::Event::FocusGained{};
CHECK(event);
CHECK(event.is<sf::Event::FocusGained>()); CHECK(event.is<sf::Event::FocusGained>());
CHECK(event.getIf<sf::Event::FocusGained>()); CHECK(event.getIf<sf::Event::FocusGained>());
event = sf::Event::TextEntered{123456}; event = sf::Event::TextEntered{123456};
CHECK(event);
CHECK(event.is<sf::Event::TextEntered>()); CHECK(event.is<sf::Event::TextEntered>());
CHECK(event.getIf<sf::Event::TextEntered>()); CHECK(event.getIf<sf::Event::TextEntered>());
const auto& textEntered = *event.getIf<sf::Event::TextEntered>(); const auto& textEntered = *event.getIf<sf::Event::TextEntered>();
CHECK(textEntered.unicode == 123456); CHECK(textEntered.unicode == 123456);
event = sf::Event::KeyPressed{sf::Keyboard::Key::C, sf::Keyboard::Scan::C, true, true, true, true}; event = sf::Event::KeyPressed{sf::Keyboard::Key::C, sf::Keyboard::Scan::C, true, true, true, true};
CHECK(event);
CHECK(event.is<sf::Event::KeyPressed>()); CHECK(event.is<sf::Event::KeyPressed>());
CHECK(event.getIf<sf::Event::KeyPressed>()); CHECK(event.getIf<sf::Event::KeyPressed>());
const auto& keyPressed = *event.getIf<sf::Event::KeyPressed>(); const auto& keyPressed = *event.getIf<sf::Event::KeyPressed>();
@ -72,6 +80,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(keyPressed.system); CHECK(keyPressed.system);
event = sf::Event::KeyReleased{sf::Keyboard::Key::D, sf::Keyboard::Scan::D, true, true, true, true}; event = sf::Event::KeyReleased{sf::Keyboard::Key::D, sf::Keyboard::Scan::D, true, true, true, true};
CHECK(event);
CHECK(event.is<sf::Event::KeyReleased>()); CHECK(event.is<sf::Event::KeyReleased>());
CHECK(event.getIf<sf::Event::KeyReleased>()); CHECK(event.getIf<sf::Event::KeyReleased>());
const auto& keyReleased = *event.getIf<sf::Event::KeyReleased>(); const auto& keyReleased = *event.getIf<sf::Event::KeyReleased>();
@ -83,6 +92,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(keyReleased.system); CHECK(keyReleased.system);
event = sf::Event::MouseWheelScrolled{sf::Mouse::Wheel::Horizontal, 3.14f, {4, 5}}; event = sf::Event::MouseWheelScrolled{sf::Mouse::Wheel::Horizontal, 3.14f, {4, 5}};
CHECK(event);
CHECK(event.is<sf::Event::MouseWheelScrolled>()); CHECK(event.is<sf::Event::MouseWheelScrolled>());
CHECK(event.getIf<sf::Event::MouseWheelScrolled>()); CHECK(event.getIf<sf::Event::MouseWheelScrolled>());
const auto& mouseWheelScrolled = *event.getIf<sf::Event::MouseWheelScrolled>(); const auto& mouseWheelScrolled = *event.getIf<sf::Event::MouseWheelScrolled>();
@ -91,6 +101,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(mouseWheelScrolled.position == sf::Vector2i(4, 5)); CHECK(mouseWheelScrolled.position == sf::Vector2i(4, 5));
event = sf::Event::MouseButtonPressed{sf::Mouse::Button::Middle, {6, 7}}; event = sf::Event::MouseButtonPressed{sf::Mouse::Button::Middle, {6, 7}};
CHECK(event);
CHECK(event.is<sf::Event::MouseButtonPressed>()); CHECK(event.is<sf::Event::MouseButtonPressed>());
CHECK(event.getIf<sf::Event::MouseButtonPressed>()); CHECK(event.getIf<sf::Event::MouseButtonPressed>());
const auto& mouseButtonPressed = *event.getIf<sf::Event::MouseButtonPressed>(); const auto& mouseButtonPressed = *event.getIf<sf::Event::MouseButtonPressed>();
@ -98,6 +109,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(mouseButtonPressed.position == sf::Vector2i(6, 7)); CHECK(mouseButtonPressed.position == sf::Vector2i(6, 7));
event = sf::Event::MouseButtonReleased{sf::Mouse::Button::Extra1, {8, 9}}; event = sf::Event::MouseButtonReleased{sf::Mouse::Button::Extra1, {8, 9}};
CHECK(event);
CHECK(event.is<sf::Event::MouseButtonReleased>()); CHECK(event.is<sf::Event::MouseButtonReleased>());
CHECK(event.getIf<sf::Event::MouseButtonReleased>()); CHECK(event.getIf<sf::Event::MouseButtonReleased>());
const auto& mouseButtonReleased = *event.getIf<sf::Event::MouseButtonReleased>(); const auto& mouseButtonReleased = *event.getIf<sf::Event::MouseButtonReleased>();
@ -105,20 +117,24 @@ TEST_CASE("[Window] sf::Event")
CHECK(mouseButtonReleased.position == sf::Vector2i(8, 9)); CHECK(mouseButtonReleased.position == sf::Vector2i(8, 9));
event = sf::Event::MouseMoved{{4, 2}}; event = sf::Event::MouseMoved{{4, 2}};
CHECK(event);
CHECK(event.is<sf::Event::MouseMoved>()); CHECK(event.is<sf::Event::MouseMoved>());
CHECK(event.getIf<sf::Event::MouseMoved>()); CHECK(event.getIf<sf::Event::MouseMoved>());
const auto& mouseMoved = *event.getIf<sf::Event::MouseMoved>(); const auto& mouseMoved = *event.getIf<sf::Event::MouseMoved>();
CHECK(mouseMoved.position == sf::Vector2i(4, 2)); CHECK(mouseMoved.position == sf::Vector2i(4, 2));
event = sf::Event::MouseEntered{}; event = sf::Event::MouseEntered{};
CHECK(event);
CHECK(event.is<sf::Event::MouseEntered>()); CHECK(event.is<sf::Event::MouseEntered>());
CHECK(event.getIf<sf::Event::MouseEntered>()); CHECK(event.getIf<sf::Event::MouseEntered>());
event = sf::Event::MouseLeft{}; event = sf::Event::MouseLeft{};
CHECK(event);
CHECK(event.is<sf::Event::MouseLeft>()); CHECK(event.is<sf::Event::MouseLeft>());
CHECK(event.getIf<sf::Event::MouseLeft>()); CHECK(event.getIf<sf::Event::MouseLeft>());
event = sf::Event::JoystickButtonPressed{100, 200}; event = sf::Event::JoystickButtonPressed{100, 200};
CHECK(event);
CHECK(event.is<sf::Event::JoystickButtonPressed>()); CHECK(event.is<sf::Event::JoystickButtonPressed>());
CHECK(event.getIf<sf::Event::JoystickButtonPressed>()); CHECK(event.getIf<sf::Event::JoystickButtonPressed>());
const auto& joystickButtonPressed = *event.getIf<sf::Event::JoystickButtonPressed>(); const auto& joystickButtonPressed = *event.getIf<sf::Event::JoystickButtonPressed>();
@ -126,6 +142,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(joystickButtonPressed.button == 200); CHECK(joystickButtonPressed.button == 200);
event = sf::Event::JoystickButtonReleased{300, 400}; event = sf::Event::JoystickButtonReleased{300, 400};
CHECK(event);
CHECK(event.is<sf::Event::JoystickButtonReleased>()); CHECK(event.is<sf::Event::JoystickButtonReleased>());
CHECK(event.getIf<sf::Event::JoystickButtonReleased>()); CHECK(event.getIf<sf::Event::JoystickButtonReleased>());
const auto& joystickButtonReleased = *event.getIf<sf::Event::JoystickButtonReleased>(); const auto& joystickButtonReleased = *event.getIf<sf::Event::JoystickButtonReleased>();
@ -133,6 +150,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(joystickButtonReleased.button == 400); CHECK(joystickButtonReleased.button == 400);
event = sf::Event::JoystickMoved{300, sf::Joystick::Axis::Z, 1.23f}; event = sf::Event::JoystickMoved{300, sf::Joystick::Axis::Z, 1.23f};
CHECK(event);
CHECK(event.is<sf::Event::JoystickMoved>()); CHECK(event.is<sf::Event::JoystickMoved>());
CHECK(event.getIf<sf::Event::JoystickMoved>()); CHECK(event.getIf<sf::Event::JoystickMoved>());
const auto& joystickMoved = *event.getIf<sf::Event::JoystickMoved>(); const auto& joystickMoved = *event.getIf<sf::Event::JoystickMoved>();
@ -141,18 +159,21 @@ TEST_CASE("[Window] sf::Event")
CHECK(joystickMoved.position == 1.23f); CHECK(joystickMoved.position == 1.23f);
event = sf::Event::JoystickConnected{42}; event = sf::Event::JoystickConnected{42};
CHECK(event);
CHECK(event.is<sf::Event::JoystickConnected>()); CHECK(event.is<sf::Event::JoystickConnected>());
CHECK(event.getIf<sf::Event::JoystickConnected>()); CHECK(event.getIf<sf::Event::JoystickConnected>());
const auto& joystickConnected = *event.getIf<sf::Event::JoystickConnected>(); const auto& joystickConnected = *event.getIf<sf::Event::JoystickConnected>();
CHECK(joystickConnected.joystickId == 42); CHECK(joystickConnected.joystickId == 42);
event = sf::Event::JoystickDisconnected{43}; event = sf::Event::JoystickDisconnected{43};
CHECK(event);
CHECK(event.is<sf::Event::JoystickDisconnected>()); CHECK(event.is<sf::Event::JoystickDisconnected>());
CHECK(event.getIf<sf::Event::JoystickDisconnected>()); CHECK(event.getIf<sf::Event::JoystickDisconnected>());
const auto& joystickDisconnected = *event.getIf<sf::Event::JoystickDisconnected>(); const auto& joystickDisconnected = *event.getIf<sf::Event::JoystickDisconnected>();
CHECK(joystickDisconnected.joystickId == 43); CHECK(joystickDisconnected.joystickId == 43);
event = sf::Event::TouchBegan{99, {98, 97}}; event = sf::Event::TouchBegan{99, {98, 97}};
CHECK(event);
CHECK(event.is<sf::Event::TouchBegan>()); CHECK(event.is<sf::Event::TouchBegan>());
CHECK(event.getIf<sf::Event::TouchBegan>()); CHECK(event.getIf<sf::Event::TouchBegan>());
const auto& touchBegan = *event.getIf<sf::Event::TouchBegan>(); const auto& touchBegan = *event.getIf<sf::Event::TouchBegan>();
@ -160,6 +181,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(touchBegan.position == sf::Vector2i(98, 97)); CHECK(touchBegan.position == sf::Vector2i(98, 97));
event = sf::Event::TouchMoved{96, {95, 94}}; event = sf::Event::TouchMoved{96, {95, 94}};
CHECK(event);
CHECK(event.is<sf::Event::TouchMoved>()); CHECK(event.is<sf::Event::TouchMoved>());
CHECK(event.getIf<sf::Event::TouchMoved>()); CHECK(event.getIf<sf::Event::TouchMoved>());
const auto& touchMoved = *event.getIf<sf::Event::TouchMoved>(); const auto& touchMoved = *event.getIf<sf::Event::TouchMoved>();
@ -167,6 +189,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(touchMoved.position == sf::Vector2i(95, 94)); CHECK(touchMoved.position == sf::Vector2i(95, 94));
event = sf::Event::TouchEnded{93, {92, 91}}; event = sf::Event::TouchEnded{93, {92, 91}};
CHECK(event);
CHECK(event.is<sf::Event::TouchEnded>()); CHECK(event.is<sf::Event::TouchEnded>());
CHECK(event.getIf<sf::Event::TouchEnded>()); CHECK(event.getIf<sf::Event::TouchEnded>());
const auto& touchEnded = *event.getIf<sf::Event::TouchEnded>(); const auto& touchEnded = *event.getIf<sf::Event::TouchEnded>();
@ -174,6 +197,7 @@ TEST_CASE("[Window] sf::Event")
CHECK(touchEnded.position == sf::Vector2i(92, 91)); CHECK(touchEnded.position == sf::Vector2i(92, 91));
event = sf::Event::SensorChanged{sf::Sensor::Type::Gravity, {1.2f, 3.4f, 5.6f}}; event = sf::Event::SensorChanged{sf::Sensor::Type::Gravity, {1.2f, 3.4f, 5.6f}};
CHECK(event);
CHECK(event.is<sf::Event::SensorChanged>()); CHECK(event.is<sf::Event::SensorChanged>());
CHECK(event.getIf<sf::Event::SensorChanged>()); CHECK(event.getIf<sf::Event::SensorChanged>());
const auto& sensorChanged = *event.getIf<sf::Event::SensorChanged>(); const auto& sensorChanged = *event.getIf<sf::Event::SensorChanged>();

View File

@ -105,15 +105,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

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

View File

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