mirror of https://github.com/bjornbytes/lovr.git
Joystick;
This commit is contained in:
parent
a6b6754f57
commit
22ea91bf0b
|
@ -68,7 +68,7 @@ const luaL_Reg lovrDevice[] = {
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrPushDevice(lua_State* L) {
|
||||
int lovrInitDevice(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrDevice);
|
||||
luaRegisterType(L, "Interface", lovrInterface);
|
||||
|
|
|
@ -7,4 +7,4 @@ int lovrDeviceGetHeadset(lua_State* L);
|
|||
int lovrDeviceGetControllers(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrDevice[];
|
||||
int lovrPushDevice(lua_State* L);
|
||||
int lovrInitDevice(lua_State* L);
|
||||
|
|
|
@ -27,7 +27,7 @@ const luaL_Reg lovrEvent[] = {
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrPushEvent(lua_State* L) {
|
||||
int lovrInitEvent(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrEvent);
|
||||
return 1;
|
||||
|
|
|
@ -7,4 +7,4 @@ int lovrEventPoll(lua_State* L);
|
|||
int lovrEventQuit(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrEvent[];
|
||||
int lovrPushEvent(lua_State* L);
|
||||
int lovrInitEvent(lua_State* L);
|
||||
|
|
|
@ -120,7 +120,7 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrPushGraphics(lua_State* L) {
|
||||
int lovrInitGraphics(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrGraphics);
|
||||
luaRegisterType(L, "Model", lovrModel);
|
||||
|
|
|
@ -10,4 +10,4 @@ int lovrGraphicsNewBuffer(lua_State* L);
|
|||
int lovrGraphicsShader(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrGraphics[];
|
||||
int lovrPushGraphics(lua_State* L);
|
||||
int lovrInitGraphics(lua_State* L);
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
#include "joystick.h"
|
||||
#include "glfw.h"
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick) {
|
||||
Joystick** userdata = (Joystick**) lua_newuserdata(L, sizeof(Joystick*));
|
||||
|
||||
luaL_getmetatable(L, "Joystick");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
*userdata = joystick;
|
||||
}
|
||||
|
||||
Joystick* luax_checkjoystick(lua_State* L, int index) {
|
||||
return *(Joystick**) luaL_checkudata(L, index, "Joystick");
|
||||
}
|
||||
|
||||
int lovrJoystickIsGamepad(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushboolean(L, joystick->type == JOYSTICK_TYPE_GLFW);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickIsTracked(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
lua_pushboolean(L, joystick->type == JOYSTICK_TYPE_OSVR);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetRawAxes(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int axisCount;
|
||||
const float* axes = glfwGetJoystickAxes(joystick->index, &axisCount);
|
||||
|
||||
for (int i = 0; i < axisCount; i++) {
|
||||
lua_pushnumber(L, axes[i]);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoystickGetRawButtons(lua_State* L) {
|
||||
Joystick* joystick = luax_checkjoystick(L, 1);
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
if (joystick->type == JOYSTICK_TYPE_OSVR) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int buttonCount;
|
||||
const unsigned char* buttons = glfwGetJoystickButtons(joystick->index, &buttonCount);
|
||||
|
||||
for (int i = 0; i < buttonCount; i++) {
|
||||
lua_pushboolean(L, buttons[i]);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrJoystick[] = {
|
||||
{ "isGamepad", lovrJoystickIsGamepad },
|
||||
{ "isTracked", lovrJoystickIsTracked },
|
||||
{ "getRawAxes", lovrJoystickGetRawAxes },
|
||||
{ "getRawButtons", lovrJoystickGetRawButtons },
|
||||
{ NULL, NULL }
|
||||
};
|
|
@ -0,0 +1,22 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
typedef enum {
|
||||
JOYSTICK_TYPE_GLFW,
|
||||
JOYSTICK_TYPE_OSVR
|
||||
} JoystickType;
|
||||
|
||||
typedef struct {
|
||||
JoystickType type;
|
||||
int index;
|
||||
} Joystick;
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick);
|
||||
Joystick* luax_checkjoystick(lua_State* L, int index);
|
||||
|
||||
int lovrJoystickIsGamepad(lua_State* L);
|
||||
int lovrJoystickIsTracked(lua_State* L);
|
||||
int lovrJoystickGetRawAxes(lua_State* L);
|
||||
int lovrJoystickGetRawButtons(lua_State* L);
|
||||
extern const luaL_Reg lovrJoystick[];
|
|
@ -0,0 +1,62 @@
|
|||
#include "joysticks.h"
|
||||
#include "glfw.h"
|
||||
#include "joystick.h"
|
||||
#include "util.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
Joystick* list[32];
|
||||
int count;
|
||||
} JoystickState;
|
||||
|
||||
JoystickState joystickState;
|
||||
|
||||
void lovrJoysticksRefresh() {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
joystickState.list[i] = NULL;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
|
||||
if (glfwJoystickPresent(i)) {
|
||||
int index = count++;
|
||||
joystickState.list[index] = malloc(sizeof(Joystick));
|
||||
joystickState.list[index]->index = i;
|
||||
}
|
||||
}
|
||||
|
||||
joystickState.count = count;
|
||||
}
|
||||
|
||||
int lovrJoysticksGetJoystickCount(lua_State* L) {
|
||||
lua_pushnumber(L, joystickState.count);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrJoysticksGetJoysticks(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
|
||||
int i = 0;
|
||||
|
||||
while (joystickState.list[i] != NULL) {
|
||||
luax_pushjoystick(L, joystickState.list[i]);
|
||||
lua_rawseti(L, -2, ++i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrJoysticks[] = {
|
||||
{ "getJoystickCount", lovrJoysticksGetJoystickCount },
|
||||
{ "getJoysticks", lovrJoysticksGetJoysticks },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrInitJoysticks(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrJoysticks);
|
||||
luaRegisterType(L, "Joystick", lovrJoystick);
|
||||
lovrJoysticksRefresh();
|
||||
return 1;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
int lovrJoysticksGetJoystickCount(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrJoysticks[];
|
||||
int lovrInitJoysticks(lua_State* L);
|
17
src/lovr.c
17
src/lovr.c
|
@ -4,21 +4,23 @@
|
|||
#include "event.h"
|
||||
#include "device.h"
|
||||
#include "graphics.h"
|
||||
#include "joysticks.h"
|
||||
#include "timer.h"
|
||||
|
||||
extern lua_State* L;
|
||||
|
||||
void lovrInit(lua_State* L) {
|
||||
|
||||
// Write top-level lovr global
|
||||
// lovr = {}
|
||||
lua_newtable(L);
|
||||
lua_setglobal(L, "lovr");
|
||||
|
||||
// Register modules
|
||||
luaPreloadModule(L, "lovr.event", lovrPushEvent);
|
||||
luaPreloadModule(L, "lovr.device", lovrPushDevice);
|
||||
luaPreloadModule(L, "lovr.graphics", lovrPushGraphics);
|
||||
luaPreloadModule(L, "lovr.timer", lovrPushTimer);
|
||||
// Preload modules
|
||||
luaPreloadModule(L, "lovr.event", lovrInitEvent);
|
||||
luaPreloadModule(L, "lovr.device", lovrInitDevice);
|
||||
luaPreloadModule(L, "lovr.graphics", lovrInitGraphics);
|
||||
luaPreloadModule(L, "lovr.joystick", lovrInitJoysticks);
|
||||
luaPreloadModule(L, "lovr.timer", lovrInitTimer);
|
||||
|
||||
// Bootstrap
|
||||
char buffer[1024];
|
||||
|
@ -28,6 +30,7 @@ void lovrInit(lua_State* L) {
|
|||
" event = true, "
|
||||
" device = true, "
|
||||
" graphics = true, "
|
||||
" joystick = true, "
|
||||
" timer = true "
|
||||
" } "
|
||||
"} "
|
||||
|
@ -41,7 +44,7 @@ void lovrInit(lua_State* L) {
|
|||
" error(err, -1) "
|
||||
"end "
|
||||
" "
|
||||
"local modules = { 'event', 'device', 'graphics', 'timer' } "
|
||||
"local modules = { 'event', 'device', 'graphics', 'joystick', 'timer' } "
|
||||
"for _, module in ipairs(modules) do "
|
||||
" if conf.modules[module] then "
|
||||
" lovr[module] = require('lovr.' .. module) "
|
||||
|
|
|
@ -12,8 +12,8 @@ int main(int argc, char* argv[]) {
|
|||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
|
||||
lovrInit(L);
|
||||
initGlfw(lovrOnError, lovrOnClose);
|
||||
lovrInit(L);
|
||||
lovrRun(L);
|
||||
|
||||
exit(0);
|
||||
|
|
|
@ -14,7 +14,7 @@ const luaL_Reg lovrTimer[] = {
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrPushTimer(lua_State* L) {
|
||||
int lovrInitTimer(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrTimer);
|
||||
return 1;
|
||||
|
|
|
@ -5,4 +5,4 @@
|
|||
int lovrTimerStep(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrTimer[];
|
||||
int lovrPushTimer(lua_State* L);
|
||||
int lovrInitTimer(lua_State* L);
|
||||
|
|
Loading…
Reference in New Issue