From 462b2d5f5058c9287fa2fa2d9d07ad671c5c0c9f Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 7 Aug 2016 18:32:37 -0700 Subject: [PATCH] Refactor; --- src/Tupfile | 5 +++ src/device.c | 77 ---------------------------------- src/device.h | 10 ----- src/{ => event}/event.c | 6 +-- src/{ => event}/event.h | 0 src/{ => graphics}/buffer.c | 1 - src/{ => graphics}/buffer.h | 2 +- src/{ => graphics}/graphics.c | 4 +- src/{ => graphics}/graphics.h | 0 src/{ => graphics}/model.c | 0 src/{ => graphics}/model.h | 0 src/{ => graphics}/shader.c | 0 src/{ => graphics}/shader.h | 2 +- src/interface.c | 62 --------------------------- src/interface.h | 14 ------- src/{ => joystick}/joystick.c | 2 +- src/{ => joystick}/joystick.h | 2 + src/{ => joystick}/joysticks.c | 38 ++++++++++++++--- src/{ => joystick}/joysticks.h | 0 src/lovr.c | 13 +++--- src/{ => timer}/timer.c | 4 +- src/{ => timer}/timer.h | 0 22 files changed, 55 insertions(+), 187 deletions(-) delete mode 100644 src/device.c delete mode 100644 src/device.h rename src/{ => event}/event.c (88%) rename src/{ => event}/event.h (100%) rename src/{ => graphics}/buffer.c (98%) rename src/{ => graphics}/buffer.h (94%) rename src/{ => graphics}/graphics.c (98%) rename src/{ => graphics}/graphics.h (100%) rename src/{ => graphics}/model.c (100%) rename src/{ => graphics}/model.h (100%) rename src/{ => graphics}/shader.c (100%) rename src/{ => graphics}/shader.h (94%) delete mode 100644 src/interface.c delete mode 100644 src/interface.h rename src/{ => joystick}/joystick.c (98%) rename src/{ => joystick}/joystick.h (90%) rename src/{ => joystick}/joysticks.c (53%) rename src/{ => joystick}/joysticks.h (100%) rename src/{ => timer}/timer.c (88%) rename src/{ => timer}/timer.h (100%) diff --git a/src/Tupfile b/src/Tupfile index 155d3c29..eb9f41b6 100644 --- a/src/Tupfile +++ b/src/Tupfile @@ -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 diff --git a/src/device.c b/src/device.c deleted file mode 100644 index ded75416..00000000 --- a/src/device.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#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; -} diff --git a/src/device.h b/src/device.h deleted file mode 100644 index b57c6139..00000000 --- a/src/device.h +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include - -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); diff --git a/src/event.c b/src/event/event.c similarity index 88% rename from src/event.c rename to src/event/event.c index 1b040e70..789bd6c4 100644 --- a/src/event.c +++ b/src/event/event.c @@ -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; diff --git a/src/event.h b/src/event/event.h similarity index 100% rename from src/event.h rename to src/event/event.h diff --git a/src/buffer.c b/src/graphics/buffer.c similarity index 98% rename from src/buffer.c rename to src/graphics/buffer.c index 6c8a1173..a5ebfbec 100644 --- a/src/buffer.c +++ b/src/graphics/buffer.c @@ -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*)); diff --git a/src/buffer.h b/src/graphics/buffer.h similarity index 94% rename from src/buffer.h rename to src/graphics/buffer.h index 70330e13..7447d57a 100644 --- a/src/buffer.h +++ b/src/graphics/buffer.h @@ -1,7 +1,7 @@ #include #include #include -#include "glfw.h" +#include "../glfw.h" typedef struct { GLfloat* data; diff --git a/src/graphics.c b/src/graphics/graphics.c similarity index 98% rename from src/graphics.c rename to src/graphics/graphics.c index f1dc02a8..878380a6 100644 --- a/src/graphics.c +++ b/src/graphics/graphics.c @@ -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 #include #include diff --git a/src/graphics.h b/src/graphics/graphics.h similarity index 100% rename from src/graphics.h rename to src/graphics/graphics.h diff --git a/src/model.c b/src/graphics/model.c similarity index 100% rename from src/model.c rename to src/graphics/model.c diff --git a/src/model.h b/src/graphics/model.h similarity index 100% rename from src/model.h rename to src/graphics/model.h diff --git a/src/shader.c b/src/graphics/shader.c similarity index 100% rename from src/shader.c rename to src/graphics/shader.c diff --git a/src/shader.h b/src/graphics/shader.h similarity index 94% rename from src/shader.h rename to src/graphics/shader.h index 7a3d6c8a..f07bb56c 100644 --- a/src/shader.h +++ b/src/graphics/shader.h @@ -1,7 +1,7 @@ #include #include #include -#include "glfw.h" +#include "../glfw.h" typedef struct { GLuint id; diff --git a/src/interface.c b/src/interface.c deleted file mode 100644 index 4ccb772d..00000000 --- a/src/interface.c +++ /dev/null @@ -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 } -}; diff --git a/src/interface.h b/src/interface.h deleted file mode 100644 index ee0b43f2..00000000 --- a/src/interface.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -#include -#include - -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[]; diff --git a/src/joystick.c b/src/joystick/joystick.c similarity index 98% rename from src/joystick.c rename to src/joystick/joystick.c index ee54f9d5..b12409ea 100644 --- a/src/joystick.c +++ b/src/joystick/joystick.c @@ -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*)); diff --git a/src/joystick.h b/src/joystick/joystick.h similarity index 90% rename from src/joystick.h rename to src/joystick/joystick.h index 20609f4a..a14577ab 100644 --- a/src/joystick.h +++ b/src/joystick/joystick.h @@ -1,6 +1,7 @@ #include #include #include +#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); diff --git a/src/joysticks.c b/src/joystick/joysticks.c similarity index 53% rename from src/joysticks.c rename to src/joystick/joysticks.c index 0a6b52f0..f4e43441 100644 --- a/src/joysticks.c +++ b/src/joystick/joysticks.c @@ -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 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; + } } } diff --git a/src/joysticks.h b/src/joystick/joysticks.h similarity index 100% rename from src/joysticks.h rename to src/joystick/joysticks.h diff --git a/src/lovr.c b/src/lovr.c index 26235797..4bb07143 100644 --- a/src/lovr.c +++ b/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) " diff --git a/src/timer.c b/src/timer/timer.c similarity index 88% rename from src/timer.c rename to src/timer/timer.c index 5a4603fc..aa31b57e 100644 --- a/src/timer.c +++ b/src/timer/timer.c @@ -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()); diff --git a/src/timer.h b/src/timer/timer.h similarity index 100% rename from src/timer.h rename to src/timer/timer.h