From b1b70a83db3c379c19f2eabc5d1ccbf0fce70abf Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 15 Feb 2021 16:08:23 -0400 Subject: [PATCH] Add Joystick:set/getPlayerIndex --- src/modules/joystick/Joystick.h | 3 +++ src/modules/joystick/sdl/Joystick.cpp | 24 ++++++++++++++++++++++++ src/modules/joystick/sdl/Joystick.h | 3 +++ src/modules/joystick/wrap_Joystick.cpp | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/modules/joystick/Joystick.h b/src/modules/joystick/Joystick.h index 5242f967c..c635f5bff 100644 --- a/src/modules/joystick/Joystick.h +++ b/src/modules/joystick/Joystick.h @@ -165,6 +165,9 @@ public: virtual bool isDown(const std::vector &buttonlist) const = 0; + virtual void setPlayerIndex(int index) = 0; + virtual int getPlayerIndex() const = 0; + virtual bool openGamepad(int deviceindex) = 0; virtual bool isGamepad() const = 0; diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index f3cfcde0c..f05de9372 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -195,6 +195,30 @@ bool Joystick::isDown(const std::vector &buttonlist) const return false; } +void Joystick::setPlayerIndex(int index) +{ + if (!isConnected()) + return; + +#if SDL_VERSION_ATLEAST(2, 0, 12) + SDL_JoystickSetPlayerIndex(joyhandle, index); +#else + LOVE_UNUSED(index); +#endif +} + +int Joystick::getPlayerIndex() const +{ + if (!isConnected()) + return -1; + +#if SDL_VERSION_ATLEAST(2, 0, 12) + return SDL_JoystickGetPlayerIndex(joyhandle); +#else + return -1; +#endif +} + bool Joystick::openGamepad(int deviceindex) { if (!SDL_IsGameController(deviceindex)) diff --git a/src/modules/joystick/sdl/Joystick.h b/src/modules/joystick/sdl/Joystick.h index c559ea228..1f378d9ed 100644 --- a/src/modules/joystick/sdl/Joystick.h +++ b/src/modules/joystick/sdl/Joystick.h @@ -61,6 +61,9 @@ public: bool isDown(const std::vector &buttonlist) const override; + void setPlayerIndex(int index) override; + int getPlayerIndex() const override; + bool openGamepad(int deviceindex) override; bool isGamepad() const override; diff --git a/src/modules/joystick/wrap_Joystick.cpp b/src/modules/joystick/wrap_Joystick.cpp index 76b9f6db9..9c23def9e 100644 --- a/src/modules/joystick/wrap_Joystick.cpp +++ b/src/modules/joystick/wrap_Joystick.cpp @@ -171,6 +171,22 @@ int w_Joystick_isDown(lua_State *L) return 1; } +int w_Joystick_setPlayerIndex(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + int index = (int) luaL_checkinteger(L, 2) - 1; + j->setPlayerIndex(index); + return 0; +} + +int w_Joystick_getPlayerIndex(lua_State *L) +{ + Joystick *j = luax_checkjoystick(L, 1); + int index = j->getPlayerIndex(); + lua_pushinteger(L, index >= 0 ? index + 1 : index); + return 1; +} + int w_Joystick_isGamepad(lua_State *L) { Joystick *j = luax_checkjoystick(L, 1); @@ -366,6 +382,8 @@ static const luaL_Reg w_Joystick_functions[] = { "getAxes", w_Joystick_getAxes }, { "getHat", w_Joystick_getHat }, { "isDown", w_Joystick_isDown }, + { "setPlayerIndex", w_Joystick_setPlayerIndex }, + { "getPlayerIndex", w_Joystick_getPlayerIndex }, { "isGamepad", w_Joystick_isGamepad }, { "getGamepadType", w_Joystick_getGamepadType },