mirror of https://github.com/bjornbytes/lovr.git
Refactor;
This commit is contained in:
parent
22ea91bf0b
commit
462b2d5f50
|
@ -10,5 +10,10 @@ LIBS += -l osvrClientKit
|
|||
LIBS += -framework OpenGL
|
||||
LIBS += -pagezero_size 10000 -image_base 100000000 # OSX magic for LuaJIT
|
||||
|
||||
: foreach event/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: foreach graphics/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: foreach joystick/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: foreach timer/*.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
|
||||
: foreach *.c |> clang -c %f -o %o $(CFLAGS) |> obj/%B.o
|
||||
: obj/*.o |> clang -o %o %f $(LIBS) |> ../lovr
|
||||
|
|
77
src/device.c
77
src/device.c
|
@ -1,77 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include "device.h"
|
||||
#include "interface.h"
|
||||
#include "osvr.h"
|
||||
#include "util.h"
|
||||
|
||||
extern OSVR_ClientContext ctx;
|
||||
|
||||
int lovrDeviceGetByName(lua_State* L) {
|
||||
const char* name = luaL_checkstring(L, 1);
|
||||
|
||||
Interface* interface = (Interface*) malloc(sizeof(Interface));
|
||||
osvrClientGetInterface(ctx, name, interface);
|
||||
|
||||
if (interface) {
|
||||
luax_pushinterface(L, interface);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrDeviceGetHeadset(lua_State* L) {
|
||||
const char* name = "/me/head";
|
||||
|
||||
Interface* headset = (Interface*) malloc(sizeof(Interface));
|
||||
osvrClientGetInterface(ctx, name, headset);
|
||||
|
||||
if (headset) {
|
||||
luax_pushinterface(L, headset);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrDeviceGetControllers(lua_State* L) {
|
||||
const char* leftHandPath = "/me/hands/left";
|
||||
const char* rightHandPath = "/me/hands/right";
|
||||
|
||||
Interface* leftHand = (Interface*) malloc(sizeof(Interface));
|
||||
osvrClientGetInterface(ctx, leftHandPath, leftHand);
|
||||
|
||||
if (leftHand) {
|
||||
luax_pushinterface(L, leftHand);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
Interface* rightHand = (Interface*) malloc(sizeof(Interface));
|
||||
osvrClientGetInterface(ctx, rightHandPath, rightHand);
|
||||
|
||||
if (rightHand) {
|
||||
luax_pushinterface(L, rightHand);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrDevice[] = {
|
||||
{ "getByName", lovrDeviceGetByName },
|
||||
{ "getHeadset", lovrDeviceGetHeadset },
|
||||
{ "getControllers", lovrDeviceGetControllers },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrInitDevice(lua_State* L) {
|
||||
lua_newtable(L);
|
||||
luaL_register(L, NULL, lovrDevice);
|
||||
luaRegisterType(L, "Interface", lovrInterface);
|
||||
initOSVR();
|
||||
return 1;
|
||||
}
|
10
src/device.h
10
src/device.h
|
@ -1,10 +0,0 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
int lovrDeviceGetByName(lua_State* L);
|
||||
int lovrDeviceGetHeadset(lua_State* L);
|
||||
int lovrDeviceGetControllers(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrDevice[];
|
||||
int lovrInitDevice(lua_State* L);
|
|
@ -1,7 +1,7 @@
|
|||
#include "event.h"
|
||||
#include "lovr.h"
|
||||
#include "glfw.h"
|
||||
#include "osvr.h"
|
||||
#include "../lovr.h"
|
||||
#include "../glfw.h"
|
||||
#include "../osvr.h"
|
||||
|
||||
extern GLFWwindow* window;
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
#include "buffer.h"
|
||||
#include "glfw.h"
|
||||
|
||||
void luax_pushbuffer(lua_State* L, Buffer* buffer) {
|
||||
Buffer** userdata = (Buffer**) lua_newuserdata(L, sizeof(Buffer*));
|
|
@ -1,7 +1,7 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "glfw.h"
|
||||
#include "../glfw.h"
|
||||
|
||||
typedef struct {
|
||||
GLfloat* data;
|
|
@ -1,9 +1,9 @@
|
|||
#include "glfw.h"
|
||||
#include "graphics.h"
|
||||
#include "model.h"
|
||||
#include "buffer.h"
|
||||
#include "shader.h"
|
||||
#include "util.h"
|
||||
#include "../glfw.h"
|
||||
#include "../util.h"
|
||||
#include <stdlib.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
|
@ -1,7 +1,7 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "glfw.h"
|
||||
#include "../glfw.h"
|
||||
|
||||
typedef struct {
|
||||
GLuint id;
|
|
@ -1,62 +0,0 @@
|
|||
#include "interface.h"
|
||||
#include "util.h"
|
||||
#include "osvr.h"
|
||||
|
||||
void luax_pushinterface(lua_State* L, Interface* interface) {
|
||||
Interface** userdata = (Interface**) lua_newuserdata(L, sizeof(Interface*));
|
||||
|
||||
luaL_getmetatable(L, "Interface");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
*userdata = interface;
|
||||
}
|
||||
|
||||
Interface* luax_checkinterface(lua_State* L, int index) {
|
||||
return *(Interface**) luaL_checkudata(L, index, "Interface");
|
||||
}
|
||||
|
||||
int lovrInterfaceGetPosition(lua_State* L) {
|
||||
Interface* interface = luax_checkinterface(L, 1);
|
||||
OSVR_TimeValue t;
|
||||
OSVR_PositionState position;
|
||||
|
||||
OSVR_ReturnCode res = osvrGetPositionState(*interface, &t, &position);
|
||||
|
||||
if (res != OSVR_RETURN_SUCCESS) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_pushnumber(L, position.data[0]);
|
||||
lua_pushnumber(L, position.data[1]);
|
||||
lua_pushnumber(L, position.data[2]);
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int lovrInterfaceGetOrientation(lua_State* L) {
|
||||
Interface* interface = luax_checkinterface(L, 1);
|
||||
OSVR_TimeValue t;
|
||||
OSVR_OrientationState orientation;
|
||||
|
||||
osvrClientUpdate(ctx);
|
||||
|
||||
OSVR_ReturnCode res = osvrGetOrientationState(*interface, &t, &orientation);
|
||||
|
||||
if (res != OSVR_RETURN_SUCCESS) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_pushnumber(L, orientation.data[0]);
|
||||
lua_pushnumber(L, orientation.data[1]);
|
||||
lua_pushnumber(L, orientation.data[2]);
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrInterface[] = {
|
||||
{ "getPosition", lovrInterfaceGetPosition },
|
||||
{ "getOrientation", lovrInterfaceGetOrientation },
|
||||
{ NULL, NULL }
|
||||
};
|
|
@ -1,14 +0,0 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#include <osvr/ClientKit/InterfaceC.h>
|
||||
#include <osvr/ClientKit/InterfaceStateC.h>
|
||||
|
||||
typedef OSVR_ClientInterface Interface;
|
||||
void luax_pushinterface(lua_State* L, Interface* interface);
|
||||
Interface* luax_checkinterface(lua_State* L, int index);
|
||||
|
||||
int lovrInterfaceGetPosition(lua_State* L);
|
||||
int lovrInterfaceGetOrientation(lua_State* L);
|
||||
extern const luaL_Reg lovrInterface[];
|
|
@ -1,5 +1,5 @@
|
|||
#include "joystick.h"
|
||||
#include "glfw.h"
|
||||
#include "../glfw.h"
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick) {
|
||||
Joystick** userdata = (Joystick**) lua_newuserdata(L, sizeof(Joystick*));
|
|
@ -1,6 +1,7 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "../osvr.h"
|
||||
|
||||
typedef enum {
|
||||
JOYSTICK_TYPE_GLFW,
|
||||
|
@ -10,6 +11,7 @@ typedef enum {
|
|||
typedef struct {
|
||||
JoystickType type;
|
||||
int index;
|
||||
OSVR_ClientInterface* interface;
|
||||
} Joystick;
|
||||
|
||||
void luax_pushjoystick(lua_State* L, Joystick* joystick);
|
|
@ -1,7 +1,8 @@
|
|||
#include "joysticks.h"
|
||||
#include "glfw.h"
|
||||
#include "joystick.h"
|
||||
#include "util.h"
|
||||
#include "../glfw.h"
|
||||
#include "../util.h"
|
||||
#include "../osvr.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
|
@ -13,15 +14,42 @@ JoystickState joystickState;
|
|||
|
||||
void lovrJoysticksRefresh() {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if (joystickState.list[i] != NULL) {
|
||||
free(joystickState.list[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;
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->type = JOYSTICK_TYPE_GLFW;
|
||||
joystick->index = i;
|
||||
|
||||
joystickState.list[count++] = joystick;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO handle OSVR joysticks
|
||||
// /me/hands/left
|
||||
// /me/hands/right
|
||||
|
||||
if (osvrClientCheckStatus(ctx) != OSVR_RETURN_FAILURE) {
|
||||
const char* hands[2] = { "/me/hands/left", "/me/hands/right" };
|
||||
|
||||
for (int i = 0; i < sizeof(hands); i++) {
|
||||
OSVR_ClientInterface* interface = (OSVR_ClientInterface*) malloc(sizeof(OSVR_ClientInterface));
|
||||
osvrClientGetInterface(ctx, hands[i], interface);
|
||||
|
||||
if (interface != NULL) {
|
||||
Joystick* joystick = malloc(sizeof(Joystick));
|
||||
joystick->type = JOYSTICK_TYPE_OSVR;
|
||||
joystick->index = -1;
|
||||
joystick->interface = interface;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
src/lovr.c
13
src/lovr.c
|
@ -1,11 +1,10 @@
|
|||
#include "lovr.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "event.h"
|
||||
#include "device.h"
|
||||
#include "graphics.h"
|
||||
#include "joysticks.h"
|
||||
#include "timer.h"
|
||||
#include "event/event.h"
|
||||
#include "graphics/graphics.h"
|
||||
#include "joystick/joysticks.h"
|
||||
#include "timer/timer.h"
|
||||
|
||||
extern lua_State* L;
|
||||
|
||||
|
@ -17,7 +16,6 @@ void lovrInit(lua_State* L) {
|
|||
|
||||
// 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);
|
||||
|
@ -28,7 +26,6 @@ void lovrInit(lua_State* L) {
|
|||
"local conf = { "
|
||||
" modules = { "
|
||||
" event = true, "
|
||||
" device = true, "
|
||||
" graphics = true, "
|
||||
" joystick = true, "
|
||||
" timer = true "
|
||||
|
@ -44,7 +41,7 @@ void lovrInit(lua_State* L) {
|
|||
" error(err, -1) "
|
||||
"end "
|
||||
" "
|
||||
"local modules = { 'event', 'device', 'graphics', 'joystick', 'timer' } "
|
||||
"local modules = { 'event', 'graphics', 'joystick', 'timer' } "
|
||||
"for _, module in ipairs(modules) do "
|
||||
" if conf.modules[module] then "
|
||||
" lovr[module] = require('lovr.' .. module) "
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "timer.h"
|
||||
#include "lovr.h"
|
||||
#include "glfw.h"
|
||||
#include "../lovr.h"
|
||||
#include "../glfw.h"
|
||||
|
||||
int lovrTimerStep(lua_State* L) {
|
||||
lua_pushnumber(L, glfwGetTime());
|
Loading…
Reference in New Issue