Fancy compare mode aliases;

This commit is contained in:
bjorn 2022-05-31 21:33:06 -07:00
parent 513a1876c9
commit 0104fe1910
3 changed files with 40 additions and 10 deletions

View File

@ -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

View File

@ -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");

View File

@ -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;
}