Skip to content

Commit

Permalink
Simplify event conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Sep 2, 2024
1 parent 3c0931b commit afc07ec
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 96 deletions.
55 changes: 31 additions & 24 deletions src/CSFML/ConvertEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,41 @@

#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;
}
else if (const auto* resized = sfmlEvent.getIf<sf::Event::Resized>())
else if (const auto* resized = sfmlEvent->getIf<sf::Event::Resized>())
{
event->type = sfEvtResized;
event->size.width = resized->size.x;
event->size.height = resized->size.y;
}
else if (sfmlEvent.is<sf::Event::FocusLost>())
else if (sfmlEvent->is<sf::Event::FocusLost>())
{
event->type = sfEvtFocusLost;
}
else if (sfmlEvent.is<sf::Event::FocusGained>())
else if (sfmlEvent->is<sf::Event::FocusGained>())
{
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->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->key.code = static_cast<sfKeyCode>(keyReleased->code);
Expand All @@ -70,7 +75,7 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->key.shift = keyReleased->shift;
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->key.code = static_cast<sfKeyCode>(keyPressed->code);
Expand All @@ -80,98 +85,100 @@ inline void convertEvent(const sf::Event& sfmlEvent, sfEvent* event)
event->key.shift = keyPressed->shift;
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->mouseWheelScroll.wheel = static_cast<sfMouseWheel>(mouseWheelScrolled->wheel);
event->mouseWheelScroll.delta = mouseWheelScrolled->delta;
event->mouseWheelScroll.x = mouseWheelScrolled->position.x;
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->mouseButton.button = static_cast<sfMouseButton>(mouseButtonPressed->button);
event->mouseButton.x = mouseButtonPressed->position.x;
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->mouseButton.button = static_cast<sfMouseButton>(mouseButtonReleased->button);
event->mouseButton.x = mouseButtonReleased->position.x;
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->mouseMove.x = mouseMoved->position.x;
event->mouseMove.y = mouseMoved->position.y;
}
else if (sfmlEvent.is<sf::Event::MouseEntered>())
else if (sfmlEvent->is<sf::Event::MouseEntered>())
{
event->type = sfEvtMouseEntered;
}
else if (sfmlEvent.is<sf::Event::MouseLeft>())
else if (sfmlEvent->is<sf::Event::MouseLeft>())
{
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->joystickButton.joystickId = joystickButtonPressed->joystickId;
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->joystickButton.joystickId = joystickButtonReleased->joystickId;
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->joystickMove.joystickId = joystickMoved->joystickId;
event->joystickMove.axis = static_cast<sfJoystickAxis>(joystickMoved->axis);
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->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->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->touch.finger = touchBegan->finger;
event->touch.x = touchBegan->position.x;
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->touch.finger = touchMoved->finger;
event->touch.x = touchMoved->position.x;
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->touch.finger = touchEnded->finger;
event->touch.x = touchEnded->position.x;
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->sensor.sensorType = static_cast<sfSensorType>(sensorChanged->type);
event->sensor.x = sensorChanged->value.x;
event->sensor.y = sensorChanged->value.y;
event->sensor.z = sensorChanged->value.z;
}

return true;
}
26 changes: 2 additions & 24 deletions src/CSFML/Graphics/RenderWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,7 @@ bool sfRenderWindow_pollEvent(sfRenderWindow* renderWindow, sfEvent* event)
{
assert(renderWindow);
assert(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;
return convertEvent(renderWindow->This.pollEvent(), event);
}


Expand All @@ -164,18 +153,7 @@ bool sfRenderWindow_waitEvent(sfRenderWindow* renderWindow, sfEvent* event)
{
assert(renderWindow);
assert(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;
return convertEvent(renderWindow->This.waitEvent(), event);
}


Expand Down
26 changes: 2 additions & 24 deletions src/CSFML/Window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,7 @@ bool sfWindow_pollEvent(sfWindow* window, sfEvent* event)
{
assert(window);
assert(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;
return convertEvent(window->This.pollEvent(), event);
}


Expand All @@ -138,18 +127,7 @@ bool sfWindow_waitEvent(sfWindow* window, sfEvent* event)
{
assert(window);
assert(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;
return convertEvent(window->This.waitEvent(), event);
}


Expand Down
26 changes: 2 additions & 24 deletions src/CSFML/Window/WindowBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,7 @@ bool sfWindowBase_pollEvent(sfWindowBase* windowBase, sfEvent* event)
{
assert(windowBase);
assert(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;
return convertEvent(windowBase->This.pollEvent(), event);
}


Expand All @@ -116,18 +105,7 @@ bool sfWindowBase_waitEvent(sfWindowBase* windowBase, sfEvent* event)
{
assert(windowBase);
assert(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;
return convertEvent(windowBase->This.waitEvent(), event);
}


Expand Down

0 comments on commit afc07ec

Please sign in to comment.