From e02d22365f67ac4f0e55c1a660d1e212309fbc31 Mon Sep 17 00:00:00 2001 From: bjorn Date: Wed, 26 Sep 2018 18:27:38 -0700 Subject: [PATCH] api: refactor; --- src/api.h | 35 ++-- src/api/audio.c | 58 +++--- src/api/data.c | 42 ++--- src/api/event.c | 36 ++-- src/api/filesystem.c | 90 ++++----- src/api/graphics.c | 258 ++++++++++++------------- src/api/headset.c | 438 +++++++++++++++++++++---------------------- src/api/lovr.c | 98 +++++----- src/api/math.c | 46 ++--- src/api/math.h | 5 + src/api/physics.c | 54 +++--- src/api/thread.c | 24 +-- src/api/timer.c | 28 +-- src/lovr.c | 15 +- 14 files changed, 603 insertions(+), 624 deletions(-) diff --git a/src/api.h b/src/api.h index b36b5349..313e1fe2 100644 --- a/src/api.h +++ b/src/api.h @@ -1,30 +1,17 @@ #include "luax.h" // Module loaders -int l_lovrInit(lua_State* L); -int l_lovrAudioInit(lua_State* L); -int l_lovrDataInit(lua_State* L); -int l_lovrEventInit(lua_State* L); -int l_lovrFilesystemInit(lua_State* L); -int l_lovrGraphicsInit(lua_State* L); -int l_lovrHeadsetInit(lua_State* L); -int l_lovrMathInit(lua_State* L); -int l_lovrPhysicsInit(lua_State* L); -int l_lovrThreadInit(lua_State* L); -int l_lovrTimerInit(lua_State* L); - -// Modules -extern const luaL_Reg lovr[]; -extern const luaL_Reg lovrAudio[]; -extern const luaL_Reg lovrData[]; -extern const luaL_Reg lovrEvent[]; -extern const luaL_Reg lovrFilesystem[]; -extern const luaL_Reg lovrGraphics[]; -extern const luaL_Reg lovrHeadset[]; -extern const luaL_Reg lovrMath[]; -extern const luaL_Reg lovrPhysics[]; -extern const luaL_Reg lovrThreadModule[]; -extern const luaL_Reg lovrTimer[]; +int luaopen_lovr(lua_State* L); +int luaopen_lovr_audio(lua_State* L); +int luaopen_lovr_data(lua_State* L); +int luaopen_lovr_event(lua_State* L); +int luaopen_lovr_filesystem(lua_State* L); +int luaopen_lovr_graphics(lua_State* L); +int luaopen_lovr_headset(lua_State* L); +int luaopen_lovr_math(lua_State* L); +int luaopen_lovr_physics(lua_State* L); +int luaopen_lovr_thread(lua_State* L); +int luaopen_lovr_timer(lua_State* L); // Objects extern const luaL_Reg lovrAnimator[]; diff --git a/src/api/audio.c b/src/api/audio.c index 386d1846..1b36aea1 100644 --- a/src/api/audio.c +++ b/src/api/audio.c @@ -15,21 +15,12 @@ const char* TimeUnits[] = { NULL }; -int l_lovrAudioInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrAudio); - luax_registertype(L, "Microphone", lovrMicrophone); - luax_registertype(L, "Source", lovrSource); - lovrAudioInit(); - return 1; -} - -int l_lovrAudioUpdate(lua_State* L) { +static int l_lovrAudioUpdate(lua_State* L) { lovrAudioUpdate(); return 0; } -int l_lovrAudioGetDopplerEffect(lua_State* L) { +static int l_lovrAudioGetDopplerEffect(lua_State* L) { float factor, speedOfSound; lovrAudioGetDopplerEffect(&factor, &speedOfSound); lua_pushnumber(L, factor); @@ -37,7 +28,7 @@ int l_lovrAudioGetDopplerEffect(lua_State* L) { return 2; } -int l_lovrAudioGetMicrophoneNames(lua_State* L) { +static int l_lovrAudioGetMicrophoneNames(lua_State* L) { const char* names[MAX_MICROPHONES]; uint8_t count; lovrAudioGetMicrophoneNames(names, &count); @@ -57,7 +48,7 @@ int l_lovrAudioGetMicrophoneNames(lua_State* L) { return 1; } -int l_lovrAudioGetOrientation(lua_State* L) { +static int l_lovrAudioGetOrientation(lua_State* L) { float angle, ax, ay, az; lovrAudioGetOrientation(&angle, &ax, &ay, &az); lua_pushnumber(L, angle); @@ -67,7 +58,7 @@ int l_lovrAudioGetOrientation(lua_State* L) { return 4; } -int l_lovrAudioGetPosition(lua_State* L) { +static int l_lovrAudioGetPosition(lua_State* L) { float x, y, z; lovrAudioGetPosition(&x, &y, &z); lua_pushnumber(L, x); @@ -76,7 +67,7 @@ int l_lovrAudioGetPosition(lua_State* L) { return 3; } -int l_lovrAudioGetVelocity(lua_State* L) { +static int l_lovrAudioGetVelocity(lua_State* L) { float x, y, z; lovrAudioGetVelocity(&x, &y, &z); lua_pushnumber(L, x); @@ -85,17 +76,17 @@ int l_lovrAudioGetVelocity(lua_State* L) { return 3; } -int l_lovrAudioGetVolume(lua_State* L) { +static int l_lovrAudioGetVolume(lua_State* L) { lua_pushnumber(L, lovrAudioGetVolume()); return 1; } -int l_lovrAudioIsSpatialized(lua_State* L) { +static int l_lovrAudioIsSpatialized(lua_State* L) { lua_pushboolean(L, lovrAudioIsSpatialized()); return 1; } -int l_lovrAudioNewMicrophone(lua_State* L) { +static int l_lovrAudioNewMicrophone(lua_State* L) { const char* name = luaL_optstring(L, 1, NULL); int samples = luaL_optinteger(L, 2, 1024); int sampleRate = luaL_optinteger(L, 3, 8000); @@ -107,7 +98,7 @@ int l_lovrAudioNewMicrophone(lua_State* L) { return 1; } -int l_lovrAudioNewSource(lua_State* L) { +static int l_lovrAudioNewSource(lua_State* L) { Source* source = NULL; SoundData* soundData = luax_totype(L, 1, SoundData); AudioStream* stream = luax_totype(L, 1, AudioStream); @@ -147,29 +138,29 @@ int l_lovrAudioNewSource(lua_State* L) { return 1; } -int l_lovrAudioPause(lua_State* L) { +static int l_lovrAudioPause(lua_State* L) { lovrAudioPause(); return 0; } -int l_lovrAudioResume(lua_State* L) { +static int l_lovrAudioResume(lua_State* L) { lovrAudioResume(); return 0; } -int l_lovrAudioRewind(lua_State* L) { +static int l_lovrAudioRewind(lua_State* L) { lovrAudioRewind(); return 0; } -int l_lovrAudioSetDopplerEffect(lua_State* L) { +static int l_lovrAudioSetDopplerEffect(lua_State* L) { float factor = luaL_optnumber(L, 1, 1.); float speedOfSound = luaL_optnumber(L, 2, 343.29); lovrAudioSetDopplerEffect(factor, speedOfSound); return 0; } -int l_lovrAudioSetOrientation(lua_State* L) { +static int l_lovrAudioSetOrientation(lua_State* L) { float angle = luaL_checknumber(L, 1); float ax = luaL_checknumber(L, 2); float ay = luaL_checknumber(L, 3); @@ -178,7 +169,7 @@ int l_lovrAudioSetOrientation(lua_State* L) { return 0; } -int l_lovrAudioSetPosition(lua_State* L) { +static int l_lovrAudioSetPosition(lua_State* L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); float z = luaL_checknumber(L, 3); @@ -186,7 +177,7 @@ int l_lovrAudioSetPosition(lua_State* L) { return 0; } -int l_lovrAudioSetVelocity(lua_State* L) { +static int l_lovrAudioSetVelocity(lua_State* L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); float z = luaL_checknumber(L, 3); @@ -194,18 +185,18 @@ int l_lovrAudioSetVelocity(lua_State* L) { return 0; } -int l_lovrAudioSetVolume(lua_State* L) { +static int l_lovrAudioSetVolume(lua_State* L) { float volume = luaL_checknumber(L, 1); lovrAudioSetVolume(volume); return 0; } -int l_lovrAudioStop(lua_State* L) { +static int l_lovrAudioStop(lua_State* L) { lovrAudioStop(); return 0; } -const luaL_Reg lovrAudio[] = { +static const luaL_Reg lovrAudio[] = { { "update", l_lovrAudioUpdate }, { "getDopplerEffect", l_lovrAudioGetDopplerEffect }, { "getMicrophoneNames", l_lovrAudioGetMicrophoneNames }, @@ -227,3 +218,12 @@ const luaL_Reg lovrAudio[] = { { "stop", l_lovrAudioStop }, { NULL, NULL } }; + +int luaopen_lovr_audio(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrAudio); + luax_registertype(L, "Microphone", lovrMicrophone); + luax_registertype(L, "Source", lovrSource); + lovrAudioInit(); + return 1; +} diff --git a/src/api/data.c b/src/api/data.c index c1633190..85448f85 100644 --- a/src/api/data.c +++ b/src/api/data.c @@ -7,20 +7,7 @@ #include "data/textureData.h" #include "data/vertexData.h" -int l_lovrDataInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrData); - luax_registertype(L, "Blob", lovrBlob); - luax_registertype(L, "AudioStream", lovrAudioStream); - luax_registertype(L, "ModelData", lovrModelData); - luax_registertype(L, "Rasterizer", lovrRasterizer); - luax_extendtype(L, "Blob", "SoundData", lovrBlob, lovrSoundData); - luax_extendtype(L, "Blob", "TextureData", lovrBlob, lovrTextureData); - luax_extendtype(L, "Blob", "VertexData", lovrBlob, lovrVertexData); - return 1; -} - -int l_lovrDataNewBlob(lua_State* L) { +static int l_lovrDataNewBlob(lua_State* L) { size_t size; uint8_t* data = NULL; int type = lua_type(L, 1); @@ -44,7 +31,7 @@ int l_lovrDataNewBlob(lua_State* L) { return 1; } -int l_lovrDataNewAudioStream(lua_State* L) { +static int l_lovrDataNewAudioStream(lua_State* L) { Blob* blob = luax_readblob(L, 1, "AudioStream"); size_t bufferSize = luaL_optinteger(L, 2, 4096); AudioStream* stream = lovrAudioStreamCreate(blob, bufferSize); @@ -54,7 +41,7 @@ int l_lovrDataNewAudioStream(lua_State* L) { return 1; } -int l_lovrDataNewModelData(lua_State* L) { +static int l_lovrDataNewModelData(lua_State* L) { Blob* blob = luax_readblob(L, 1, "Model"); ModelData* modelData = lovrModelDataCreate(blob); luax_pushobject(L, modelData); @@ -63,7 +50,7 @@ int l_lovrDataNewModelData(lua_State* L) { return 1; } -int l_lovrDataNewRasterizer(lua_State* L) { +static int l_lovrDataNewRasterizer(lua_State* L) { Blob* blob = NULL; float size; @@ -81,7 +68,7 @@ int l_lovrDataNewRasterizer(lua_State* L) { return 1; } -int l_lovrDataNewSoundData(lua_State* L) { +static int l_lovrDataNewSoundData(lua_State* L) { if (lua_type(L, 1) == LUA_TNUMBER) { int samples = luaL_checkinteger(L, 1); int sampleRate = luaL_optinteger(L, 2, 44100); @@ -109,7 +96,7 @@ int l_lovrDataNewSoundData(lua_State* L) { return 1; } -int l_lovrDataNewTextureData(lua_State* L) { +static int l_lovrDataNewTextureData(lua_State* L) { TextureData* textureData = NULL; if (lua_type(L, 1) == LUA_TNUMBER) { int width = luaL_checknumber(L, 1); @@ -127,7 +114,7 @@ int l_lovrDataNewTextureData(lua_State* L) { return 1; } -int l_lovrDataNewVertexData(lua_State* L) { +static int l_lovrDataNewVertexData(lua_State* L) { uint32_t count; int dataIndex = 0; bool hasFormat = false; @@ -163,7 +150,7 @@ int l_lovrDataNewVertexData(lua_State* L) { return 1; } -const luaL_Reg lovrData[] = { +static const luaL_Reg lovrData[] = { { "newBlob", l_lovrDataNewBlob }, { "newAudioStream", l_lovrDataNewAudioStream }, { "newModelData", l_lovrDataNewModelData }, @@ -173,3 +160,16 @@ const luaL_Reg lovrData[] = { { "newVertexData", l_lovrDataNewVertexData }, { NULL, NULL } }; + +int luaopen_lovr_data(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrData); + luax_registertype(L, "Blob", lovrBlob); + luax_registertype(L, "AudioStream", lovrAudioStream); + luax_registertype(L, "ModelData", lovrModelData); + luax_registertype(L, "Rasterizer", lovrRasterizer); + luax_extendtype(L, "Blob", "SoundData", lovrBlob, lovrSoundData); + luax_extendtype(L, "Blob", "TextureData", lovrBlob, lovrTextureData); + luax_extendtype(L, "Blob", "VertexData", lovrBlob, lovrVertexData); + return 1; +} diff --git a/src/api/event.c b/src/api/event.c index b4a15efd..13faee10 100644 --- a/src/api/event.c +++ b/src/api/event.c @@ -118,34 +118,22 @@ static int nextEvent(lua_State* L) { } } -int l_lovrEventInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrEvent); - - // Store nextEvent in the registry to avoid creating a closure every time we poll for events. - lua_pushcfunction(L, nextEvent); - pollRef = luaL_ref(L, LUA_REGISTRYINDEX); - - lovrEventInit(); - return 1; -} - -int l_lovrEventClear(lua_State* L) { +static int l_lovrEventClear(lua_State* L) { lovrEventClear(); return 0; } -int l_lovrEventPoll(lua_State* L) { +static int l_lovrEventPoll(lua_State* L) { lua_rawgeti(L, LUA_REGISTRYINDEX, pollRef); return 1; } -int l_lovrEventPump(lua_State* L) { +static int l_lovrEventPump(lua_State* L) { lovrEventPump(); return 0; } -int l_lovrEventPush(lua_State* L) { +static int l_lovrEventPush(lua_State* L) { CustomEvent eventData; const char* name = luaL_checkstring(L, 1); strncpy(eventData.name, name, MAX_EVENT_NAME_LENGTH - 1); @@ -158,7 +146,7 @@ int l_lovrEventPush(lua_State* L) { return 0; } -int l_lovrEventQuit(lua_State* L) { +static int l_lovrEventQuit(lua_State* L) { EventData data; int argType = lua_type(L, 1); @@ -178,7 +166,7 @@ int l_lovrEventQuit(lua_State* L) { return 0; } -const luaL_Reg lovrEvent[] = { +static const luaL_Reg lovrEvent[] = { { "clear", l_lovrEventClear }, { "poll", l_lovrEventPoll }, { "pump", l_lovrEventPump }, @@ -186,3 +174,15 @@ const luaL_Reg lovrEvent[] = { { "quit", l_lovrEventQuit }, { NULL, NULL } }; + +int luaopen_lovr_event(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrEvent); + + // Store nextEvent in the registry to avoid creating a closure every time we poll for events. + lua_pushcfunction(L, nextEvent); + pollRef = luaL_ref(L, LUA_REGISTRYINDEX); + + lovrEventInit(); + return 1; +} diff --git a/src/api/filesystem.c b/src/api/filesystem.c index 0271d56f..691310ce 100644 --- a/src/api/filesystem.c +++ b/src/api/filesystem.c @@ -30,7 +30,7 @@ static int pushDirectoryItem(void* userdata, const char* path, const char* filen return 1; } -int l_lovrFilesystemLoad(lua_State* L); +static int l_lovrFilesystemLoad(lua_State* L); static int moduleLoader(lua_State* L) { const char* module = luaL_gsub(L, lua_tostring(L, -1), ".", "/"); @@ -105,24 +105,7 @@ static int libraryLoader(lua_State* L) { return 0; } -int l_lovrFilesystemInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrFilesystem); - - lua_getglobal(L, "arg"); - lua_rawgeti(L, -1, -2); - lua_rawgeti(L, -2, 1); - const char* arg0 = lua_tostring(L, -2); - const char* arg1 = lua_tostring(L, -1); - lovrFilesystemInit(arg0, arg1); - lua_pop(L, 3); - - luax_registerloader(L, moduleLoader, 2); - luax_registerloader(L, libraryLoader, 3); - return 1; -} - -int l_lovrFilesystemAppend(lua_State* L) { +static int l_lovrFilesystemAppend(lua_State* L) { size_t size; const char* path = luaL_checkstring(L, 1); const char* content = luaL_checklstring(L, 2, &size); @@ -130,13 +113,13 @@ int l_lovrFilesystemAppend(lua_State* L) { return 1; } -int l_lovrFilesystemCreateDirectory(lua_State* L) { +static int l_lovrFilesystemCreateDirectory(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushboolean(L, !lovrFilesystemCreateDirectory(path)); return 1; } -int l_lovrFilesystemGetAppdataDirectory(lua_State* L) { +static int l_lovrFilesystemGetAppdataDirectory(lua_State* L) { char buffer[LOVR_PATH_MAX]; if (lovrFilesystemGetAppdataDirectory(buffer, sizeof(buffer))) { @@ -148,14 +131,14 @@ int l_lovrFilesystemGetAppdataDirectory(lua_State* L) { return 1; } -int l_lovrFilesystemGetDirectoryItems(lua_State* L) { +static int l_lovrFilesystemGetDirectoryItems(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_newtable(L); lovrFilesystemGetDirectoryItems(path, pushDirectoryItem, L); return 1; } -int l_lovrFilesystemGetExecutablePath(lua_State* L) { +static int l_lovrFilesystemGetExecutablePath(lua_State* L) { char buffer[LOVR_PATH_MAX]; if (lovrFilesystemGetExecutablePath(buffer, sizeof(buffer))) { @@ -167,7 +150,7 @@ int l_lovrFilesystemGetExecutablePath(lua_State* L) { return 1; } -int l_lovrFilesystemGetIdentity(lua_State* L) { +static int l_lovrFilesystemGetIdentity(lua_State* L) { const char* identity = lovrFilesystemGetIdentity(); if (identity) { lua_pushstring(L, identity); @@ -177,7 +160,7 @@ int l_lovrFilesystemGetIdentity(lua_State* L) { return 1; } -int l_lovrFilesystemGetLastModified(lua_State* L) { +static int l_lovrFilesystemGetLastModified(lua_State* L) { const char* path = luaL_checkstring(L, 1); int lastModified = lovrFilesystemGetLastModified(path); @@ -190,7 +173,7 @@ int l_lovrFilesystemGetLastModified(lua_State* L) { return 1; } -int l_lovrFilesystemGetRealDirectory(lua_State* L) { +static int l_lovrFilesystemGetRealDirectory(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushstring(L, lovrFilesystemGetRealDirectory(path)); return 1; @@ -206,18 +189,18 @@ static void pushRequirePath(lua_State* L, vec_str_t* path) { lua_concat(L, path->length * 2 - 1); } -int l_lovrFilesystemGetRequirePath(lua_State* L) { +static int l_lovrFilesystemGetRequirePath(lua_State* L) { pushRequirePath(L, lovrFilesystemGetRequirePath()); pushRequirePath(L, lovrFilesystemGetCRequirePath()); return 2; } -int l_lovrFilesystemGetSaveDirectory(lua_State* L) { +static int l_lovrFilesystemGetSaveDirectory(lua_State* L) { lua_pushstring(L, lovrFilesystemGetSaveDirectory()); return 1; } -int l_lovrFilesystemGetSize(lua_State* L) { +static int l_lovrFilesystemGetSize(lua_State* L) { const char* path = luaL_checkstring(L, 1); size_t size = lovrFilesystemGetSize(path); if ((int) size == -1) { @@ -227,7 +210,7 @@ int l_lovrFilesystemGetSize(lua_State* L) { return 1; } -int l_lovrFilesystemGetSource(lua_State* L) { +static int l_lovrFilesystemGetSource(lua_State* L) { const char* source = lovrFilesystemGetSource(); if (source) { @@ -239,12 +222,12 @@ int l_lovrFilesystemGetSource(lua_State* L) { return 1; } -int l_lovrFilesystemGetUserDirectory(lua_State* L) { +static int l_lovrFilesystemGetUserDirectory(lua_State* L) { lua_pushstring(L, lovrFilesystemGetUserDirectory()); return 1; } -int l_lovrFilesystemGetWorkingDirectory(lua_State* L) { +static int l_lovrFilesystemGetWorkingDirectory(lua_State* L) { char buffer[LOVR_PATH_MAX]; if (lovrFilesystemGetWorkingDirectory(buffer, sizeof(buffer))) { @@ -256,24 +239,24 @@ int l_lovrFilesystemGetWorkingDirectory(lua_State* L) { return 1; } -int l_lovrFilesystemIsDirectory(lua_State* L) { +static int l_lovrFilesystemIsDirectory(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushboolean(L, lovrFilesystemIsDirectory(path)); return 1; } -int l_lovrFilesystemIsFile(lua_State* L) { +static int l_lovrFilesystemIsFile(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushboolean(L, lovrFilesystemIsFile(path)); return 1; } -int l_lovrFilesystemIsFused(lua_State* L) { +static int l_lovrFilesystemIsFused(lua_State* L) { lua_pushboolean(L, lovrFilesystemIsFused()); return 1; } -int l_lovrFilesystemLoad(lua_State* L) { +static int l_lovrFilesystemLoad(lua_State* L) { const char* path = luaL_checkstring(L, 1); size_t size; char* content = lovrFilesystemRead(path, &size); @@ -294,7 +277,7 @@ int l_lovrFilesystemLoad(lua_State* L) { } } -int l_lovrFilesystemMount(lua_State* L) { +static int l_lovrFilesystemMount(lua_State* L) { const char* path = luaL_checkstring(L, 1); const char* mountpoint = luaL_optstring(L, 2, NULL); bool append = lua_isnoneornil(L, 3) ? 0 : lua_toboolean(L, 3); @@ -302,7 +285,7 @@ int l_lovrFilesystemMount(lua_State* L) { return 1; } -int l_lovrFilesystemNewBlob(lua_State* L) { +static int l_lovrFilesystemNewBlob(lua_State* L) { size_t size; const char* path = luaL_checkstring(L, 1); uint8_t* data = lovrFilesystemRead(path, &size); @@ -313,7 +296,7 @@ int l_lovrFilesystemNewBlob(lua_State* L) { return 1; } -int l_lovrFilesystemRead(lua_State* L) { +static int l_lovrFilesystemRead(lua_State* L) { const char* path = luaL_checkstring(L, 1); size_t size; char* content = lovrFilesystemRead(path, &size); @@ -323,13 +306,13 @@ int l_lovrFilesystemRead(lua_State* L) { return 1; } -int l_lovrFilesystemRemove(lua_State* L) { +static int l_lovrFilesystemRemove(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushboolean(L, !lovrFilesystemRemove(path)); return 1; } -int l_lovrFilesystemSetIdentity(lua_State* L) { +static int l_lovrFilesystemSetIdentity(lua_State* L) { if (lua_isnoneornil(L, 1)) { lovrFilesystemSetIdentity(NULL); } else { @@ -339,19 +322,19 @@ int l_lovrFilesystemSetIdentity(lua_State* L) { return 0; } -int l_lovrFilesystemSetRequirePath(lua_State* L) { +static int l_lovrFilesystemSetRequirePath(lua_State* L) { if (lua_type(L, 1) == LUA_TSTRING) lovrFilesystemSetRequirePath(luaL_checkstring(L, 1)); if (lua_type(L, 2) == LUA_TSTRING) lovrFilesystemSetCRequirePath(luaL_checkstring(L, 2)); return 0; } -int l_lovrFilesystemUnmount(lua_State* L) { +static int l_lovrFilesystemUnmount(lua_State* L) { const char* path = luaL_checkstring(L, 1); lua_pushboolean(L, !lovrFilesystemUnmount(path)); return 1; } -int l_lovrFilesystemWrite(lua_State* L) { +static int l_lovrFilesystemWrite(lua_State* L) { size_t size; const char* path = luaL_checkstring(L, 1); const char* content = luaL_checklstring(L, 2, &size); @@ -359,7 +342,7 @@ int l_lovrFilesystemWrite(lua_State* L) { return 1; } -const luaL_Reg lovrFilesystem[] = { +static const luaL_Reg lovrFilesystem[] = { { "append", l_lovrFilesystemAppend }, { "createDirectory", l_lovrFilesystemCreateDirectory }, { "getAppdataDirectory", l_lovrFilesystemGetAppdataDirectory }, @@ -387,3 +370,20 @@ const luaL_Reg lovrFilesystem[] = { { "write", l_lovrFilesystemWrite }, { NULL, NULL } }; + +int luaopen_lovr_filesystem(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrFilesystem); + + lua_getglobal(L, "arg"); + lua_rawgeti(L, -1, -2); + lua_rawgeti(L, -2, 1); + const char* arg0 = lua_tostring(L, -2); + const char* arg1 = lua_tostring(L, -1); + lovrFilesystemInit(arg0, arg1); + lua_pop(L, 3); + + luax_registerloader(L, moduleLoader, 2); + luax_registerloader(L, libraryLoader, 3); + return 1; +} diff --git a/src/api/graphics.c b/src/api/graphics.c index 62150c32..6398bd4f 100644 --- a/src/api/graphics.c +++ b/src/api/graphics.c @@ -250,69 +250,12 @@ static TextureData* luax_checktexturedata(lua_State* L, int index, bool flip) { // Base -int l_lovrGraphicsInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrGraphics); - luax_registertype(L, "Animator", lovrAnimator); - luax_registertype(L, "Font", lovrFont); - luax_registertype(L, "Material", lovrMaterial); - luax_registertype(L, "Mesh", lovrMesh); - luax_registertype(L, "Model", lovrModel); - luax_registertype(L, "Shader", lovrShader); - luax_registertype(L, "ShaderBlock", lovrShaderBlock); - luax_registertype(L, "Texture", lovrTexture); - luax_registertype(L, "Canvas", lovrCanvas); - - luax_pushconf(L); - - // Gamma correct - lua_getfield(L, -1, "gammacorrect"); - bool gammaCorrect = lua_toboolean(L, -1); - lua_pop(L, 1); - - lovrGraphicsInit(gammaCorrect); - - // Create window if needed - lua_getfield(L, -1, "window"); - if (!lua_isnil(L, -1)) { - lua_getfield(L, -1, "width"); - int width = luaL_checkinteger(L, -1); - lua_pop(L, 1); - - lua_getfield(L, -1, "height"); - int height = luaL_checkinteger(L, -1); - lua_pop(L, 1); - - lua_getfield(L, -1, "fullscreen"); - bool fullscreen = lua_toboolean(L, -1); - lua_pop(L, 1); - - lua_getfield(L, -1, "msaa"); - int msaa = luaL_checkinteger(L, -1); - lua_pop(L, 1); - - lua_getfield(L, -1, "title"); - const char* title = luaL_checkstring(L, -1); - lua_pop(L, 1); - - lua_getfield(L, -1, "icon"); - const char* icon = luaL_optstring(L, -1, NULL); - lua_pop(L, 1); - - lovrGraphicsCreateWindow(width, height, fullscreen, msaa, title, icon); - } - - lua_pop(L, 2); - - return 1; -} - -int l_lovrGraphicsPresent(lua_State* L) { +static int l_lovrGraphicsPresent(lua_State* L) { lovrGraphicsPresent(); return 0; } -int l_lovrGraphicsCreateWindow(lua_State* L) { +static int l_lovrGraphicsCreateWindow(lua_State* L) { int width = luaL_optnumber(L, 1, 1080); int height = luaL_optnumber(L, 2, 600); bool fullscreen = !lua_isnoneornil(L, 3) && lua_toboolean(L, 3); @@ -323,23 +266,23 @@ int l_lovrGraphicsCreateWindow(lua_State* L) { return 0; } -int l_lovrGraphicsGetWidth(lua_State* L) { +static int l_lovrGraphicsGetWidth(lua_State* L) { lua_pushnumber(L, lovrGraphicsGetWidth()); return 1; } -int l_lovrGraphicsGetHeight(lua_State* L) { +static int l_lovrGraphicsGetHeight(lua_State* L) { lua_pushnumber(L, lovrGraphicsGetHeight()); return 1; } -int l_lovrGraphicsGetDimensions(lua_State* L) { +static int l_lovrGraphicsGetDimensions(lua_State* L) { lua_pushnumber(L, lovrGraphicsGetWidth()); lua_pushnumber(L, lovrGraphicsGetHeight()); return 2; } -int l_lovrGraphicsGetSupported(lua_State* L) { +static int l_lovrGraphicsGetSupported(lua_State* L) { const GpuFeatures* features = lovrGraphicsGetSupported(); lua_newtable(L); lua_pushboolean(L, features->computeShaders); @@ -349,7 +292,7 @@ int l_lovrGraphicsGetSupported(lua_State* L) { return 1; } -int l_lovrGraphicsGetSystemLimits(lua_State* L) { +static int l_lovrGraphicsGetSystemLimits(lua_State* L) { const GpuLimits* limits = lovrGraphicsGetLimits(); lua_newtable(L); lua_pushnumber(L, limits->pointSizes[1]); @@ -363,7 +306,7 @@ int l_lovrGraphicsGetSystemLimits(lua_State* L) { return 1; } -int l_lovrGraphicsGetStats(lua_State* L) { +static int l_lovrGraphicsGetStats(lua_State* L) { if (lua_gettop(L) > 0) { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); @@ -381,12 +324,12 @@ int l_lovrGraphicsGetStats(lua_State* L) { // State -int l_lovrGraphicsReset(lua_State* L) { +static int l_lovrGraphicsReset(lua_State* L) { lovrGraphicsReset(); return 0; } -int l_lovrGraphicsGetBackgroundColor(lua_State* L) { +static int l_lovrGraphicsGetBackgroundColor(lua_State* L) { Color color = lovrGraphicsGetBackgroundColor(); lua_pushnumber(L, color.r); lua_pushnumber(L, color.g); @@ -395,7 +338,7 @@ int l_lovrGraphicsGetBackgroundColor(lua_State* L) { return 4; } -int l_lovrGraphicsSetBackgroundColor(lua_State* L) { +static int l_lovrGraphicsSetBackgroundColor(lua_State* L) { Color color; color.r = luaL_checknumber(L, 1); color.g = luaL_checknumber(L, 2); @@ -405,7 +348,7 @@ int l_lovrGraphicsSetBackgroundColor(lua_State* L) { return 0; } -int l_lovrGraphicsGetBlendMode(lua_State* L) { +static int l_lovrGraphicsGetBlendMode(lua_State* L) { BlendMode mode; BlendAlphaMode alphaMode; lovrGraphicsGetBlendMode(&mode, &alphaMode); @@ -414,26 +357,26 @@ int l_lovrGraphicsGetBlendMode(lua_State* L) { return 2; } -int l_lovrGraphicsSetBlendMode(lua_State* L) { +static int l_lovrGraphicsSetBlendMode(lua_State* L) { BlendMode mode = luaL_checkoption(L, 1, NULL, BlendModes); BlendAlphaMode alphaMode = luaL_checkoption(L, 2, "alphamultiply", BlendAlphaModes); lovrGraphicsSetBlendMode(mode, alphaMode); return 0; } -int l_lovrGraphicsGetCanvas(lua_State* L) { +static int l_lovrGraphicsGetCanvas(lua_State* L) { Canvas* canvas = lovrGraphicsGetCanvas(); luax_pushobject(L, canvas); return 1; } -int l_lovrGraphicsSetCanvas(lua_State* L) { +static int l_lovrGraphicsSetCanvas(lua_State* L) { Canvas* canvas = lua_isnoneornil(L, 1) ? NULL : luax_checktype(L, 1, Canvas); lovrGraphicsSetCanvas(canvas); return 0; } -int l_lovrGraphicsGetColor(lua_State* L) { +static int l_lovrGraphicsGetColor(lua_State* L) { Color color = lovrGraphicsGetColor(); lua_pushnumber(L, color.r); lua_pushnumber(L, color.g); @@ -442,23 +385,23 @@ int l_lovrGraphicsGetColor(lua_State* L) { return 4; } -int l_lovrGraphicsSetColor(lua_State* L) { +static int l_lovrGraphicsSetColor(lua_State* L) { Color color = luax_checkcolor(L, 1); lovrGraphicsSetColor(color); return 0; } -int l_lovrGraphicsIsCullingEnabled(lua_State* L) { +static int l_lovrGraphicsIsCullingEnabled(lua_State* L) { lua_pushboolean(L, lovrGraphicsIsCullingEnabled()); return 1; } -int l_lovrGraphicsSetCullingEnabled(lua_State* L) { +static int l_lovrGraphicsSetCullingEnabled(lua_State* L) { lovrGraphicsSetCullingEnabled(lua_toboolean(L, 1)); return 0; } -int l_lovrGraphicsGetDefaultFilter(lua_State* L) { +static int l_lovrGraphicsGetDefaultFilter(lua_State* L) { TextureFilter filter = lovrGraphicsGetDefaultFilter(); lua_pushstring(L, FilterModes[filter.mode]); if (filter.mode == FILTER_ANISOTROPIC) { @@ -468,14 +411,14 @@ int l_lovrGraphicsGetDefaultFilter(lua_State* L) { return 1; } -int l_lovrGraphicsSetDefaultFilter(lua_State* L) { +static int l_lovrGraphicsSetDefaultFilter(lua_State* L) { FilterMode mode = luaL_checkoption(L, 1, NULL, FilterModes); float anisotropy = luaL_optnumber(L, 2, 1.); lovrGraphicsSetDefaultFilter((TextureFilter) { .mode = mode, .anisotropy = anisotropy }); return 0; } -int l_lovrGraphicsGetDepthTest(lua_State* L) { +static int l_lovrGraphicsGetDepthTest(lua_State* L) { CompareMode mode; bool write; lovrGraphicsGetDepthTest(&mode, &write); @@ -484,66 +427,66 @@ int l_lovrGraphicsGetDepthTest(lua_State* L) { return 2; } -int l_lovrGraphicsSetDepthTest(lua_State* L) { +static int l_lovrGraphicsSetDepthTest(lua_State* L) { CompareMode mode = lua_isnoneornil(L, 1) ? COMPARE_NONE : luaL_checkoption(L, 1, NULL, CompareModes); bool write = lua_isnoneornil(L, 2) ? true : lua_toboolean(L, 2); lovrGraphicsSetDepthTest(mode, write); return 0; } -int l_lovrGraphicsGetFont(lua_State* L) { +static int l_lovrGraphicsGetFont(lua_State* L) { Font* font = lovrGraphicsGetFont(); luax_pushobject(L, font); return 1; } -int l_lovrGraphicsSetFont(lua_State* L) { +static int l_lovrGraphicsSetFont(lua_State* L) { Font* font = lua_isnoneornil(L, 1) ? NULL : luax_checktype(L, 1, Font); lovrGraphicsSetFont(font); return 0; } -int l_lovrGraphicsIsGammaCorrect(lua_State* L) { +static int l_lovrGraphicsIsGammaCorrect(lua_State* L) { bool gammaCorrect = lovrGraphicsIsGammaCorrect(); lua_pushboolean(L, gammaCorrect); return 1; } -int l_lovrGraphicsGetLineWidth(lua_State* L) { +static int l_lovrGraphicsGetLineWidth(lua_State* L) { lua_pushnumber(L, lovrGraphicsGetLineWidth()); return 1; } -int l_lovrGraphicsSetLineWidth(lua_State* L) { +static int l_lovrGraphicsSetLineWidth(lua_State* L) { float width = luaL_optnumber(L, 1, 1.f); lovrGraphicsSetLineWidth(width); return 0; } -int l_lovrGraphicsGetPointSize(lua_State* L) { +static int l_lovrGraphicsGetPointSize(lua_State* L) { lua_pushnumber(L, lovrGraphicsGetPointSize()); return 1; } -int l_lovrGraphicsSetPointSize(lua_State* L) { +static int l_lovrGraphicsSetPointSize(lua_State* L) { float size = luaL_optnumber(L, 1, 1.f); lovrGraphicsSetPointSize(size); return 0; } -int l_lovrGraphicsGetShader(lua_State* L) { +static int l_lovrGraphicsGetShader(lua_State* L) { Shader* shader = lovrGraphicsGetShader(); luax_pushobject(L, shader); return 1; } -int l_lovrGraphicsSetShader(lua_State* L) { +static int l_lovrGraphicsSetShader(lua_State* L) { Shader* shader = lua_isnoneornil(L, 1) ? NULL : luax_checktype(L, 1, Shader); lovrGraphicsSetShader(shader); return 0; } -int l_lovrGraphicsGetStencilTest(lua_State* L) { +static int l_lovrGraphicsGetStencilTest(lua_State* L) { CompareMode mode; int value; lovrGraphicsGetStencilTest(&mode, &value); @@ -558,7 +501,7 @@ int l_lovrGraphicsGetStencilTest(lua_State* L) { return 2; } -int l_lovrGraphicsSetStencilTest(lua_State* L) { +static int l_lovrGraphicsSetStencilTest(lua_State* L) { if (lua_isnoneornil(L, 1)) { lovrGraphicsSetStencilTest(COMPARE_NONE, 0); } else { @@ -569,44 +512,44 @@ int l_lovrGraphicsSetStencilTest(lua_State* L) { return 0; } -int l_lovrGraphicsGetWinding(lua_State* L) { +static int l_lovrGraphicsGetWinding(lua_State* L) { lua_pushstring(L, Windings[lovrGraphicsGetWinding()]); return 1; } -int l_lovrGraphicsSetWinding(lua_State* L) { +static int l_lovrGraphicsSetWinding(lua_State* L) { lovrGraphicsSetWinding(luaL_checkoption(L, 1, NULL, Windings)); return 0; } -int l_lovrGraphicsIsWireframe(lua_State* L) { +static int l_lovrGraphicsIsWireframe(lua_State* L) { lua_pushboolean(L, lovrGraphicsIsWireframe()); return 1; } -int l_lovrGraphicsSetWireframe(lua_State* L) { +static int l_lovrGraphicsSetWireframe(lua_State* L) { lovrGraphicsSetWireframe(lua_toboolean(L, 1)); return 0; } // Transforms -int l_lovrGraphicsPush(lua_State* L) { +static int l_lovrGraphicsPush(lua_State* L) { lovrGraphicsPush(); return 0; } -int l_lovrGraphicsPop(lua_State* L) { +static int l_lovrGraphicsPop(lua_State* L) { lovrGraphicsPop(); return 0; } -int l_lovrGraphicsOrigin(lua_State* L) { +static int l_lovrGraphicsOrigin(lua_State* L) { lovrGraphicsOrigin(); return 0; } -int l_lovrGraphicsTranslate(lua_State* L) { +static int l_lovrGraphicsTranslate(lua_State* L) { float x = luaL_checknumber(L, 1); float y = luaL_checknumber(L, 2); float z = luaL_checknumber(L, 3); @@ -614,7 +557,7 @@ int l_lovrGraphicsTranslate(lua_State* L) { return 0; } -int l_lovrGraphicsRotate(lua_State* L) { +static int l_lovrGraphicsRotate(lua_State* L) { float angle = luaL_checknumber(L, 1); float axisX = luaL_optnumber(L, 2, 0); float axisY = luaL_optnumber(L, 3, 1); @@ -623,7 +566,7 @@ int l_lovrGraphicsRotate(lua_State* L) { return 0; } -int l_lovrGraphicsScale(lua_State* L) { +static int l_lovrGraphicsScale(lua_State* L) { float x = luaL_checknumber(L, 1); float y = luaL_optnumber(L, 2, x); float z = luaL_optnumber(L, 3, x); @@ -631,7 +574,7 @@ int l_lovrGraphicsScale(lua_State* L) { return 0; } -int l_lovrGraphicsTransform(lua_State* L) { +static int l_lovrGraphicsTransform(lua_State* L) { float transform[16]; luax_readtransform(L, 1, transform, 3); lovrGraphicsMatrixTransform(transform); @@ -640,7 +583,7 @@ int l_lovrGraphicsTransform(lua_State* L) { // Rendering -int l_lovrGraphicsClear(lua_State* L) { +static int l_lovrGraphicsClear(lua_State* L) { int index = 1; int top = lua_gettop(L); @@ -682,19 +625,19 @@ int l_lovrGraphicsClear(lua_State* L) { return 0; } -int l_lovrGraphicsPoints(lua_State* L) { +static int l_lovrGraphicsPoints(lua_State* L) { uint32_t count = luax_readvertices(L, 1); lovrGraphicsPoints(count); return 0; } -int l_lovrGraphicsLine(lua_State* L) { +static int l_lovrGraphicsLine(lua_State* L) { uint32_t count = luax_readvertices(L, 1); lovrGraphicsLine(count); return 0; } -int l_lovrGraphicsTriangle(lua_State* L) { +static int l_lovrGraphicsTriangle(lua_State* L) { DrawMode drawMode = DRAW_MODE_FILL; Material* material = NULL; if (lua_isuserdata(L, 1)) { @@ -713,7 +656,7 @@ int l_lovrGraphicsTriangle(lua_State* L) { return 0; } -int l_lovrGraphicsPlane(lua_State* L) { +static int l_lovrGraphicsPlane(lua_State* L) { DrawMode drawMode = DRAW_MODE_FILL; Material* material = NULL; if (lua_isuserdata(L, 1)) { @@ -741,15 +684,15 @@ static int luax_rectangularprism(lua_State* L, int scaleComponents) { return 0; } -int l_lovrGraphicsCube(lua_State* L) { +static int l_lovrGraphicsCube(lua_State* L) { return luax_rectangularprism(L, 1); } -int l_lovrGraphicsBox(lua_State* L) { +static int l_lovrGraphicsBox(lua_State* L) { return luax_rectangularprism(L, 3); } -int l_lovrGraphicsArc(lua_State* L) { +static int l_lovrGraphicsArc(lua_State* L) { DrawMode drawMode = DRAW_MODE_FILL; Material* material = NULL; if (lua_isuserdata(L, 1)) { @@ -771,7 +714,7 @@ int l_lovrGraphicsArc(lua_State* L) { return 0; } -int l_lovrGraphicsCircle(lua_State* L) { +static int l_lovrGraphicsCircle(lua_State* L) { DrawMode drawMode = DRAW_MODE_FILL; Material* material = NULL; if (lua_isuserdata(L, 1)) { @@ -786,7 +729,7 @@ int l_lovrGraphicsCircle(lua_State* L) { return 0; } -int l_lovrGraphicsCylinder(lua_State* L) { +static int l_lovrGraphicsCylinder(lua_State* L) { int index = 1; Material* material = lua_isuserdata(L, index) ? luax_checktype(L, index++, Material) : NULL; float x1 = luaL_checknumber(L, index++); @@ -803,7 +746,7 @@ int l_lovrGraphicsCylinder(lua_State* L) { return 0; } -int l_lovrGraphicsSphere(lua_State* L) { +static int l_lovrGraphicsSphere(lua_State* L) { float transform[16]; int index = 1; Material* material = lua_isuserdata(L, index) ? luax_checktype(L, index++, Material) : NULL; @@ -813,7 +756,7 @@ int l_lovrGraphicsSphere(lua_State* L) { return 0; } -int l_lovrGraphicsSkybox(lua_State* L) { +static int l_lovrGraphicsSkybox(lua_State* L) { Texture* texture = luax_checktexture(L, 1); float angle = luaL_optnumber(L, 2, 0); float ax = luaL_optnumber(L, 3, 0); @@ -823,7 +766,7 @@ int l_lovrGraphicsSkybox(lua_State* L) { return 0; } -int l_lovrGraphicsPrint(lua_State* L) { +static int l_lovrGraphicsPrint(lua_State* L) { const char* str = luaL_checkstring(L, 1); float transform[16]; int index = luax_readtransform(L, 2, transform, 1); @@ -834,7 +777,7 @@ int l_lovrGraphicsPrint(lua_State* L) { return 0; } -int l_lovrGraphicsStencil(lua_State* L) { +static int l_lovrGraphicsStencil(lua_State* L) { luaL_checktype(L, 1, LUA_TFUNCTION); StencilAction action = luaL_checkoption(L, 2, "replace", StencilActions); int replaceValue = luaL_optinteger(L, 3, 1); @@ -848,13 +791,13 @@ int l_lovrGraphicsStencil(lua_State* L) { return 0; } -int l_lovrGraphicsFill(lua_State* L) { +static int l_lovrGraphicsFill(lua_State* L) { Texture* texture = lua_isnoneornil(L, 1) ? NULL : luax_checktexture(L, 1); lovrGraphicsFill(texture); return 0; } -int l_lovrGraphicsCompute(lua_State* L) { +static int l_lovrGraphicsCompute(lua_State* L) { Shader* shader = luax_checktype(L, 1, Shader); int x = luaL_optinteger(L, 2, 1); int y = luaL_optinteger(L, 3, 1); @@ -865,7 +808,7 @@ int l_lovrGraphicsCompute(lua_State* L) { // Types -int l_lovrGraphicsNewAnimator(lua_State* L) { +static int l_lovrGraphicsNewAnimator(lua_State* L) { Model* model = luax_checktype(L, 1, Model); Animator* animator = lovrAnimatorCreate(model->modelData); luax_pushobject(L, animator); @@ -873,7 +816,7 @@ int l_lovrGraphicsNewAnimator(lua_State* L) { return 1; } -int l_lovrGraphicsNewShaderBlock(lua_State* L) { +static int l_lovrGraphicsNewShaderBlock(lua_State* L) { vec_uniform_t uniforms; vec_init(&uniforms); @@ -926,7 +869,7 @@ int l_lovrGraphicsNewShaderBlock(lua_State* L) { return 1; } -int l_lovrGraphicsNewCanvas(lua_State* L) { +static int l_lovrGraphicsNewCanvas(lua_State* L) { Attachment attachments[MAX_CANVAS_ATTACHMENTS]; int attachmentCount = 0; int width = 0; @@ -1024,7 +967,7 @@ int l_lovrGraphicsNewCanvas(lua_State* L) { return 1; } -int l_lovrGraphicsNewFont(lua_State* L) { +static int l_lovrGraphicsNewFont(lua_State* L) { Rasterizer* rasterizer = luax_totype(L, 1, Rasterizer); if (!rasterizer) { @@ -1049,7 +992,7 @@ int l_lovrGraphicsNewFont(lua_State* L) { return 1; } -int l_lovrGraphicsNewMaterial(lua_State* L) { +static int l_lovrGraphicsNewMaterial(lua_State* L) { Material* material = lovrMaterialCreate(); int index = 1; @@ -1078,7 +1021,7 @@ int l_lovrGraphicsNewMaterial(lua_State* L) { return 1; } -int l_lovrGraphicsNewMesh(lua_State* L) { +static int l_lovrGraphicsNewMesh(lua_State* L) { uint32_t count; int dataIndex = 0; int drawModeIndex = 2; @@ -1137,7 +1080,7 @@ int l_lovrGraphicsNewMesh(lua_State* L) { return 1; } -int l_lovrGraphicsNewModel(lua_State* L) { +static int l_lovrGraphicsNewModel(lua_State* L) { ModelData* modelData = luax_totype(L, 1, ModelData); if (!modelData) { @@ -1195,7 +1138,7 @@ static void luax_readshadersource(lua_State* L, int index) { free(contents); } -int l_lovrGraphicsNewShader(lua_State* L) { +static int l_lovrGraphicsNewShader(lua_State* L) { luax_readshadersource(L, 1); luax_readshadersource(L, 2); const char* vertexSource = lua_tostring(L, 1); @@ -1206,7 +1149,7 @@ int l_lovrGraphicsNewShader(lua_State* L) { return 1; } -int l_lovrGraphicsNewComputeShader(lua_State* L) { +static int l_lovrGraphicsNewComputeShader(lua_State* L) { luax_readshadersource(L, 1); const char* source = lua_tostring(L, 1); Shader* shader = lovrShaderCreateCompute(source); @@ -1215,7 +1158,7 @@ int l_lovrGraphicsNewComputeShader(lua_State* L) { return 1; } -int l_lovrGraphicsNewTexture(lua_State* L) { +static int l_lovrGraphicsNewTexture(lua_State* L) { int index = 1; int width, height, depth; int argType = lua_type(L, index); @@ -1300,7 +1243,7 @@ int l_lovrGraphicsNewTexture(lua_State* L) { return 1; } -const luaL_Reg lovrGraphics[] = { +static const luaL_Reg lovrGraphics[] = { // Base { "present", l_lovrGraphicsPresent }, @@ -1385,3 +1328,60 @@ const luaL_Reg lovrGraphics[] = { { NULL, NULL } }; + +int luaopen_lovr_graphics(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrGraphics); + luax_registertype(L, "Animator", lovrAnimator); + luax_registertype(L, "Font", lovrFont); + luax_registertype(L, "Material", lovrMaterial); + luax_registertype(L, "Mesh", lovrMesh); + luax_registertype(L, "Model", lovrModel); + luax_registertype(L, "Shader", lovrShader); + luax_registertype(L, "ShaderBlock", lovrShaderBlock); + luax_registertype(L, "Texture", lovrTexture); + luax_registertype(L, "Canvas", lovrCanvas); + + luax_pushconf(L); + + // Gamma correct + lua_getfield(L, -1, "gammacorrect"); + bool gammaCorrect = lua_toboolean(L, -1); + lua_pop(L, 1); + + lovrGraphicsInit(gammaCorrect); + + // Create window if needed + lua_getfield(L, -1, "window"); + if (!lua_isnil(L, -1)) { + lua_getfield(L, -1, "width"); + int width = luaL_checkinteger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "height"); + int height = luaL_checkinteger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "fullscreen"); + bool fullscreen = lua_toboolean(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "msaa"); + int msaa = luaL_checkinteger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "title"); + const char* title = luaL_checkstring(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "icon"); + const char* icon = luaL_optstring(L, -1, NULL); + lua_pop(L, 1); + + lovrGraphicsCreateWindow(width, height, fullscreen, msaa, title, icon); + } + + lua_pop(L, 2); + + return 1; +} diff --git a/src/api/headset.c b/src/api/headset.c index ef960444..3f51fcb2 100644 --- a/src/api/headset.c +++ b/src/api/headset.c @@ -75,7 +75,225 @@ static void renderHelper(void* userdata) { lua_call(L, 0, 0); } -int l_lovrHeadsetInit(lua_State* L) { +static int l_lovrHeadsetGetDriver(lua_State* L) { + lua_pushstring(L, HeadsetDrivers[lovrHeadsetDriver->driverType]); + return 1; +} + +static int l_lovrHeadsetGetType(lua_State* L) { + lua_pushstring(L, HeadsetTypes[lovrHeadsetDriver->getType()]); + return 1; +} + +static int l_lovrHeadsetGetOriginType(lua_State* L) { + lua_pushstring(L, HeadsetOrigins[lovrHeadsetDriver->getOriginType()]); + return 1; +} + +static int l_lovrHeadsetIsMounted(lua_State* L) { + lua_pushboolean(L, lovrHeadsetDriver->isMounted()); + return 1; +} + +static int l_lovrHeadsetIsMirrored(lua_State* L) { + lua_pushboolean(L, lovrHeadsetDriver->isMirrored()); + return 1; +} + +static int l_lovrHeadsetSetMirrored(lua_State* L) { + int mirror = lua_toboolean(L, 1); + lovrHeadsetDriver->setMirrored(mirror); + return 0; +} + +static int l_lovrHeadsetGetDisplayWidth(lua_State* L) { + int width, height; + lovrHeadsetDriver->getDisplayDimensions(&width, &height); + lua_pushnumber(L, width); + return 1; +} + +static int l_lovrHeadsetGetDisplayHeight(lua_State* L) { + int width, height; + lovrHeadsetDriver->getDisplayDimensions(&width, &height); + lua_pushnumber(L, height); + return 1; +} + +static int l_lovrHeadsetGetDisplayDimensions(lua_State* L) { + int width, height; + lovrHeadsetDriver->getDisplayDimensions(&width, &height); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 2; +} + +static int l_lovrHeadsetGetClipDistance(lua_State* L) { + float clipNear, clipFar; + lovrHeadsetDriver->getClipDistance(&clipNear, &clipFar); + lua_pushnumber(L, clipNear); + lua_pushnumber(L, clipFar); + return 2; +} + +static int l_lovrHeadsetSetClipDistance(lua_State* L) { + float clipNear = luaL_checknumber(L, 1); + float clipFar = luaL_checknumber(L, 2); + lovrHeadsetDriver->setClipDistance(clipNear, clipFar); + return 0; +} + +static int l_lovrHeadsetGetBoundsWidth(lua_State* L) { + float width, depth; + lovrHeadsetDriver->getBoundsDimensions(&width, &depth); + lua_pushnumber(L, width); + return 1; +} + +static int l_lovrHeadsetGetBoundsDepth(lua_State* L) { + float width, depth; + lovrHeadsetDriver->getBoundsDimensions(&width, &depth); + lua_pushnumber(L, depth); + return 1; +} + +static int l_lovrHeadsetGetBoundsDimensions(lua_State* L) { + float width, depth; + lovrHeadsetDriver->getBoundsDimensions(&width, &depth); + lua_pushnumber(L, width); + lua_pushnumber(L, depth); + return 2; +} + +static void luax_getPose(lua_State* L, float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az) { + if (lua_type(L, 1) == LUA_TSTRING) { + HeadsetEye eye = luaL_checkoption(L, 1, NULL, HeadsetEyes); + lovrHeadsetDriver->getEyePose(eye, x, y, z, angle, ax, ay, az); + } else { + lovrHeadsetDriver->getPose(x, y, z, angle, ax, ay, az); + } +} + +static int l_lovrHeadsetGetPose(lua_State* L) { + float x, y, z, angle, ax, ay, az; + luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + lua_pushnumber(L, angle); + lua_pushnumber(L, ax); + lua_pushnumber(L, ay); + lua_pushnumber(L, az); + return 7; +} + +static int l_lovrHeadsetGetPosition(lua_State* L) { + float x, y, z, angle, ax, ay, az; + luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + return 3; +} + +static int l_lovrHeadsetGetOrientation(lua_State* L) { + float x, y, z, angle, ax, ay, az; + luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); + lua_pushnumber(L, angle); + lua_pushnumber(L, ax); + lua_pushnumber(L, ay); + lua_pushnumber(L, az); + return 4; +} + +static int l_lovrHeadsetGetVelocity(lua_State* L) { + float x, y, z; + lovrHeadsetDriver->getVelocity(&x, &y, &z); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + return 3; +} + +static int l_lovrHeadsetGetAngularVelocity(lua_State* L) { + float x, y, z; + lovrHeadsetDriver->getAngularVelocity(&x, &y, &z); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + return 3; +} + +static int l_lovrHeadsetGetControllers(lua_State* L) { + uint8_t count; + Controller** controllers = lovrHeadsetDriver->getControllers(&count); + lua_newtable(L); + for (uint8_t i = 0; i < count; i++) { + luax_pushobject(L, controllers[i]); + lua_rawseti(L, -2, i + 1); + } + return 1; +} + +static int l_lovrHeadsetGetControllerCount(lua_State* L) { + uint8_t count; + lovrHeadsetDriver->getControllers(&count); + lua_pushnumber(L, count); + return 1; +} + +static int l_lovrHeadsetRenderTo(lua_State* L) { + lua_settop(L, 1); + luaL_checktype(L, 1, LUA_TFUNCTION); + +#ifdef EMSCRIPTEN + if (headsetRenderData.ref != LUA_NOREF) { + luaL_unref(L, LUA_REGISTRYINDEX, headsetRenderData.ref); + } + + headsetRenderData.ref = luaL_ref(L, LUA_REGISTRYINDEX); +#endif + headsetRenderData.L = L; + lovrHeadsetDriver->renderTo(renderHelper, &headsetRenderData); + return 0; +} + +static int l_lovrHeadsetUpdate(lua_State* L) { + if (lovrHeadsetDriver->update) { + lovrHeadsetDriver->update(luaL_checknumber(L, 1)); + } + + return 0; +} + +static const luaL_Reg lovrHeadset[] = { + { "getDriver", l_lovrHeadsetGetDriver }, + { "getType", l_lovrHeadsetGetType }, + { "getOriginType", l_lovrHeadsetGetOriginType }, + { "isMounted", l_lovrHeadsetIsMounted }, + { "isMirrored", l_lovrHeadsetIsMirrored }, + { "setMirrored", l_lovrHeadsetSetMirrored }, + { "getDisplayWidth", l_lovrHeadsetGetDisplayWidth }, + { "getDisplayHeight", l_lovrHeadsetGetDisplayHeight }, + { "getDisplayDimensions", l_lovrHeadsetGetDisplayDimensions }, + { "getClipDistance", l_lovrHeadsetGetClipDistance }, + { "setClipDistance", l_lovrHeadsetSetClipDistance }, + { "getBoundsWidth", l_lovrHeadsetGetBoundsWidth }, + { "getBoundsDepth", l_lovrHeadsetGetBoundsDepth }, + { "getBoundsDimensions", l_lovrHeadsetGetBoundsDimensions }, + { "getPose", l_lovrHeadsetGetPose }, + { "getPosition", l_lovrHeadsetGetPosition }, + { "getOrientation", l_lovrHeadsetGetOrientation }, + { "getVelocity", l_lovrHeadsetGetVelocity }, + { "getAngularVelocity", l_lovrHeadsetGetAngularVelocity }, + { "getControllers", l_lovrHeadsetGetControllers }, + { "getControllerCount", l_lovrHeadsetGetControllerCount }, + { "renderTo", l_lovrHeadsetRenderTo }, + { "update", l_lovrHeadsetUpdate }, + { NULL, NULL } +}; + +int luaopen_lovr_headset(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrHeadset); luax_registertype(L, "Controller", lovrController); @@ -127,221 +345,3 @@ int l_lovrHeadsetInit(lua_State* L) { return 1; } - -int l_lovrHeadsetGetDriver(lua_State* L) { - lua_pushstring(L, HeadsetDrivers[lovrHeadsetDriver->driverType]); - return 1; -} - -int l_lovrHeadsetGetType(lua_State* L) { - lua_pushstring(L, HeadsetTypes[lovrHeadsetDriver->getType()]); - return 1; -} - -int l_lovrHeadsetGetOriginType(lua_State* L) { - lua_pushstring(L, HeadsetOrigins[lovrHeadsetDriver->getOriginType()]); - return 1; -} - -int l_lovrHeadsetIsMounted(lua_State* L) { - lua_pushboolean(L, lovrHeadsetDriver->isMounted()); - return 1; -} - -int l_lovrHeadsetIsMirrored(lua_State* L) { - lua_pushboolean(L, lovrHeadsetDriver->isMirrored()); - return 1; -} - -int l_lovrHeadsetSetMirrored(lua_State* L) { - int mirror = lua_toboolean(L, 1); - lovrHeadsetDriver->setMirrored(mirror); - return 0; -} - -int l_lovrHeadsetGetDisplayWidth(lua_State* L) { - int width, height; - lovrHeadsetDriver->getDisplayDimensions(&width, &height); - lua_pushnumber(L, width); - return 1; -} - -int l_lovrHeadsetGetDisplayHeight(lua_State* L) { - int width, height; - lovrHeadsetDriver->getDisplayDimensions(&width, &height); - lua_pushnumber(L, height); - return 1; -} - -int l_lovrHeadsetGetDisplayDimensions(lua_State* L) { - int width, height; - lovrHeadsetDriver->getDisplayDimensions(&width, &height); - lua_pushnumber(L, width); - lua_pushnumber(L, height); - return 2; -} - -int l_lovrHeadsetGetClipDistance(lua_State* L) { - float clipNear, clipFar; - lovrHeadsetDriver->getClipDistance(&clipNear, &clipFar); - lua_pushnumber(L, clipNear); - lua_pushnumber(L, clipFar); - return 2; -} - -int l_lovrHeadsetSetClipDistance(lua_State* L) { - float clipNear = luaL_checknumber(L, 1); - float clipFar = luaL_checknumber(L, 2); - lovrHeadsetDriver->setClipDistance(clipNear, clipFar); - return 0; -} - -int l_lovrHeadsetGetBoundsWidth(lua_State* L) { - float width, depth; - lovrHeadsetDriver->getBoundsDimensions(&width, &depth); - lua_pushnumber(L, width); - return 1; -} - -int l_lovrHeadsetGetBoundsDepth(lua_State* L) { - float width, depth; - lovrHeadsetDriver->getBoundsDimensions(&width, &depth); - lua_pushnumber(L, depth); - return 1; -} - -int l_lovrHeadsetGetBoundsDimensions(lua_State* L) { - float width, depth; - lovrHeadsetDriver->getBoundsDimensions(&width, &depth); - lua_pushnumber(L, width); - lua_pushnumber(L, depth); - return 2; -} - -static void luax_getPose(lua_State* L, float* x, float* y, float* z, float* angle, float* ax, float* ay, float* az) { - if (lua_type(L, 1) == LUA_TSTRING) { - HeadsetEye eye = luaL_checkoption(L, 1, NULL, HeadsetEyes); - lovrHeadsetDriver->getEyePose(eye, x, y, z, angle, ax, ay, az); - } else { - lovrHeadsetDriver->getPose(x, y, z, angle, ax, ay, az); - } -} - -int l_lovrHeadsetGetPose(lua_State* L) { - float x, y, z, angle, ax, ay, az; - luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); - lua_pushnumber(L, angle); - lua_pushnumber(L, ax); - lua_pushnumber(L, ay); - lua_pushnumber(L, az); - return 7; -} - -int l_lovrHeadsetGetPosition(lua_State* L) { - float x, y, z, angle, ax, ay, az; - luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); - return 3; -} - -int l_lovrHeadsetGetOrientation(lua_State* L) { - float x, y, z, angle, ax, ay, az; - luax_getPose(L, &x, &y, &z, &angle, &ax, &ay, &az); - lua_pushnumber(L, angle); - lua_pushnumber(L, ax); - lua_pushnumber(L, ay); - lua_pushnumber(L, az); - return 4; -} - -int l_lovrHeadsetGetVelocity(lua_State* L) { - float x, y, z; - lovrHeadsetDriver->getVelocity(&x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); - return 3; -} - -int l_lovrHeadsetGetAngularVelocity(lua_State* L) { - float x, y, z; - lovrHeadsetDriver->getAngularVelocity(&x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); - return 3; -} - -int l_lovrHeadsetGetControllers(lua_State* L) { - uint8_t count; - Controller** controllers = lovrHeadsetDriver->getControllers(&count); - lua_newtable(L); - for (uint8_t i = 0; i < count; i++) { - luax_pushobject(L, controllers[i]); - lua_rawseti(L, -2, i + 1); - } - return 1; -} - -int l_lovrHeadsetGetControllerCount(lua_State* L) { - uint8_t count; - lovrHeadsetDriver->getControllers(&count); - lua_pushnumber(L, count); - return 1; -} - -int l_lovrHeadsetRenderTo(lua_State* L) { - lua_settop(L, 1); - luaL_checktype(L, 1, LUA_TFUNCTION); - -#ifdef EMSCRIPTEN - if (headsetRenderData.ref != LUA_NOREF) { - luaL_unref(L, LUA_REGISTRYINDEX, headsetRenderData.ref); - } - - headsetRenderData.ref = luaL_ref(L, LUA_REGISTRYINDEX); -#endif - headsetRenderData.L = L; - lovrHeadsetDriver->renderTo(renderHelper, &headsetRenderData); - return 0; -} - -int l_lovrHeadsetUpdate(lua_State* L) { - if (lovrHeadsetDriver->update) { - lovrHeadsetDriver->update(luaL_checknumber(L, 1)); - } - - return 0; -} - -const luaL_Reg lovrHeadset[] = { - { "getDriver", l_lovrHeadsetGetDriver }, - { "getType", l_lovrHeadsetGetType }, - { "getOriginType", l_lovrHeadsetGetOriginType }, - { "isMounted", l_lovrHeadsetIsMounted }, - { "isMirrored", l_lovrHeadsetIsMirrored }, - { "setMirrored", l_lovrHeadsetSetMirrored }, - { "getDisplayWidth", l_lovrHeadsetGetDisplayWidth }, - { "getDisplayHeight", l_lovrHeadsetGetDisplayHeight }, - { "getDisplayDimensions", l_lovrHeadsetGetDisplayDimensions }, - { "getClipDistance", l_lovrHeadsetGetClipDistance }, - { "setClipDistance", l_lovrHeadsetSetClipDistance }, - { "getBoundsWidth", l_lovrHeadsetGetBoundsWidth }, - { "getBoundsDepth", l_lovrHeadsetGetBoundsDepth }, - { "getBoundsDimensions", l_lovrHeadsetGetBoundsDimensions }, - { "getPose", l_lovrHeadsetGetPose }, - { "getPosition", l_lovrHeadsetGetPosition }, - { "getOrientation", l_lovrHeadsetGetOrientation }, - { "getVelocity", l_lovrHeadsetGetVelocity }, - { "getAngularVelocity", l_lovrHeadsetGetAngularVelocity }, - { "getControllers", l_lovrHeadsetGetControllers }, - { "getControllerCount", l_lovrHeadsetGetControllerCount }, - { "renderTo", l_lovrHeadsetRenderTo }, - { "update", l_lovrHeadsetUpdate }, - { NULL, NULL } -}; diff --git a/src/api/lovr.c b/src/api/lovr.c index 976c30f5..e2751290 100644 --- a/src/api/lovr.c +++ b/src/api/lovr.c @@ -4,53 +4,7 @@ #include "lib/lua-cjson/lua_cjson.h" #include "lib/lua-enet/enet.h" -int l_lovrInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovr); - - lua_pushlstring(L, (const char*) logo_png, logo_png_len); - lua_setfield(L, -2, "_logo"); - -#ifdef LOVR_ENABLE_AUDIO - luax_preloadmodule(L, "lovr.audio", l_lovrAudioInit); -#endif -#ifdef LOVR_ENABLE_DATA - luax_preloadmodule(L, "lovr.data", l_lovrDataInit); -#endif -#ifdef LOVR_ENABLE_EVENT - luax_preloadmodule(L, "lovr.event", l_lovrEventInit); -#endif -#ifdef LOVR_ENABLE_FILESYSTEM - luax_preloadmodule(L, "lovr.filesystem", l_lovrFilesystemInit); -#endif -#ifdef LOVR_ENABLE_GRAPHICS - luax_preloadmodule(L, "lovr.graphics", l_lovrGraphicsInit); -#endif -#ifdef LOVR_ENABLE_HEADSET - luax_preloadmodule(L, "lovr.headset", l_lovrHeadsetInit); -#endif -#ifdef LOVR_ENABLE_MATH - luax_preloadmodule(L, "lovr.math", l_lovrMathInit); -#endif -#ifdef LOVR_ENABLE_PHYSICS - luax_preloadmodule(L, "lovr.physics", l_lovrPhysicsInit); -#endif -#ifdef LOVR_ENABLE_THREAD - luax_preloadmodule(L, "lovr.thread", l_lovrThreadInit); -#endif -#ifdef LOVR_ENABLE_TIMER - luax_preloadmodule(L, "lovr.timer", l_lovrTimerInit); -#endif -#ifdef LOVR_ENABLE_ENET - luax_preloadmodule(L, "enet", luaopen_enet); -#endif -#ifdef LOVR_ENABLE_JSON - luax_preloadmodule(L, "json", luaopen_cjson); -#endif - return 1; -} - -int l_lovrGetOS(lua_State* L) { +static int l_lovrGetOS(lua_State* L) { const char* os = lovrGetOS(); if (os) { lua_pushstring(L, os); @@ -60,7 +14,7 @@ int l_lovrGetOS(lua_State* L) { return 1; } -int l_lovrGetVersion(lua_State* L) { +static int l_lovrGetVersion(lua_State* L) { int major, minor, patch; lovrGetVersion(&major, &minor, &patch); lua_pushinteger(L, major); @@ -69,9 +23,55 @@ int l_lovrGetVersion(lua_State* L) { return 3; } -const luaL_Reg lovr[] = { +static const luaL_Reg lovr[] = { { "_setConf", luax_setconf }, { "getOS", l_lovrGetOS }, { "getVersion", l_lovrGetVersion }, { NULL, NULL } }; + +int luaopen_lovr(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovr); + + lua_pushlstring(L, (const char*) logo_png, logo_png_len); + lua_setfield(L, -2, "_logo"); + +#ifdef LOVR_ENABLE_AUDIO + luax_preloadmodule(L, "lovr.audio", luaopen_lovr_audio); +#endif +#ifdef LOVR_ENABLE_DATA + luax_preloadmodule(L, "lovr.data", luaopen_lovr_data); +#endif +#ifdef LOVR_ENABLE_EVENT + luax_preloadmodule(L, "lovr.event", luaopen_lovr_event); +#endif +#ifdef LOVR_ENABLE_FILESYSTEM + luax_preloadmodule(L, "lovr.filesystem", luaopen_lovr_filesystem); +#endif +#ifdef LOVR_ENABLE_GRAPHICS + luax_preloadmodule(L, "lovr.graphics", luaopen_lovr_graphics); +#endif +#ifdef LOVR_ENABLE_HEADSET + luax_preloadmodule(L, "lovr.headset", luaopen_lovr_headset); +#endif +#ifdef LOVR_ENABLE_MATH + luax_preloadmodule(L, "lovr.math", luaopen_lovr_math); +#endif +#ifdef LOVR_ENABLE_PHYSICS + luax_preloadmodule(L, "lovr.physics", luaopen_lovr_physics); +#endif +#ifdef LOVR_ENABLE_THREAD + luax_preloadmodule(L, "lovr.thread", luaopen_lovr_thread); +#endif +#ifdef LOVR_ENABLE_TIMER + luax_preloadmodule(L, "lovr.timer", luaopen_lovr_timer); +#endif +#ifdef LOVR_ENABLE_ENET + luax_preloadmodule(L, "enet", luaopen_enet); +#endif +#ifdef LOVR_ENABLE_JSON + luax_preloadmodule(L, "json", luaopen_cjson); +#endif + return 1; +} diff --git a/src/api/math.c b/src/api/math.c index 76bb55b3..5a9eee10 100644 --- a/src/api/math.c +++ b/src/api/math.c @@ -6,21 +6,7 @@ #include "math/randomGenerator.h" #include "math/transform.h" -extern int l_lovrRandomGeneratorRandom(lua_State* L); -extern int l_lovrRandomGeneratorRandomNormal(lua_State* L); -extern int l_lovrRandomGeneratorGetSeed(lua_State* L); -extern int l_lovrRandomGeneratorSetSeed(lua_State* L); - -int l_lovrMathInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrMath); - luax_registertype(L, "RandomGenerator", lovrRandomGenerator); - luax_registertype(L, "Transform", lovrTransform); - lovrMathInit(); - return 1; -} - -int l_lovrMathNewRandomGenerator(lua_State* L) { +static int l_lovrMathNewRandomGenerator(lua_State* L) { RandomGenerator* generator = lovrRandomGeneratorCreate(); if (lua_gettop(L) > 0){ Seed seed = luax_checkrandomseed(L, 1); @@ -31,7 +17,7 @@ int l_lovrMathNewRandomGenerator(lua_State* L) { return 1; } -int l_lovrMathNewTransform(lua_State* L) { +static int l_lovrMathNewTransform(lua_State* L) { float matrix[16]; luax_readtransform(L, 1, matrix, 3); Transform* transform = lovrTransformCreate(matrix); @@ -40,7 +26,7 @@ int l_lovrMathNewTransform(lua_State* L) { return 1; } -int l_lovrMathLookAt(lua_State* L) { +static int l_lovrMathLookAt(lua_State* L) { float from[3] = { luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3) }; float to[3] = { luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6) }; float up[3] = { luaL_optnumber(L, 7, 0), luaL_optnumber(L, 8, 1), luaL_optnumber(L, 9, 0) }; @@ -55,7 +41,7 @@ int l_lovrMathLookAt(lua_State* L) { return 4; } -int l_lovrMathOrientationToDirection(lua_State* L) { +static int l_lovrMathOrientationToDirection(lua_State* L) { float angle = luaL_checknumber(L, 1); float ax = luaL_optnumber(L, 2, 0); float ay = luaL_optnumber(L, 3, 1); @@ -68,7 +54,7 @@ int l_lovrMathOrientationToDirection(lua_State* L) { return 3; } -int l_lovrMathNoise(lua_State* L) { +static int l_lovrMathNoise(lua_State* L) { switch (lua_gettop(L)) { case 0: case 1: lua_pushnumber(L, lovrMathNoise1(luaL_checknumber(L, 1))); return 1; @@ -81,31 +67,31 @@ int l_lovrMathNoise(lua_State* L) { } } -int l_lovrMathRandom(lua_State* L) { +static int l_lovrMathRandom(lua_State* L) { luax_pushobject(L, lovrMathGetRandomGenerator()); lua_insert(L, 1); return l_lovrRandomGeneratorRandom(L); } -int l_lovrMathRandomNormal(lua_State* L) { +static int l_lovrMathRandomNormal(lua_State* L) { luax_pushobject(L, lovrMathGetRandomGenerator()); lua_insert(L, 1); return l_lovrRandomGeneratorRandomNormal(L); } -int l_lovrMathGetRandomSeed(lua_State* L) { +static int l_lovrMathGetRandomSeed(lua_State* L) { luax_pushobject(L, lovrMathGetRandomGenerator()); lua_insert(L, 1); return l_lovrRandomGeneratorGetSeed(L); } -int l_lovrMathSetRandomSeed(lua_State* L) { +static int l_lovrMathSetRandomSeed(lua_State* L) { luax_pushobject(L, lovrMathGetRandomGenerator()); lua_insert(L, 1); return l_lovrRandomGeneratorSetSeed(L); } -int l_lovrMathGammaToLinear(lua_State* L) { +static int l_lovrMathGammaToLinear(lua_State* L) { if (lua_istable(L, 1)) { for (int i = 0; i < 3; i++) { lua_rawgeti(L, 1, i + 1); @@ -122,7 +108,7 @@ int l_lovrMathGammaToLinear(lua_State* L) { } } -int l_lovrMathLinearToGamma(lua_State* L) { +static int l_lovrMathLinearToGamma(lua_State* L) { if (lua_istable(L, 1)) { for (int i = 0; i < 3; i++) { lua_rawgeti(L, 1, i + 1); @@ -139,7 +125,7 @@ int l_lovrMathLinearToGamma(lua_State* L) { } } -const luaL_Reg lovrMath[] = { +static const luaL_Reg lovrMath[] = { { "newRandomGenerator", l_lovrMathNewRandomGenerator }, { "newTransform", l_lovrMathNewTransform }, { "orientationToDirection", l_lovrMathOrientationToDirection }, @@ -154,3 +140,11 @@ const luaL_Reg lovrMath[] = { { NULL, NULL } }; +int luaopen_lovr_math(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrMath); + luax_registertype(L, "RandomGenerator", lovrRandomGenerator); + luax_registertype(L, "Transform", lovrTransform); + lovrMathInit(); + return 1; +} diff --git a/src/api/math.h b/src/api/math.h index 372147db..d149c24e 100644 --- a/src/api/math.h +++ b/src/api/math.h @@ -3,3 +3,8 @@ int luax_readtransform(lua_State* L, int index, mat4 transform, int scaleComponents); Seed luax_checkrandomseed(lua_State* L, int index); +int l_lovrRandomGeneratorRandom(lua_State* L); +int l_lovrRandomGeneratorRandomNormal(lua_State* L); +int l_lovrRandomGeneratorGetSeed(lua_State* L); +int l_lovrRandomGeneratorSetSeed(lua_State* L); + diff --git a/src/api/physics.c b/src/api/physics.c index 0350ebe1..c676938d 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -17,24 +17,7 @@ const char* JointTypes[] = { NULL }; -int l_lovrPhysicsInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrPhysics); - luax_registertype(L, "World", lovrWorld); - luax_registertype(L, "Collider", lovrCollider); - luax_extendtype(L, "Joint", "BallJoint", lovrJoint, lovrBallJoint); - luax_extendtype(L, "Joint", "DistanceJoint", lovrJoint, lovrDistanceJoint); - luax_extendtype(L, "Joint", "HingeJoint", lovrJoint, lovrHingeJoint); - luax_extendtype(L, "Joint", "SliderJoint", lovrJoint, lovrSliderJoint); - luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); - luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); - luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); - luax_extendtype(L, "Shape", "CylinderShape", lovrShape, lovrCylinderShape); - lovrPhysicsInit(); - return 1; -} - -int l_lovrPhysicsNewWorld(lua_State* L) { +static int l_lovrPhysicsNewWorld(lua_State* L) { float xg = luaL_optnumber(L, 1, 0.f); float yg = luaL_optnumber(L, 2, -9.81); float zg = luaL_optnumber(L, 3, 0.f); @@ -61,7 +44,7 @@ int l_lovrPhysicsNewWorld(lua_State* L) { return 1; } -int l_lovrPhysicsNewBallJoint(lua_State* L) { +static int l_lovrPhysicsNewBallJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); float x = luaL_checknumber(L, 3); @@ -73,7 +56,7 @@ int l_lovrPhysicsNewBallJoint(lua_State* L) { return 1; } -int l_lovrPhysicsNewBoxShape(lua_State* L) { +static int l_lovrPhysicsNewBoxShape(lua_State* L) { float x = luaL_optnumber(L, 1, 1.f); float y = luaL_optnumber(L, 2, x); float z = luaL_optnumber(L, 3, x); @@ -83,7 +66,7 @@ int l_lovrPhysicsNewBoxShape(lua_State* L) { return 1; } -int l_lovrPhysicsNewCapsuleShape(lua_State* L) { +static int l_lovrPhysicsNewCapsuleShape(lua_State* L) { float radius = luaL_optnumber(L, 1, 1.f); float length = luaL_optnumber(L, 2, 1.f); CapsuleShape* capsule = lovrCapsuleShapeCreate(radius, length); @@ -92,7 +75,7 @@ int l_lovrPhysicsNewCapsuleShape(lua_State* L) { return 1; } -int l_lovrPhysicsNewCylinderShape(lua_State* L) { +static int l_lovrPhysicsNewCylinderShape(lua_State* L) { float radius = luaL_optnumber(L, 1, 1.f); float length = luaL_optnumber(L, 2, 1.f); CylinderShape* cylinder = lovrCylinderShapeCreate(radius, length); @@ -101,7 +84,7 @@ int l_lovrPhysicsNewCylinderShape(lua_State* L) { return 1; } -int l_lovrPhysicsNewDistanceJoint(lua_State* L) { +static int l_lovrPhysicsNewDistanceJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); float x1 = luaL_checknumber(L, 3); @@ -116,7 +99,7 @@ int l_lovrPhysicsNewDistanceJoint(lua_State* L) { return 1; } -int l_lovrPhysicsNewHingeJoint(lua_State* L) { +static int l_lovrPhysicsNewHingeJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); float x = luaL_checknumber(L, 3); @@ -131,7 +114,7 @@ int l_lovrPhysicsNewHingeJoint(lua_State* L) { return 1; } -int l_lovrPhysicsNewSliderJoint(lua_State* L) { +static int l_lovrPhysicsNewSliderJoint(lua_State* L) { Collider* a = luax_checktype(L, 1, Collider); Collider* b = luax_checktype(L, 2, Collider); float ax = luaL_checknumber(L, 3); @@ -143,7 +126,7 @@ int l_lovrPhysicsNewSliderJoint(lua_State* L) { return 1; } -int l_lovrPhysicsNewSphereShape(lua_State* L) { +static int l_lovrPhysicsNewSphereShape(lua_State* L) { float radius = luaL_optnumber(L, 1, 1.f); SphereShape* sphere = lovrSphereShapeCreate(radius); luax_pushobject(L, sphere); @@ -151,7 +134,7 @@ int l_lovrPhysicsNewSphereShape(lua_State* L) { return 1; } -const luaL_Reg lovrPhysics[] = { +static const luaL_Reg lovrPhysics[] = { { "newWorld", l_lovrPhysicsNewWorld }, { "newBallJoint", l_lovrPhysicsNewBallJoint }, { "newBoxShape", l_lovrPhysicsNewBoxShape }, @@ -163,3 +146,20 @@ const luaL_Reg lovrPhysics[] = { { "newSphereShape", l_lovrPhysicsNewSphereShape }, { NULL, NULL } }; + +int luaopen_lovr_physics(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrPhysics); + luax_registertype(L, "World", lovrWorld); + luax_registertype(L, "Collider", lovrCollider); + luax_extendtype(L, "Joint", "BallJoint", lovrJoint, lovrBallJoint); + luax_extendtype(L, "Joint", "DistanceJoint", lovrJoint, lovrDistanceJoint); + luax_extendtype(L, "Joint", "HingeJoint", lovrJoint, lovrHingeJoint); + luax_extendtype(L, "Joint", "SliderJoint", lovrJoint, lovrSliderJoint); + luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); + luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); + luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); + luax_extendtype(L, "Shape", "CylinderShape", lovrShape, lovrCylinderShape); + lovrPhysicsInit(); + return 1; +} diff --git a/src/api/thread.c b/src/api/thread.c index 96858eef..f06abfbc 100644 --- a/src/api/thread.c +++ b/src/api/thread.c @@ -40,16 +40,7 @@ static int threadRunner(void* data) { return 0; } -int l_lovrThreadInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrThreadModule); - luax_registertype(L, "Thread", lovrThread); - luax_registertype(L, "Channel", lovrChannel); - lovrThreadInit(); - return 1; -} - -int l_lovrThreadNewThread(lua_State* L) { +static int l_lovrThreadNewThread(lua_State* L) { const char* body = luaL_checkstring(L, 1); Thread* thread = lovrThreadCreate(threadRunner, body); luax_pushobject(L, thread); @@ -57,15 +48,24 @@ int l_lovrThreadNewThread(lua_State* L) { return 1; } -int l_lovrThreadGetChannel(lua_State* L) { +static int l_lovrThreadGetChannel(lua_State* L) { const char* name = luaL_checkstring(L, 1); Channel* channel = lovrThreadGetChannel(name); luax_pushobject(L, channel); return 1; } -const luaL_Reg lovrThreadModule[] = { +static const luaL_Reg lovrThreadModule[] = { { "newThread", l_lovrThreadNewThread }, { "getChannel", l_lovrThreadGetChannel }, { NULL, NULL } }; + +int luaopen_lovr_thread(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrThreadModule); + luax_registertype(L, "Thread", lovrThread); + luax_registertype(L, "Channel", lovrChannel); + lovrThreadInit(); + return 1; +} diff --git a/src/api/timer.c b/src/api/timer.c index acc90108..63736bf9 100644 --- a/src/api/timer.c +++ b/src/api/timer.c @@ -1,45 +1,38 @@ #include "api.h" #include "timer/timer.h" -int l_lovrTimerInit(lua_State* L) { - lua_newtable(L); - luaL_register(L, NULL, lovrTimer); - lovrTimerInit(); - return 1; -} - -int l_lovrTimerGetDelta(lua_State* L) { +static int l_lovrTimerGetDelta(lua_State* L) { lua_pushnumber(L, lovrTimerGetDelta()); return 1; } -int l_lovrTimerGetAverageDelta(lua_State* L) { +static int l_lovrTimerGetAverageDelta(lua_State* L) { lua_pushnumber(L, lovrTimerGetAverageDelta()); return 1; } -int l_lovrTimerGetFPS(lua_State* L) { +static int l_lovrTimerGetFPS(lua_State* L) { lua_pushnumber(L, lovrTimerGetFPS()); return 1; } -int l_lovrTimerGetTime(lua_State* L) { +static int l_lovrTimerGetTime(lua_State* L) { lua_pushnumber(L, lovrTimerGetTime()); return 1; } -int l_lovrTimerStep(lua_State* L) { +static int l_lovrTimerStep(lua_State* L) { lua_pushnumber(L, lovrTimerStep()); return 1; } -int l_lovrTimerSleep(lua_State* L) { +static int l_lovrTimerSleep(lua_State* L) { double duration = luaL_checknumber(L, 1); lovrTimerSleep(duration); return 0; } -const luaL_Reg lovrTimer[] = { +static const luaL_Reg lovrTimer[] = { { "getDelta", l_lovrTimerGetDelta }, { "getAverageDelta", l_lovrTimerGetAverageDelta }, { "getFPS", l_lovrTimerGetFPS }, @@ -48,3 +41,10 @@ const luaL_Reg lovrTimer[] = { { "sleep", l_lovrTimerSleep }, { NULL, NULL } }; + +int luaopen_lovr_timer(lua_State* L) { + lua_newtable(L); + luaL_register(L, NULL, lovrTimer); + lovrTimerInit(); + return 1; +} diff --git a/src/lovr.c b/src/lovr.c index 948b42a3..e4511aa0 100644 --- a/src/lovr.c +++ b/src/lovr.c @@ -93,23 +93,16 @@ bool lovrRun(int argc, char** argv, int* status) { // arg lua_newtable(L); - - if (argc > 0) { - lua_pushstring(L, argv[0]); - lua_rawseti(L, -2, -2); - } - lua_pushstring(L, "lovr"); lua_rawseti(L, -2, -1); - - for (int i = 1; i < argc; i++) { + for (int i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i); + lua_rawseti(L, -2, i == 0 ? -2 : i); } - lua_setglobal(L, "arg"); - l_lovrInit(L); + // _G['lovr'] + luaopen_lovr(L); lua_setglobal(L, "lovr"); lua_pushcfunction(L, luax_getstack);