Skip to content

Commit

Permalink
update SDL3 codepaths to work with 3.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Oct 6, 2024
1 parent e1494d4 commit dda7603
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/common/macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void setWindowSRGBColorSpace(SDL_Window *window)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_PropertiesID props = SDL_GetWindowProperties(window);
NSWindow *window = (__bridge NSWindow *) SDL_GetProperty(props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
NSWindow *window = (__bridge NSWindow *) SDL_GetPointerProperty(props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
window.colorSpace = [NSColorSpace sRGBColorSpace];
#else
SDL_SysWMinfo info = {};
Expand Down
37 changes: 35 additions & 2 deletions src/modules/event/sdl/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ static void normalizedToDPICoords(double *x, double *y)
// SDL's event watch callbacks trigger when the event is actually posted inside
// SDL, unlike with SDL_PollEvents. This is useful for some events which require
// handling inside the function which triggered them on some backends.
static int SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
#if SDL_VERSION_ATLEAST(3, 0, 0)
static bool
#else
static int
#endif
SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
{
auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);

Expand All @@ -159,15 +164,23 @@ static int SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event)
Event::Event()
: love::event::Event("love.event.sdl")
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_EVENTS))
#else
if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
#endif
throw love::Exception("Could not initialize SDL events subsystem (%s)", SDL_GetError());

SDL_AddEventWatch(watchAppEvents, this);
}

Event::~Event()
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_RemoveEventWatch(watchAppEvents, this);
#else
SDL_DelEventWatch(watchAppEvents, this);
#endif
SDL_QuitSubSystem(SDL_INIT_EVENTS);
}

Expand All @@ -194,7 +207,11 @@ Message *Event::wait()

SDL_Event e;

#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WaitEvent(&e))
#else
if (SDL_WaitEvent(&e) != 1)
#endif
return nullptr;

return convert(e);
Expand Down Expand Up @@ -256,11 +273,19 @@ Message *Event::convert(const SDL_Event &e)
break;
}

#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.key, key);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.sym, key);
#endif
if (!love::keyboard::Keyboard::getConstant(key, txt))
txt = "unknown";

#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.scancode, scancode);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
#endif
if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
txt2 = "unknown";

Expand All @@ -270,11 +295,19 @@ Message *Event::convert(const SDL_Event &e)
msg = new Message("keypressed", vargs);
break;
case SDL_EVENT_KEY_UP:
#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.key, key);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.sym, key);
#endif
if (!love::keyboard::Keyboard::getConstant(key, txt))
txt = "unknown";

#if SDL_VERSION_ATLEAST(3, 0, 0)
love::keyboard::sdl::Keyboard::getConstant(e.key.scancode, scancode);
#else
love::keyboard::sdl::Keyboard::getConstant(e.key.keysym.scancode, scancode);
#endif
if (!love::keyboard::Keyboard::getConstant(scancode, txt2))
txt2 = "unknown";

Expand Down Expand Up @@ -551,7 +584,7 @@ Message *Event::convert(const SDL_Event &e)
{
SDL_Sensor *sensor = (SDL_Sensor *) s;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SensorID id = SDL_GetSensorInstanceID(sensor);
SDL_SensorID id = SDL_GetSensorID(sensor);
#else
SDL_SensorID id = SDL_SensorGetInstanceID(sensor);
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/modules/filesystem/wrap_Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,11 @@ int extloader(lua_State *L)
}
}

#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SharedObject *handle = nullptr;
#else
void *handle = nullptr;
#endif
auto *inst = instance();

#ifdef LOVE_ANDROID
Expand Down
16 changes: 8 additions & 8 deletions src/modules/joystick/sdl/JoystickModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ JoystickModule::JoystickModule()
: love::joystick::JoystickModule("love.joystick.sdl")
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD) < 0)
if (!SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD))
#else
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0)
#endif
Expand All @@ -60,8 +60,8 @@ JoystickModule::JoystickModule()

