Simplify event conversion

This commit is contained in:
Chris Thrasher committed 2024-09-02 15:39:15 -06:00
1 parent 3c0931b744
commit afc07ec565
4 files changed
+37 -96

No files matched your search

+31 -24
View File
@@ -31,36 +31,41 @@
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include <optional>
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Define a function to convert a sf::Event to a sfEvent // Convert sf::Event to sfEvent
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event) [[nodiscard]] inline bool convertEvent(const std::optional<sf::Event>& sfmlEvent, sfEvent* event)
{ {
if (sfmlEvent.is<sf::Event::Closed>()) if (!sfmlEvent)
return false;
if (sfmlEvent->is<sf::Event::Closed>())
{ {
event->type = sfEvtClosed; event->type = sfEvtClosed;
} }
else if (const auto* resized = sfmlEvent.getIf<sf::Event::Resized>()) else if (const auto* resized = sfmlEvent->getIf<sf::Event::Resized>())
{ {
event->type = sfEvtResized; event->type = sfEvtResized;
event->size.width = resized->size.x; event->size.width = resized->size.x;
event->size.height = resized->size.y; event->size.height = resized->size.y;
} }
else if (sfmlEvent.is<sf::Event::FocusLost>()) else if (sfmlEvent->is<sf::Event::FocusLost>())
{ {
event->type = sfEvtFocusLost; event->type = sfEvtFocusLost;
} }
else if (sfmlEvent.is<sf::Event::FocusGained>()) else if (sfmlEvent->is<sf::Event::FocusGained>())
{ {
event->type = sfEvtFocusGained; event->type = sfEvtFocusGained;
} }
else if (const auto* textEntered = sfmlEvent.getIf<sf::Event::TextEntered>()) else if (const auto* textEntered = sfmlEvent->getIf<sf::Event::TextEntered>())
{ {
event->type = sfEvtTextEntered; event->type = sfEvtTextEntered;
event->text.unicode = textEntered->unicode; event->text.unicode = textEntered->unicode;
} }
else if (const auto* keyReleased = sfmlEvent.getIf<sf::Event::KeyReleased>()) else if (const auto* keyReleased = sfmlEvent->getIf<sf::Event::KeyReleased>())
{ {
event->type = sfEvtKeyReleased; event->type = sfEvtKeyReleased;
event->key.code = static_cast<sfKeyCode>(keyReleased->code); event->key.code = static_cast<sfKeyCode>(keyReleased->code);
@@ -70,7 +75,7 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->key.shift = keyReleased->shift; event->key.shift = keyReleased->shift;
event->key.system = keyReleased->system; event->key.system = keyReleased->system;
} }
else if (const auto* keyPressed = sfmlEvent.getIf<sf::Event::KeyPressed>()) else if (const auto* keyPressed = sfmlEvent->getIf<sf::Event::KeyPressed>())
{ {
event->type = sfEvtKeyPressed; event->type = sfEvtKeyPressed;
event->key.code = static_cast<sfKeyCode>(keyPressed->code); event->key.code = static_cast<sfKeyCode>(keyPressed->code);
@@ -80,7 +85,7 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->key.shift = keyPressed->shift; event->key.shift = keyPressed->shift;
event->key.system = keyPressed->system; event->key.system = keyPressed->system;
} }
else if (const auto* mouseWheelScrolled = sfmlEvent.getIf<sf::Event::MouseWheelScrolled>()) else if (const auto* mouseWheelScrolled = sfmlEvent->getIf<sf::Event::MouseWheelScrolled>())
{ {
event->type = sfEvtMouseWheelScrolled; event->type = sfEvtMouseWheelScrolled;
event->mouseWheelScroll.wheel = static_cast<sfMouseWheel>(mouseWheelScrolled->wheel); event->mouseWheelScroll.wheel = static_cast<sfMouseWheel>(mouseWheelScrolled->wheel);
@@ -88,85 +93,85 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->mouseWheelScroll.x = mouseWheelScrolled->position.x; event->mouseWheelScroll.x = mouseWheelScrolled->position.x;
event->mouseWheelScroll.y = mouseWheelScrolled->position.y; event->mouseWheelScroll.y = mouseWheelScrolled->position.y;
} }
else if (const auto* mouseButtonPressed = sfmlEvent.getIf<sf::Event::MouseButtonPressed>()) else if (const auto* mouseButtonPressed = sfmlEvent->getIf<sf::Event::MouseButtonPressed>())
{ {
event->type = sfEvtMouseButtonPressed; event->type = sfEvtMouseButtonPressed;
event->mouseButton.button = static_cast<sfMouseButton>(mouseButtonPressed->button); event->mouseButton.button = static_cast<sfMouseButton>(mouseButtonPressed->button);
event->mouseButton.x = mouseButtonPressed->position.x; event->mouseButton.x = mouseButtonPressed->position.x;
event->mouseButton.y = mouseButtonPressed->position.y; event->mouseButton.y = mouseButtonPressed->position.y;
} }
else if (const auto* mouseButtonReleased = sfmlEvent.getIf<sf::Event::MouseButtonReleased>()) else if (const auto* mouseButtonReleased = sfmlEvent->getIf<sf::Event::MouseButtonReleased>())
{ {
event->type = sfEvtMouseButtonReleased; event->type = sfEvtMouseButtonReleased;
event->mouseButton.button = static_cast<sfMouseButton>(mouseButtonReleased->button); event->mouseButton.button = static_cast<sfMouseButton>(mouseButtonReleased->button);
event->mouseButton.x = mouseButtonReleased->position.x; event->mouseButton.x = mouseButtonReleased->position.x;
event->mouseButton.y = mouseButtonReleased->position.y; event->mouseButton.y = mouseButtonReleased->position.y;
} }
else if (const auto* mouseMoved = sfmlEvent.getIf<sf::Event::MouseMoved>()) else if (const auto* mouseMoved = sfmlEvent->getIf<sf::Event::MouseMoved>())
{ {
event->type = sfEvtMouseMoved; event->type = sfEvtMouseMoved;
event->mouseMove.x = mouseMoved->position.x; event->mouseMove.x = mouseMoved->position.x;
event->mouseMove.y = mouseMoved->position.y; event->mouseMove.y = mouseMoved->position.y;
} }
else if (sfmlEvent.is<sf::Event::MouseEntered>()) else if (sfmlEvent->is<sf::Event::MouseEntered>())
{ {
event->type = sfEvtMouseEntered; event->type = sfEvtMouseEntered;
} }
else if (sfmlEvent.is<sf::Event::MouseLeft>()) else if (sfmlEvent->is<sf::Event::MouseLeft>())
{ {
event->type = sfEvtMouseLeft; event->type = sfEvtMouseLeft;
} }
else if (const auto* joystickButtonPressed = sfmlEvent.getIf<sf::Event::JoystickButtonPressed>()) else if (const auto* joystickButtonPressed = sfmlEvent->getIf<sf::Event::JoystickButtonPressed>())
{ {
event->type = sfEvtJoystickButtonPressed; event->type = sfEvtJoystickButtonPressed;
event->joystickButton.joystickId = joystickButtonPressed->joystickId; event->joystickButton.joystickId = joystickButtonPressed->joystickId;
event->joystickButton.button = joystickButtonPressed->button; event->joystickButton.button = joystickButtonPressed->button;
} }
else if (const auto* joystickButtonReleased = sfmlEvent.getIf<sf::Event::JoystickButtonReleased>()) else if (const auto* joystickButtonReleased = sfmlEvent->getIf<sf::Event::JoystickButtonReleased>())
{ {
event->type = sfEvtJoystickButtonReleased; event->type = sfEvtJoystickButtonReleased;
event->joystickButton.joystickId = joystickButtonReleased->joystickId; event->joystickButton.joystickId = joystickButtonReleased->joystickId;
event->joystickButton.button = joystickButtonReleased->button; event->joystickButton.button = joystickButtonReleased->button;
} }
else if (const auto* joystickMoved = sfmlEvent.getIf<sf::Event::JoystickMoved>()) else if (const auto* joystickMoved = sfmlEvent->getIf<sf::Event::JoystickMoved>())
{ {
event->type = sfEvtJoystickMoved; event->type = sfEvtJoystickMoved;
event->joystickMove.joystickId = joystickMoved->joystickId; event->joystickMove.joystickId = joystickMoved->joystickId;
event->joystickMove.axis = static_cast<sfJoystickAxis>(joystickMoved->axis); event->joystickMove.axis = static_cast<sfJoystickAxis>(joystickMoved->axis);
event->joystickMove.position = joystickMoved->position; event->joystickMove.position = joystickMoved->position;
} }
else if (const auto* joystickConnected = sfmlEvent.getIf<sf::Event::JoystickConnected>()) else if (const auto* joystickConnected = sfmlEvent->getIf<sf::Event::JoystickConnected>())
{ {
event->type = sfEvtJoystickConnected; event->type = sfEvtJoystickConnected;
event->joystickConnect.joystickId = joystickConnected->joystickId; event->joystickConnect.joystickId = joystickConnected->joystickId;
} }
else if (const auto* joystickDisconnected = sfmlEvent.getIf<sf::Event::JoystickDisconnected>()) else if (const auto* joystickDisconnected = sfmlEvent->getIf<sf::Event::JoystickDisconnected>())
{ {
event->type = sfEvtJoystickDisconnected; event->type = sfEvtJoystickDisconnected;
event->joystickConnect.joystickId = joystickDisconnected->joystickId; event->joystickConnect.joystickId = joystickDisconnected->joystickId;
} }
else if (const auto* touchBegan = sfmlEvent.getIf<sf::Event::TouchBegan>()) else if (const auto* touchBegan = sfmlEvent->getIf<sf::Event::TouchBegan>())
{ {
event->type = sfEvtTouchBegan; event->type = sfEvtTouchBegan;
event->touch.finger = touchBegan->finger; event->touch.finger = touchBegan->finger;
event->touch.x = touchBegan->position.x; event->touch.x = touchBegan->position.x;
event->touch.y = touchBegan->position.y; event->touch.y = touchBegan->position.y;
} }
else if (const auto* touchMoved = sfmlEvent.getIf<sf::Event::TouchMoved>()) else if (const auto* touchMoved = sfmlEvent->getIf<sf::Event::TouchMoved>())
{ {
event->type = sfEvtTouchMoved; event->type = sfEvtTouchMoved;
event->touch.finger = touchMoved->finger; event->touch.finger = touchMoved->finger;
event->touch.x = touchMoved->position.x; event->touch.x = touchMoved->position.x;
event->touch.y = touchMoved->position.y; event->touch.y = touchMoved->position.y;
} }
else if (const auto* touchEnded = sfmlEvent.getIf<sf::Event::TouchEnded>()) else if (const auto* touchEnded = sfmlEvent->getIf<sf::Event::TouchEnded>())
{ {
event->type = sfEvtTouchEnded; event->type = sfEvtTouchEnded;
event->touch.finger = touchEnded->finger; event->touch.finger = touchEnded->finger;
event->touch.x = touchEnded->position.x; event->touch.x = touchEnded->position.x;
event->touch.y = touchEnded->position.y; event->touch.y = touchEnded->position.y;
} }
else if (const auto* sensorChanged = sfmlEvent.getIf<sf::Event::SensorChanged>()) else if (const auto* sensorChanged = sfmlEvent->getIf<sf::Event::SensorChanged>())
{ {
event->type = sfEvtSensorChanged; event->type = sfEvtSensorChanged;
event->sensor.sensorType = static_cast<sfSensorType>(sensorChanged->type); event->sensor.sensorType = static_cast<sfSensorType>(sensorChanged->type);
@@ -174,4 +179,6 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->sensor.y = sensorChanged->value.y; event->sensor.y = sensorChanged->value.y;
event->sensor.z = sensorChanged->value.z; event->sensor.z = sensorChanged->value.z;
} }
return true;
} }
+2 -24
View File
@@ -144,18 +144,7 @@ bool sfRenderWindow_pollEvent(sfRenderWindow* renderWindow, sfEvent* event)
{ {
assert(renderWindow); assert(renderWindow);
assert(event); assert(event);
return convertEvent(renderWindow->This.pollEvent(), event);
// Get the event
const std::optional sfmlEvent = renderWindow->This.pollEvent();
// No event, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }
@@ -164,18 +153,7 @@ bool sfRenderWindow_waitEvent(sfRenderWindow* renderWindow, sfEvent* event)
{ {
assert(renderWindow); assert(renderWindow);
assert(event); assert(event);
return convertEvent(renderWindow->This.waitEvent(), event);
// Get the event
const std::optional sfmlEvent = renderWindow->This.waitEvent();
// Error, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }
+2 -24
View File
@@ -118,18 +118,7 @@ bool sfWindow_pollEvent(sfWindow* window, sfEvent* event)
{ {
assert(window); assert(window);
assert(event); assert(event);
return convertEvent(window->This.pollEvent(), event);
// Get the event
const std::optional sfmlEvent = window->This.pollEvent();
// No event, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }
@@ -138,18 +127,7 @@ bool sfWindow_waitEvent(sfWindow* window, sfEvent* event)
{ {
assert(window); assert(window);
assert(event); assert(event);
return convertEvent(window->This.waitEvent(), event);
// Get the event
const std::optional sfmlEvent = window->This.waitEvent();
// Error, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }
+2 -24
View File
@@ -96,18 +96,7 @@ bool sfWindowBase_pollEvent(sfWindowBase* windowBase, sfEvent* event)
{ {
assert(windowBase); assert(windowBase);
assert(event); assert(event);
return convertEvent(windowBase->This.pollEvent(), event);
// Get the event
const std::optional sfmlEvent = windowBase->This.pollEvent();
// No event, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }
@@ -116,18 +105,7 @@ bool sfWindowBase_waitEvent(sfWindowBase* windowBase, sfEvent* event)
{ {
assert(windowBase); assert(windowBase);
assert(event); assert(event);
return convertEvent(windowBase->This.waitEvent(), event);
// Get the event
const std::optional sfmlEvent = windowBase->This.waitEvent();
// Error, return
if (!sfmlEvent)
return false;
// Convert the sf::Event event to a sfEvent
convertEvent(*sfmlEvent, event);
return true;
} }