mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
Merged SDL2 changes from bartbes/love-experiments/SDL2 into default.
- Added love.system module, including clipboard functions. - Added custom hardware cursors (love.mouse.newCursor, love.mouse.setCursor.) - Revamped love.joystick: -- added Joystick objects and moved love.joystick functions which operated on individual joysticks to methods on the Joystick objects. -- Added love.joystick.getJoystick and love.joystick.getJoystickCount. -- Added events for when joysticks are connected and disconnected. -- Added 'Gamepad' methods to Joystick objects: Joystick:isGamepad, Joystick:getGamepadAxis, and Joystick:isGamepadDown. They're a common abstraction for xbox controller-like joystick across operating systems. - Added monitor choosing support and "fullscreen desktop" mode to love.window. - Moved unicode text input events to their own callback function (love.textinput), removed the key repeat functions from love.keyboard and made the second argument of love.keypressed a boolean saying whether the keypress was a repeat. - liblove now works with love.window and love.graphics in OS X
This commit is contained in:
@@ -18,7 +18,11 @@
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "wrap_Joystick.h"
|
||||
#include "wrap_JoystickModule.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -27,141 +31,179 @@ namespace joystick
|
||||
namespace sdl
|
||||
{
|
||||
|
||||
static Joystick *instance = 0;
|
||||
|
||||
int w_reload(lua_State *L)
|
||||
Joystick *luax_checkjoystick(lua_State *L, int idx)
|
||||
{
|
||||
try
|
||||
{
|
||||
instance->reload();
|
||||
}
|
||||
catch(love::Exception &e)
|
||||
{
|
||||
return luaL_error(L, "%s", e.what());
|
||||
}
|
||||
return 0;
|
||||
return luax_checktype<Joystick>(L, idx, "Joystick", JOYSTICK_JOYSTICK_T);
|
||||
}
|
||||
|
||||
int w_getJoystickCount(lua_State *L)
|
||||
int w_Joystick_isConnected(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, instance->getJoystickCount());
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
luax_pushboolean(L, j->isConnected());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getName(lua_State *L)
|
||||
int w_Joystick_getName(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
lua_pushstring(L, instance->getName(index));
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
lua_pushstring(L, j->getName());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getAxisCount(lua_State *L)
|
||||
int w_Joystick_getID(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
lua_pushinteger(L, instance->getAxisCount(index));
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
lua_pushinteger(L, j->getID());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getButtonCount(lua_State *L)
|
||||
int w_Joystick_getGUID(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
lua_pushinteger(L, instance->getButtonCount(index));
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
luax_pushstring(L, j->getGUID());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getHatCount(lua_State *L)
|
||||
int w_Joystick_getAxisCount(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
lua_pushinteger(L, instance->getHatCount(index));
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
lua_pushinteger(L, j->getAxisCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getAxis(lua_State *L)
|
||||
int w_Joystick_getButtonCount(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
int axis = luaL_checkint(L, 2) - 1;
|
||||
lua_pushnumber(L, instance->getAxis(index, axis));
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
lua_pushinteger(L, j->getButtonCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getAxes(lua_State *L)
|
||||
int w_Joystick_getHatCount(lua_State *L)
|
||||
{
|
||||
return instance->getAxes(L);
|
||||
}
|
||||
|
||||
int w_isDown(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1) - 1;
|
||||
int num = lua_gettop(L);
|
||||
|
||||
int *buttonlist = new int[num];
|
||||
int counter = 0;
|
||||
|
||||
for (int i = 1; i < num; i++)
|
||||
buttonlist[counter++] = luaL_checkint(L, i + 1) - 1;
|
||||
|
||||
buttonlist[counter] = -1;
|
||||
|
||||
luax_pushboolean(L, instance->isDown(index, buttonlist));
|
||||
delete[] buttonlist;
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
lua_pushinteger(L, j->getHatCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getHat(lua_State *L)
|
||||
int w_Joystick_getAxis(lua_State *L)
|
||||
{
|
||||
int index = luaL_checkint(L, 1)-1;
|
||||
int hat = luaL_checkint(L, 2)-1;
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
int axisindex = luaL_checkint(L, 2) - 1;
|
||||
lua_pushnumber(L, j->getAxis(axisindex));
|
||||
return 1;
|
||||
}
|
||||
|
||||
Joystick::Hat h = instance->getHat(index, hat);
|
||||
int w_Joystick_getAxes(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
std::vector<float> axes = j->getAxes();
|
||||
|
||||
for (size_t i = 0; i < axes.size(); i++)
|
||||
lua_pushnumber(L, axes[i]);
|
||||
|
||||
return (int) axes.size();
|
||||
}
|
||||
|
||||
int w_Joystick_getHat(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
int hatindex = luaL_checkint(L, 2) - 1;
|
||||
|
||||
Joystick::Hat h = j->getHat(hatindex);
|
||||
|
||||
const char *direction = "";
|
||||
Joystick::getConstant(h, direction);
|
||||
lua_pushstring(L, direction);
|
||||
love::joystick::Joystick::getConstant(h, direction);
|
||||
|
||||
lua_pushstring(L, direction);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_isDown(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
|
||||
luaL_checkinteger(L, 2);
|
||||
|
||||
std::vector<int> buttons;
|
||||
for (int i = 2; i <= lua_gettop(L); i++)
|
||||
buttons.push_back(luaL_checkint(L, i));
|
||||
|
||||
luax_pushboolean(L, j->isDown(buttons));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_isGamepad(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
luax_pushboolean(L, j->isGamepad());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_getGamepadAxis(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
|
||||
const char *str = luaL_checkstring(L, 2);
|
||||
Joystick::GamepadAxis axis;
|
||||
|
||||
if (!joystick::Joystick::getConstant(str, axis))
|
||||
return luaL_error(L, "Invalid gamepad axis: %s", str);
|
||||
|
||||
lua_pushnumber(L, j->getGamepadAxis(axis));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Joystick_isGamepadDown(lua_State *L)
|
||||
{
|
||||
Joystick *j = luax_checkjoystick(L, 1);
|
||||
|
||||
std::vector<Joystick::GamepadButton> buttons;
|
||||
buttons.reserve(lua_gettop(L) - 1);
|
||||
|
||||
luaL_checkstring(L, 2);
|
||||
|
||||
for (int i = 2; i <= lua_gettop(L); i++)
|
||||
{
|
||||
const char *str = luaL_checkstring(L, i);
|
||||
Joystick::GamepadButton button;
|
||||
|
||||
if (!joystick::Joystick::getConstant(str, button))
|
||||
return luaL_error(L, "Invalid gamepad button: %s", str);
|
||||
|
||||
buttons.push_back(button);
|
||||
}
|
||||
|
||||
luax_pushboolean(L, j->isGamepadDown(buttons));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "reload", w_reload },
|
||||
{ "getJoystickCount", w_getJoystickCount },
|
||||
{ "getName", w_getName },
|
||||
{ "getAxisCount", w_getAxisCount },
|
||||
{ "getButtonCount", w_getButtonCount },
|
||||
{ "getHatCount", w_getHatCount },
|
||||
{ "getAxis", w_getAxis },
|
||||
{ "getAxes", w_getAxes },
|
||||
{ "isDown", w_isDown },
|
||||
{ "getHat", w_getHat },
|
||||
{ 0, 0 }
|
||||
{ "isConnected", w_Joystick_isConnected },
|
||||
{ "getName", w_Joystick_getName },
|
||||
{ "getID", w_Joystick_getID },
|
||||
{ "getGUID", w_Joystick_getGUID },
|
||||
{ "getAxisCount", w_Joystick_getAxisCount },
|
||||
{ "getButtonCount", w_Joystick_getButtonCount },
|
||||
{ "getHatCount", w_Joystick_getHatCount },
|
||||
{ "getAxis", w_Joystick_getAxis },
|
||||
{ "getAxes", w_Joystick_getAxes },
|
||||
{ "getHat", w_Joystick_getHat },
|
||||
{ "isDown", w_Joystick_isDown },
|
||||
{ "isGamepad", w_Joystick_isGamepad },
|
||||
{ "getGamepadAxis", w_Joystick_getGamepadAxis },
|
||||
{ "isGamepadDown", w_Joystick_isGamepadDown },
|
||||
|
||||
// From wrap_JoystickModule.
|
||||
{ "getIndex", w_getIndex },
|
||||
{ "getGamepadMapping", w_getGamepadMapping },
|
||||
{ 0, 0 },
|
||||
};
|
||||
|
||||
extern "C" int luaopen_love_joystick(lua_State *L)
|
||||
extern "C" int luaopen_joystick(lua_State *L)
|
||||
{
|
||||
if (instance == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
instance = new Joystick();
|
||||
}
|
||||
catch(Exception &e)
|
||||
{
|
||||
return luaL_error(L, e.what());
|
||||
}
|
||||
}
|
||||
else
|
||||
instance->retain();
|
||||
|
||||
|
||||
WrappedModule w;
|
||||
w.module = instance;
|
||||
w.name = "joystick";
|
||||
w.flags = MODULE_T;
|
||||
w.functions = functions;
|
||||
w.types = 0;
|
||||
|
||||
return luax_register_module(L, w);
|
||||
return luax_register_type(L, "Joystick", functions);
|
||||
}
|
||||
|
||||
} // sdl
|
||||
|
||||
Reference in New Issue
Block a user