diff --git a/src/api/api.h b/src/api/api.h index 84243b5f..91b2fba1 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -153,6 +153,7 @@ void* luax_readfile(const char* filename, size_t* bytesRead); #ifndef LOVR_DISABLE_GRAPHICS struct Buffer; void luax_readbufferdata(struct lua_State* L, int index, struct Buffer* buffer, char* data); +uint32_t luax_checkcomparemode(struct lua_State* L, int index); #endif #ifndef LOVR_DISABLE_MATH diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index 6fb27e7d..6a780832 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -447,6 +447,39 @@ static Canvas luax_checkcanvas(lua_State* L, int index) { return canvas; } +uint32_t luax_checkcomparemode(lua_State* L, int index) { + size_t length; + const char* string = lua_tolstring(L, index, &length); + + if (string && length <= 2) { + if (string[0] == '=' && (length == 1 || string[1] == '=')) { + return COMPARE_EQUAL; + } + + if ((string[0] == '~' || string[0] == '!') && string[1] == '=') { + return COMPARE_NEQUAL; + } + + if (string[0] == '<' && length == 1) { + return COMPARE_LESS; + } + + if (string[0] == '<' && string[1] == '=') { + return COMPARE_LEQUAL; + } + + if (string[0] == '>' && length == 1) { + return COMPARE_GREATER; + } + + if (string[0] == '>' && string[1] == '=') { + return COMPARE_GEQUAL; + } + } + + return luax_checkenum(L, index, CompareMode, "none"); +} + static int l_lovrGraphicsInit(lua_State* L) { bool debug = false; bool vsync = false; @@ -885,7 +918,7 @@ static int l_lovrGraphicsNewSampler(lua_State* L) { lua_pop(L, 1); lua_getfield(L, 1, "compare"); - info.compare = luax_checkenum(L, -1, CompareMode, "none"); + info.compare = luax_checkcomparemode(L, -1); lua_pop(L, 1); lua_getfield(L, 1, "anisotropy"); diff --git a/src/api/l_graphics_pass.c b/src/api/l_graphics_pass.c index 77b42949..59812168 100644 --- a/src/api/l_graphics_pass.c +++ b/src/api/l_graphics_pass.c @@ -211,7 +211,7 @@ static int l_lovrPassSetCullMode(lua_State* L) { static int l_lovrPassSetDepthTest(lua_State* L) { Pass* pass = luax_checktype(L, 1, Pass); - CompareMode test = lua_isnoneornil(L, 2) ? COMPARE_NONE : luax_checkenum(L, 2, CompareMode, NULL); + CompareMode test = luax_checkcomparemode(L, 2); lovrPassSetDepthTest(pass, test); return 0; } @@ -256,14 +256,10 @@ static int l_lovrPassSetShader(lua_State* L) { static int l_lovrPassSetStencilTest(lua_State* L) { Pass* pass = luax_checktype(L, 1, Pass); - if (lua_isnoneornil(L, 2)) { - lovrPassSetStencilTest(pass, COMPARE_NONE, 0, 0xff); - } else { - CompareMode test = luax_checkenum(L, 2, CompareMode, NULL); - uint8_t value = luaL_checkinteger(L, 3); - uint8_t mask = luaL_optinteger(L, 4, 0xff); - lovrPassSetStencilTest(pass, test, value, mask); - } + CompareMode test = luax_checkcomparemode(L, 2); + uint8_t value = lua_tointeger(L, 3); + uint8_t mask = luaL_optinteger(L, 4, 0xff); + lovrPassSetStencilTest(pass, test, value, mask); return 0; }