mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Add love.joystick.reload, remove love.joystick.open and friends. (issue #392)
Reload originates from love-experiments, as noted in the ticket.
This commit is contained in:
@@ -34,7 +34,7 @@ namespace sdl
|
||||
{
|
||||
// Init the SDL joystick system.
|
||||
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
|
||||
throw Exception(SDL_GetError());
|
||||
throw love::Exception("%s", SDL_GetError());
|
||||
|
||||
// Start joystick event watching.
|
||||
SDL_JoystickEventState(SDL_ENABLE);
|
||||
@@ -61,6 +61,29 @@ namespace sdl
|
||||
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
||||
}
|
||||
|
||||
void Joystick::reload()
|
||||
{
|
||||
// Closes any open joysticks.
|
||||
for (int i = 0; i != getNumJoysticks(); i++)
|
||||
{
|
||||
if (isOpen(i))
|
||||
close(i);
|
||||
}
|
||||
|
||||
free(joysticks);
|
||||
|
||||
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
|
||||
|
||||
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
|
||||
throw love::Exception("%s", SDL_GetError());
|
||||
|
||||
int numjoysticks = this->getNumJoysticks();
|
||||
this->joysticks = (SDL_Joystick **)calloc(numjoysticks, sizeof(SDL_Joystick*));
|
||||
|
||||
for (int i = 0;i<numjoysticks;i++)
|
||||
this->open(i);
|
||||
}
|
||||
|
||||
const char * Joystick::getName() const
|
||||
{
|
||||
return "love.joystick.sdl";
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace sdl
|
||||
// Implements Module.
|
||||
const char * getName() const;
|
||||
|
||||
void reload();
|
||||
bool checkIndex(int index);
|
||||
int getNumJoysticks();
|
||||
const char * getName(int index);
|
||||
|
||||
@@ -28,6 +28,19 @@ namespace sdl
|
||||
{
|
||||
static Joystick * instance = 0;
|
||||
|
||||
int w_reload(lua_State * L)
|
||||
{
|
||||
try
|
||||
{
|
||||
instance->reload();
|
||||
}
|
||||
catch (love::Exception & e)
|
||||
{
|
||||
return luaL_error(L, "%s", e.what());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getNumJoysticks(lua_State * L)
|
||||
{
|
||||
lua_pushinteger(L, instance->getNumJoysticks());
|
||||
@@ -41,20 +54,6 @@ namespace sdl
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_open(lua_State * L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1)-1;
|
||||
luax_pushboolean(L, instance->open(index));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_isOpen(lua_State * L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1)-1;
|
||||
luax_pushboolean(L, instance->isOpen(index));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getNumAxes(lua_State * L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1)-1;
|
||||
@@ -133,19 +132,11 @@ namespace sdl
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_close(lua_State * L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1)-1;
|
||||
instance->close(index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] = {
|
||||
{ "reload", w_reload },
|
||||
{ "getNumJoysticks", w_getNumJoysticks },
|
||||
{ "getName", w_getName },
|
||||
{ "open", w_open },
|
||||
{ "isOpen", w_isOpen },
|
||||
{ "getNumAxes", w_getNumAxes },
|
||||
{ "getNumBalls", w_getNumBalls },
|
||||
{ "getNumButtons", w_getNumButtons },
|
||||
@@ -157,7 +148,6 @@ namespace sdl
|
||||
|
||||
{ "isDown", w_isDown },
|
||||
{ "getHat", w_getHat },
|
||||
{ "close", w_close },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
@@ -31,10 +31,9 @@ namespace joystick
|
||||
{
|
||||
namespace sdl
|
||||
{
|
||||
int w_reload(lua_State * L);
|
||||
int w_getNumJoysticks(lua_State * L);
|
||||
int w_getName(lua_State * L);
|
||||
int w_open(lua_State * L);
|
||||
int w_isOpen(lua_State * L);
|
||||
int w_getNumAxes(lua_State * L);
|
||||
int w_getNumBalls(lua_State * L);
|
||||
int w_getNumButtons(lua_State * L);
|
||||
@@ -44,7 +43,6 @@ namespace sdl
|
||||
int w_getBall(lua_State * L);
|
||||
int w_isDown(lua_State * L);
|
||||
int w_getHat(lua_State * L);
|
||||
int w_close(lua_State * L);
|
||||
extern "C" LOVE_EXPORT int luaopen_love_joystick(lua_State * L);
|
||||
|
||||
} // sdl
|
||||
|
||||
Reference in New Issue
Block a user