// Start joystick event watching. Joysticks are automatically added and
// removed via love.event.
SDL_SetJoystickEventsEnabled(SDL_TRUE);
SDL_SetGamepadEventsEnabled(SDL_TRUE);
SDL_SetJoystickEventsEnabled(true);
SDL_SetGamepadEventsEnabled(true);
#else
for (int i = 0; i < SDL_NumJoysticks(); i++)
addJoystick(i);
Expand Down Expand Up @@ -215,7 +215,7 @@ bool JoystickModule::setGamepadMapping(const std::string &guid, Joystick::Gamepa
throw love::Exception("Invalid joystick GUID: %s", guid.c_str());

#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_JoystickGUID sdlguid = SDL_GetJoystickGUIDFromString(guid.c_str());
SDL_GUID sdlguid = SDL_StringToGUID(guid.c_str());
std::string mapstr;

char *sdlmapstr = SDL_GetGamepadMappingForGUID(sdlguid);
Expand Down Expand Up @@ -468,8 +468,8 @@ std::string JoystickModule::getDeviceGUID(int64 deviceid) const
return std::string("");

// SDL's GUIDs identify *classes* of devices, instead of unique devices.
SDL_JoystickGUID guid = SDL_GetJoystickInstanceGUID((SDL_JoystickID)deviceid);
SDL_GetJoystickGUIDString(guid, guidstr, sizeof(guidstr));
SDL_GUID guid = SDL_GetJoystickGUIDForID((SDL_JoystickID)deviceid);
SDL_GUIDToString(guid, guidstr, sizeof(guidstr));
#else
int deviceindex = (int) deviceid;
if (deviceindex < 0 || deviceindex >= SDL_NumJoysticks())
Expand Down Expand Up @@ -548,7 +548,7 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
std::string JoystickModule::getGamepadMappingString(const std::string &guid) const
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_JoystickGUID sdlguid = SDL_GetJoystickGUIDFromString(guid.c_str());
SDL_GUID sdlguid = SDL_StringToGUID(guid.c_str());
char *sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
#else
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(guid.c_str());
Expand Down Expand Up @@ -577,7 +577,7 @@ std::string JoystickModule::saveGamepadMappings()
for (const auto &g : recentGamepadGUIDs)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_JoystickGUID sdlguid = SDL_GetJoystickGUIDFromString(g.first.c_str());
SDL_GUID sdlguid = SDL_StringToGUID(g.first.c_str());
char *sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
#else
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(g.first.c_str());
Expand Down
24 changes: 13 additions & 11 deletions src/modules/joystick/sdl/JoystickSDL3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#if SDL_VERSION_ATLEAST(3, 0, 0)

#include <SDL_guid.h>

// C++
#include <algorithm>
#include <limits>
Expand Down Expand Up @@ -62,13 +64,13 @@ bool Joystick::open(int64 deviceid)

if (joyhandle)
{
instanceid = SDL_GetJoystickInstanceID(joyhandle);
instanceid = SDL_GetJoystickID(joyhandle);

// SDL_JoystickGetGUIDString uses 32 bytes plus the null terminator.
char cstr[33];

SDL_JoystickGUID sdlguid = SDL_GetJoystickGUID(joyhandle);
SDL_GetJoystickGUIDString(sdlguid, cstr, (int) sizeof(cstr));
SDL_GUID sdlguid = SDL_GetJoystickGUID(joyhandle);
SDL_GUIDToString(sdlguid, cstr, (int) sizeof(cstr));

pguid = std::string(cstr);

Expand Down Expand Up @@ -212,7 +214,7 @@ bool Joystick::isDown(const std::vector<int> &buttonlist) const
if (button < 0 || button >= numbuttons)
continue;

if (SDL_GetJoystickButton(joyhandle, button) == 1)
if (SDL_GetJoystickButton(joyhandle, button))
return true;
}

Expand Down Expand Up @@ -304,7 +306,7 @@ bool Joystick::isGamepadDown(const std::vector<GamepadButton> &blist) const
if (!getConstant(button, sdlbutton))
continue;

if (SDL_GetGamepadButton(controller, sdlbutton) == 1)
if (SDL_GetGamepadButton(controller, sdlbutton))
return true;
}

Expand Down Expand Up @@ -382,7 +384,7 @@ std::string Joystick::getGamepadMappingString() const

if (sdlmapping == nullptr)
{
SDL_JoystickGUID sdlguid = SDL_GetJoystickGUIDFromString(pguid.c_str());
SDL_GUID sdlguid = SDL_StringToGUID(pguid.c_str());
sdlmapping = SDL_GetGamepadMappingForGUID(sdlguid);
}

Expand Down Expand Up @@ -445,7 +447,7 @@ bool Joystick::isVibrationSupported()
return false;

SDL_PropertiesID props = SDL_GetJoystickProperties(joyhandle);
return SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, SDL_FALSE);
return SDL_GetBooleanProperty(props, SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, false);
}

bool Joystick::setVibration(float left, float right, float duration)
Expand Down Expand Up @@ -489,7 +491,7 @@ bool Joystick::hasSensor(Sensor::SensorType type) const
if (!isGamepad())
return false;

return SDL_GamepadHasSensor(controller, SDLSensor::convert(type)) == SDL_TRUE;
return SDL_GamepadHasSensor(controller, SDLSensor::convert(type));
#else
return false;
#endif
Expand All @@ -503,7 +505,7 @@ bool Joystick::isSensorEnabled(Sensor::SensorType type) const
if (!isGamepad())
return false;

return SDL_GamepadSensorEnabled(controller, SDLSensor::convert(type)) == SDL_TRUE;
return SDL_GamepadSensorEnabled(controller, SDLSensor::convert(type));
#else
return false;
#endif
Expand All @@ -517,7 +519,7 @@ void Joystick::setSensorEnabled(Sensor::SensorType type, bool enabled)
if (!isGamepad())
throw love::Exception("Sensor is only supported on gamepad");

if (SDL_SetGamepadSensorEnabled(controller, SDLSensor::convert(type), enabled ? SDL_TRUE : SDL_FALSE) != 0)
if (!SDL_SetGamepadSensorEnabled(controller, SDLSensor::convert(type), enabled))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
Expand Down Expand Up @@ -547,7 +549,7 @@ std::vector<float> Joystick::getSensorData(Sensor::SensorType type) const
throw love::Exception("\"%s\" gamepad sensor is not enabled", name);
}

if (SDL_GetGamepadSensorData(controller, SDLSensor::convert(type), data.data(), (int) data.size()) != 0)
if (!SDL_GetGamepadSensorData(controller, SDLSensor::convert(type), data.data(), (int) data.size()))
{
const char *name = nullptr;
SDLSensor::getConstant(type, name);
Expand Down
Loading

0 comments on commit dda7603

Please sign in to comment.