mirror of https://github.com/bjornbytes/lovr.git
Enum helpers;
This commit is contained in:
parent
a59f03ef78
commit
82a1f17b16
|
@ -275,17 +275,8 @@ int l_lovrGraphicsSetCullingEnabled(lua_State* L) {
|
|||
}
|
||||
|
||||
int l_lovrGraphicsGetPolygonWinding(lua_State* L) {
|
||||
switch (lovrGraphicsGetPolygonWinding()) {
|
||||
case POLYGON_WINDING_CLOCKWISE:
|
||||
lua_pushstring(L, "clockwise");
|
||||
return 1;
|
||||
|
||||
case POLYGON_WINDING_COUNTERCLOCKWISE:
|
||||
lua_pushstring(L, "counterclockwise");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
lua_pushstring(L, map_int_find(&PolygonWindings, lovrGraphicsGetPolygonWinding()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsSetPolygonWinding(lua_State* L) {
|
||||
|
@ -453,20 +444,11 @@ int l_lovrGraphicsNewBuffer(lua_State* L) {
|
|||
}
|
||||
} else {
|
||||
luaL_argerror(L, 1, "table or number expected");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* userDrawMode = luaL_optstring(L, drawModeIndex, "fan");
|
||||
BufferDrawMode* drawMode = (BufferDrawMode*) map_get(&BufferDrawModes, userDrawMode);
|
||||
if (!drawMode) {
|
||||
return luaL_error(L, "Invalid buffer draw mode: '%s'", userDrawMode);
|
||||
}
|
||||
|
||||
const char* userUsage = luaL_optstring(L, drawModeIndex + 1, "dynamic");
|
||||
BufferUsage* usage = (BufferUsage*) map_get(&BufferUsages, userUsage);
|
||||
if (!usage) {
|
||||
return luaL_error(L, "Invalid buffer usage: '%s'", userUsage);
|
||||
}
|
||||
|
||||
BufferDrawMode* drawMode = (BufferDrawMode*) luax_optenum(L, drawModeIndex, "fan", &BufferDrawModes, "buffer draw mode");
|
||||
BufferUsage* usage = (BufferUsage*) luax_optenum(L, drawModeIndex + 1, "dynamic", &BufferUsages, "buffer usage");
|
||||
Buffer* buffer = lovrBufferCreate(size, format.length ? &format : NULL, *drawMode, *usage);
|
||||
|
||||
if (dataIndex) {
|
||||
|
|
|
@ -147,12 +147,7 @@ int l_lovrHeadsetGetAngularVelocity(lua_State* L) {
|
|||
}
|
||||
|
||||
int l_lovrHeadsetGetController(lua_State* L) {
|
||||
const char* userHand = luaL_checkstring(L, 1);
|
||||
ControllerHand* hand = (ControllerHand*) map_get(&ControllerHands, userHand);
|
||||
if (!hand) {
|
||||
return luaL_error(L, "Invalid controller hand: '%s'", userHand);
|
||||
}
|
||||
|
||||
ControllerHand* hand = (ControllerHand*) luax_checkenum(L, 1, &ControllerHands, "controller hand");
|
||||
luax_pushcontroller(L, lovrHeadsetGetController(*hand));
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -51,18 +51,10 @@ void luax_checkbufferformat(lua_State* L, int index, BufferFormat* format) {
|
|||
lua_rawgeti(L, -3, 3);
|
||||
|
||||
const char* name = lua_tostring(L, -3);
|
||||
const char* userAttributeType = lua_tostring(L, -2);
|
||||
BufferAttributeType* type = (BufferAttributeType*) luax_checkenum(L, -2, &BufferAttributeTypes, "buffer attribute type");
|
||||
int size = lua_tointeger(L, -1);
|
||||
|
||||
BufferAttributeType* type = (BufferAttributeType*) map_get(&BufferAttributeTypes, userAttributeType);
|
||||
if (!type) {
|
||||
luaL_error(L, "Invalid buffer attribute type: '%s'", userAttributeType);
|
||||
return;
|
||||
}
|
||||
|
||||
BufferAttribute attribute = { .name = name, .type = *type, .size = size };
|
||||
vec_push(format, attribute);
|
||||
|
||||
lua_pop(L, 4);
|
||||
}
|
||||
}
|
||||
|
@ -101,44 +93,14 @@ int l_lovrBufferDraw(lua_State* L) {
|
|||
|
||||
int l_lovrBufferGetDrawMode(lua_State* L) {
|
||||
Buffer* buffer = luax_checkbuffer(L, 1);
|
||||
BufferDrawMode drawMode = lovrBufferGetDrawMode(buffer);
|
||||
|
||||
switch (drawMode) {
|
||||
case BUFFER_POINTS:
|
||||
lua_pushstring(L, "points");
|
||||
break;
|
||||
|
||||
case BUFFER_TRIANGLE_STRIP:
|
||||
lua_pushstring(L, "strip");
|
||||
break;
|
||||
|
||||
case BUFFER_TRIANGLES:
|
||||
lua_pushstring(L, "triangles");
|
||||
break;
|
||||
|
||||
case BUFFER_TRIANGLE_FAN:
|
||||
lua_pushstring(L, "fan");
|
||||
break;
|
||||
|
||||
default:
|
||||
lua_pushstring(L, "unknown");
|
||||
break;
|
||||
}
|
||||
|
||||
lua_pushstring(L, map_int_find(&BufferDrawModes, lovrBufferGetDrawMode(buffer)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrBufferSetDrawMode(lua_State* L) {
|
||||
Buffer* buffer = luax_checkbuffer(L, 1);
|
||||
|
||||
const char* userDrawMode = luaL_checkstring(L, 2);
|
||||
BufferDrawMode* drawMode = (BufferDrawMode*) map_get(&BufferDrawModes, userDrawMode);
|
||||
if (!drawMode) {
|
||||
return luaL_error(L, "Invalid buffer draw mode: '%s'", userDrawMode);
|
||||
}
|
||||
|
||||
BufferDrawMode* drawMode = (BufferDrawMode*) luax_checkenum(L, 2, &BufferDrawModes, "buffer draw mode");
|
||||
lovrBufferSetDrawMode(buffer, *drawMode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#include "controller.h"
|
||||
#include "../headset.h"
|
||||
#include "../../util.h"
|
||||
|
||||
void luax_pushcontroller(lua_State* L, Controller* controller) {
|
||||
if (controller == NULL) {
|
||||
|
@ -52,20 +54,6 @@ int l_lovrControllerGetOrientation(lua_State* L) {
|
|||
|
||||
int l_lovrControllerGetHand(lua_State* L) {
|
||||
Controller* controller = luax_checkcontroller(L, 1);
|
||||
ControllerHand hand = lovrHeadsetGetControllerHand(controller);
|
||||
|
||||
switch (hand) {
|
||||
case CONTROLLER_HAND_LEFT:
|
||||
lua_pushstring(L, "left");
|
||||
break;
|
||||
|
||||
case CONTROLLER_HAND_RIGHT:
|
||||
lua_pushstring(L, "right");
|
||||
break;
|
||||
|
||||
default:
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
lua_pushstring(L, map_int_find(&ControllerHands, lovrHeadsetGetControllerHand(controller)));
|
||||
return 1;
|
||||
}
|
||||
|
|
34
src/util.c
34
src/util.c
|
@ -71,3 +71,37 @@ void luaRegisterType(lua_State* L, const char* name, const luaL_Reg* functions,
|
|||
// Pop metatable
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
// Returns a key that maps to value or NULL if the value is not in the map
|
||||
const char* map_int_find(map_int_t* map, int value) {
|
||||
const char* key;
|
||||
map_iter_t iter = map_iter(map);
|
||||
while ((key = map_next(map, &iter))) {
|
||||
if (*map_get(map, key) == value) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* luax_checkenum(lua_State* L, int index, map_int_t* map, const char* typeName) {
|
||||
const char* key = luaL_checkstring(L, index);
|
||||
void* value = map_get(map, key);
|
||||
if (!value) {
|
||||
luaL_error(L, "Invalid %s '%s'", typeName, key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void* luax_optenum(lua_State* L, int index, const char* fallback, map_int_t* map, const char* typeName) {
|
||||
const char* key = luaL_optstring(L, index, fallback);
|
||||
void* value = map_get(map, key);
|
||||
if (!value) {
|
||||
luaL_error(L, "Invalid %s '%s'", typeName, key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "vendor/vec/vec.h"
|
||||
#include "vendor/map/map.h"
|
||||
|
||||
#ifndef UTIL_TYPES
|
||||
#define UTIL_TYPES
|
||||
|
@ -16,3 +17,6 @@ unsigned char* loadImage(void* data, size_t size, int* w, int* h, int* n, int ch
|
|||
void luaRegisterModule(lua_State* L, const char* name, const luaL_Reg* module);
|
||||
void luaRegisterType(lua_State* L, const char* name, const luaL_Reg* functions, lua_CFunction gc);
|
||||
int luaPreloadModule(lua_State* L, const char* key, lua_CFunction f);
|
||||
const char* map_int_find(map_int_t *map, int value);
|
||||
void* luax_checkenum(lua_State* L, int index, map_int_t* map, const char* typeName);
|
||||
void* luax_optenum(lua_State* L, int index, const char* fallback, map_int_t* map, const char* typeName);
|
||||
|
|
Loading…
Reference in New Issue