Add color support.
Some checks failed
Build Dusk / run-tests (push) Failing after 1m35s
Build Dusk / build-linux (push) Failing after 1m34s
Build Dusk / build-psp (push) Failing after 1m53s

This commit is contained in:
2026-02-01 11:16:52 -06:00
parent 982d28a3e0
commit 78e1ae885a
8 changed files with 211 additions and 45 deletions

View File

@@ -37,4 +37,12 @@ elseif(DUSK_TARGET_SYSTEM STREQUAL "psp")
DISPLAY_HEIGHT=272
DISPLAY_SIZE_DYNAMIC=0
)
endif()
endif()
dusk_run_python(
dusk_color_defs
tools.display.color.csv
--csv ${CMAKE_CURRENT_SOURCE_DIR}/color.csv
--output ${DUSK_GENERATED_HEADERS_DIR}/display/color.h
)
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} dusk_color_defs)

View File

@@ -19,4 +19,5 @@ brown,0.6,0.4,0.2,1
pink,1,0.75,0.8,1
lime,0.75,1,0,1
navy,0,0,0.5,1
teal,0,0.5,0.5,1
teal,0,0.5,0.5,1
cornflower_blue,0.39,0.58,0.93,1
1 name r g b a
19 pink 1 0.75 0.8 1
20 lime 0.75 1 0 1
21 navy 0 0 0.5 1
22 teal 0 0.5 0.5 1
23 cornflower_blue 0.39 0.58 0.93 1

View File

@@ -14,7 +14,45 @@
void moduleColor(scriptcontext_t *context) {
assertNotNull(context, "Context cannot be NULL.");
scriptStructRegister(context, "color_mt", moduleColorGetter, NULL);
scriptStructRegister(
context, "color_mt", moduleColorGetter, moduleColorSetter
);
scriptContextRegFunc(context, "color", moduleColorFuncColor);
scriptContextExec(context, COLOR_SCRIPT);
}
int moduleColorFuncColor(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
scriptcontext_t *context = *(scriptcontext_t **)lua_getextraspace(L);
assertNotNull(context, "Script context cannot be NULL.");
// Needs 4 channel uint8_t
if(
!lua_isnumber(L, 1) || !lua_isnumber(L, 2) ||
!lua_isnumber(L, 3) || !lua_isnumber(L, 4)
) {
return luaL_error(L, "color(r, g, b, a) requires four number arguments.");
}
colorchannel8_t r = (colorchannel8_t)lua_tonumber(L, 1);
colorchannel8_t g = (colorchannel8_t)lua_tonumber(L, 2);
colorchannel8_t b = (colorchannel8_t)lua_tonumber(L, 3);
colorchannel8_t a = (colorchannel8_t)lua_tonumber(L, 4);
// Create color_t as lua memory
color_t *color = (color_t*)lua_newuserdata(L, sizeof(color_t));
color->r = r;
color->g = g;
color->b = b;
color->a = a;
// Push color struct
scriptStructPushMetatable(context, "color_mt", color);
return 1;
}
void moduleColorGetter(
@@ -45,4 +83,51 @@ void moduleColorGetter(
outValue->value.intValue = color->a;
return;
}
}
void moduleColorSetter(
const scriptcontext_t *context,
const char_t *key,
void *structPtr,
const scriptvalue_t *inValue
) {
color_t *color = (color_t*)structPtr;
if(stringCompare(key, "r") == 0) {
if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
color->r = (colorchannel8_t)inValue->value.intValue;
} else if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
color->r = (colorchannel8_t)(inValue->value.floatValue);
} else {
assertUnreachable("Invalid type for color channel r");
}
return;
} else if(stringCompare(key, "g") == 0) {
if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
color->g = (colorchannel8_t)inValue->value.intValue;
} else if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
color->g = (colorchannel8_t)(inValue->value.floatValue);
} else {
assertUnreachable("Invalid type for color channel g");
}
return;
} else if(stringCompare(key, "b") == 0) {
if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
color->b = (colorchannel8_t)inValue->value.intValue;
} else if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
color->b = (colorchannel8_t)(inValue->value.floatValue);
} else {
assertUnreachable("Invalid type for color channel b");
}
return;
} else if(stringCompare(key, "a") == 0) {
if(inValue->type == SCRIPT_VALUE_TYPE_INT) {
color->a = (colorchannel8_t)inValue->value.intValue;
} else if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) {
color->a = (colorchannel8_t)(inValue->value.floatValue);
} else {
assertUnreachable("Invalid type for color channel a");
}
return;
}
}

View File

@@ -15,6 +15,14 @@
*/
void moduleColor(scriptcontext_t *context);
/**
* Lua function to create a color.
*
* @param L The Lua state.
* @return Number of return values.
*/
int moduleColorFuncColor(lua_State *L);
/**
* Getter function for the color structure.
*
@@ -28,4 +36,19 @@ void moduleColorGetter(
const char_t *key,
const void *structPtr,
scriptvalue_t *outValue
);
/**
* Setter function for the color structure.
*
* @param context The script context.
* @param key The key to set.
* @param structPtr Pointer to the color structure.
* @param inValue Input value.
*/
void moduleColorSetter(
const scriptcontext_t *context,
const char_t *key,
void *structPtr,
const scriptvalue_t *inValue
);

View File

@@ -45,13 +45,17 @@ int moduleSpriteBatchPush(lua_State *L) {
return luaL_error(L, "Sprite coordinates must be numbers");
}
// Color (struct), must be Nil for now
if(!lua_isnil(L, 6)) {
return luaL_error(L, "Color parameter must be nil");
// Color (struct) or nil for white
color_t *color = NULL;
if(lua_isuserdata(L, 6)) {
color = *(color_t**)lua_touserdata(L, 6);
assertNotNull(color, "Color struct cannot be NULL");
} else if(lua_isnil(L, 6)) {
// Extrapolate later.
} else {
return luaL_error(L, "Sprite color must be a color struct or nil");
}
// if(!lua_istable(L, 6)) {
// return luaL_error(L, "Color parameter must be a color struct");
// }
// Optional UV min and maxes, defaults to 0,0 -> 1,1
float_t u0 = 0.0f;
@@ -88,7 +92,7 @@ int moduleSpriteBatchPush(lua_State *L) {
minY,
maxX,
maxY,
COLOR_MAGENTA,
color == NULL ? COLOR_WHITE : *color,
u0,
v0,
u1,