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

@@ -17,48 +17,88 @@ outHeader += '#include "dusk.h"\n\n'
outHeader += f"typedef float_t colorchannelf_t;\n"
colors = {}
with open(args.csv, newline="", encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
# CSV must have id column.
if "id" not in reader.fieldnames:
raise Exception("CSV file must have 'id' column")
if "name" not in reader.fieldnames:
raise Exception("CSV file must have 'name' column")
# For each ID
inputIds = []
inputIdValues = {}
# For each color, by name...
for row in reader:
inputId = row["id"]
if inputId not in inputIds:
inputIds.append(inputId)
name = row["name"]
r = row["r"]
g = row["g"]
b = row["b"]
a = row["a"] if "a" in row and row["a"] != "" else "1.0"
# For each ID, create enum entry.
count = 0
outHeader += "typedef enum {\n"
outHeader += f" INPUT_ACTION_NULL = {count},\n\n"
count += 1
for inputId in inputIds:
inputIdValues[inputId] = count
outHeader += f" {csvIdToEnumName(inputId)} = {count},\n"
count += 1
outHeader += f"\n INPUT_ACTION_COUNT = {count}\n"
outHeader += "} inputaction_t;\n\n"
# Ensure values are between 0.0 and 1.0
for channelValue in (r, g, b, a):
fvalue = float(channelValue)
if fvalue < 0.0 or fvalue > 1.0:
raise Exception(f"Color channel value {channelValue} for color {name} is out of range (0.0 to 1.0)")
# Write IDs to char array.
outHeader += f"static const char_t* INPUT_ACTION_IDS[] = {{\n"
for inputId in inputIds:
outHeader += f" [{csvIdToEnumName(inputId)}] = \"{inputId}\",\n"
outHeader += f"}};\n\n"
colors[name] = (r, g, b, a)
# Lua Script
outHeader += f"static const char_t *INPUT_ACTION_SCRIPT = \n"
for inputId in inputIds:
# Reference the enum
outHeader += f" \"{csvIdToEnumName(inputId)} = {inputIdValues[inputId]}\\n\"\n"
outHeader += f";\n\n"
# Prep output header
outHeader = "#pragma once\n"
outHeader += '#include "dusk.h"\n\n'
# Write to output file.
os.makedirs(os.path.dirname(args.output), exist_ok=True)
# Typedefs for float and uin8t color channels
outHeader += f"typedef float_t colorchannelf_t;\n"
outHeader += f"typedef uint8_t colorchannel8_t;\n\n"
# Typedefs for 3 and 4 channel colors in both float and uint8_t
outHeader += f"typedef struct {{ colorchannelf_t r, g, b; }} color3f_t;\n"
outHeader += f"typedef struct {{ colorchannelf_t r, g, b, a; }} color4f_t;\n"
outHeader += f"typedef struct {{ colorchannel8_t r, g, b; }} color3b_t;\n"
outHeader += f"typedef struct {{ colorchannel8_t r, g, b, a; }} color4b_t;\n"
outHeader += "typedef color4b_t color_t;\n\n"# Preferred format.
outHeader += "#define color3f(r, g, b) ((color3f_t){ r, g, b })\n"
outHeader += "#define color4f(r, g, b, a) ((color4f_t){ r, g, b, a })\n"
outHeader += "#define color3b(r, g, b) ((color3b_t){ r, g, b })\n"
outHeader += "#define color4b(r, g, b, a) ((color4b_t){ r, g, b, a })\n\n"
luaScript = ""
# Define each color, in each format
for color_name, (r, g, b, a) in colors.items():
outHeader += f"// Color: {color_name}\n"
r8 = int(float(r) * 255)
g8 = int(float(g) * 255)
b8 = int(float(b) * 255)
a8 = int(float(a) * 255)
macro_name = "COLOR_" + color_name.upper()
outHeader += f"#define {macro_name}_4B color4b({r8}, {g8}, {b8}, {a8})\n"
outHeader += f"#define {macro_name}_3B color3b({r8}, {g8}, {b8})\n"
outHeader += f"#define {macro_name}_3F color3f({r}f, {g}f, {b}f)\n"
outHeader += f"#define {macro_name}_4F color4f({r}f, {g}f, {b}f, {a}f)\n"
outHeader += f"#define {macro_name} {macro_name}_4B\n\n" # Preferred format.
nameSplit = color_name.split("_")
camelFirstLetter = ""
for part in nameSplit:
camelFirstLetter += part[0].upper() + part[1:].lower()
luaName = "color" + camelFirstLetter
luaScript += f"function {luaName}()\n"
luaScript += f" return color({r8}, {g8}, {b8}, {a8})\n"
luaScript += "end\n\n"
outHeader += "// Lua color functions\n"
outHeader += "#define COLOR_SCRIPT \\\n"
# Stringify and put each line with a backslash
for line in luaScript.splitlines():
outHeader += f' "{line}\\n" \\\n'
outHeader = outHeader.rstrip(" \\\n") + "\n" # Remove last backslash and add newline
# Write to output file
with open(args.output, "w", encoding="utf-8") as outFile:
outFile.write(outHeader)