mirror of https://github.com/bjornbytes/lovr.git
Add luax_checkcolor;
This commit is contained in:
parent
32b05cb15d
commit
b8fededcf6
|
@ -244,26 +244,7 @@ int l_lovrGraphicsGetColor(lua_State* L) {
|
|||
}
|
||||
|
||||
int l_lovrGraphicsSetColor(lua_State* L) {
|
||||
Color color = { 0xff, 0xff, 0xff, 0xff };
|
||||
|
||||
if (lua_istable(L, 1)) {
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
}
|
||||
color.r = luaL_checknumber(L, -4);
|
||||
color.g = luaL_checknumber(L, -3);
|
||||
color.b = luaL_checknumber(L, -2);
|
||||
color.a = luaL_optnumber(L, -1, 255);
|
||||
lua_pop(L, 4);
|
||||
} else if (lua_gettop(L) >= 3) {
|
||||
color.r = lua_tointeger(L, 1);
|
||||
color.g = lua_tointeger(L, 2);
|
||||
color.b = lua_tointeger(L, 3);
|
||||
color.a = lua_isnoneornil(L, 4) ? 255 : lua_tointeger(L, 4);
|
||||
} else {
|
||||
return luaL_error(L, "Invalid color, expected 3 numbers, 4 numbers, or a table");
|
||||
}
|
||||
|
||||
Color color = luax_checkcolor(L, 1);
|
||||
lovrGraphicsSetColor(color);
|
||||
return 0;
|
||||
}
|
||||
|
|
24
src/luax.c
24
src/luax.c
|
@ -148,3 +148,27 @@ void* luax_optenum(lua_State* L, int index, const char* fallback, map_int_t* map
|
|||
|
||||
return value;
|
||||
}
|
||||
|
||||
Color luax_checkcolor(lua_State* L, int index) {
|
||||
Color color = { 0xff, 0xff, 0xff, 0xff };
|
||||
|
||||
if (lua_istable(L, 1)) {
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
}
|
||||
color.r = luaL_checknumber(L, -4);
|
||||
color.g = luaL_checknumber(L, -3);
|
||||
color.b = luaL_checknumber(L, -2);
|
||||
color.a = luaL_optnumber(L, -1, 255);
|
||||
lua_pop(L, 4);
|
||||
} else if (lua_gettop(L) >= index + 2) {
|
||||
color.r = lua_tointeger(L, index);
|
||||
color.g = lua_tointeger(L, index + 1);
|
||||
color.b = lua_tointeger(L, index + 2);
|
||||
color.a = lua_isnoneornil(L, index + 3) ? 255 : lua_tointeger(L, index + 3);
|
||||
} else {
|
||||
luaL_error(L, "Invalid color, expected 3 numbers, 4 numbers, or a table");
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "lib/map/map.h"
|
||||
#include "util.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
|
@ -35,3 +36,4 @@ void luax_registerobject(lua_State* L, void* object);
|
|||
void luax_pushenum(lua_State* L, 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);
|
||||
Color luax_checkcolor(lua_State* L, int index);
|
||||
|
|
Loading…
Reference in New Issue