love.sensor Lua API

This commit is contained in:
Miku AuahDark
2022-12-05 13:57:39 +08:00
parent 8f7eac4fde
commit 332b8d01ac
3 changed files with 99 additions and 0 deletions
+5
View File
@@ -44,6 +44,11 @@ Sensor::~Sensor()
SDL_QuitSubSystem(SDL_INIT_SENSOR);
}
const char *Sensor::getName() const
{
return "love.sensor.sdl";
}
bool Sensor::isAvailable(SensorType type)
{
for (int i = 0; i < SDL_NumSensors(); i++)
+3
View File
@@ -43,6 +43,9 @@ public:
Sensor();
~Sensor() override;
// Implements Module.
const char *getName() const override;
bool isAvailable(SensorType type) override;
bool isEnabled(SensorType type) override;
void setEnabled(SensorType type, bool enable) override;
+91
View File
@@ -17,3 +17,94 @@
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "wrap_Sensor.h"
#include "sdl/Sensor.h"
namespace love
{
namespace sensor
{
#define instance() (Module::getInstance<Sensor>(Module::M_SENSOR))
inline Sensor::SensorType luax_checksensortype(lua_State *L, int i)
{
const char *sensorType = luaL_checkstring(L, i);
Sensor::SensorType type = Sensor::SENSOR_MAX_ENUM;
if (!Sensor::getConstant(sensorType, type))
luax_enumerror(L, "sensor mode", Sensor::getConstants(type), sensorType);
return type;
}
static int w_isAvailable(lua_State *L)
{
Sensor::SensorType type = luax_checksensortype(L, 1);
lua_pushboolean(L, instance()->isAvailable(type));
return 1;
}
static int w_isEnabled(lua_State *L)
{
Sensor::SensorType type = luax_checksensortype(L, 1);
lua_pushboolean(L, instance()->isEnabled(type));
return 1;
}
static int w_setEnabled(lua_State *L)
{
Sensor::SensorType type = luax_checksensortype(L, 1);
bool enabled = luax_checkboolean(L, 2);
luax_catchexcept(L, [&](){ instance()->setEnabled(type, enabled); });
return 0;
}
static int w_getData(lua_State *L)
{
Sensor::SensorType type = luax_checksensortype(L, 1);
std::vector<float> data;
luax_catchexcept(L, [&](){ data = instance()->getData(type); });
for (float f: data)
lua_pushnumber(L, f);
return data.size();
}
static const luaL_Reg functions[] =
{
{ "isAvailable", w_isAvailable },
{ "hasSensor", w_isAvailable },
{ "isEnabled", w_isEnabled },
{ "setEnabled", w_setEnabled },
{ "getData", w_getData },
{ nullptr, nullptr }
};
extern "C" int luaopen_love_sensor(lua_State * L)
{
Sensor *instance = instance();
if (instance == nullptr)
luax_catchexcept(L, [&]() { instance = new love::sensor::sdl::Sensor(); });
else
instance->retain();
WrappedModule w;
w.module = instance;
w.name = "sensor";
w.type = &Module::type;
w.functions = functions;
w.types = nullptr;
return luax_register_module(L, w);
}
} // sensor
} // love