twnlua: partial impl for return propagation, input now works

This commit is contained in:
veclavtalica 2024-12-23 22:02:17 +03:00
parent e7ed72dfc0
commit e06c879869
3 changed files with 50 additions and 25 deletions

View File

@ -31,27 +31,37 @@ def default(parameter):
raise BaseException("Unhandled default value of type '%s'" % parameter["type"]) raise BaseException("Unhandled default value of type '%s'" % parameter["type"])
print('#include "twn_game_api.h"') print('#include "twn_game_api.h"\n')
print('#include <lua.h>') print('#include <lua.h>')
print('#include <lualib.h>') print('#include <lualib.h>')
print('#include <lauxlib.h>\n') print('#include <lauxlib.h>\n')
print('#include <string.h>\n')
for typename, typedesc in api["types"].items(): for typename, typedesc in api["types"].items():
converter = "static %s table_to_%s(lua_State *L, int idx) {\n" % (typename, typename.lower()) converter = "static %s to_%s(lua_State *L, int idx) {\n" % (typename, typename.lower())
converter += " %s %s;\n" % (typename, typename.lower()); converter += " %s %s;\n" % (typename, typename.lower());
if not "fields" in typedesc: if "fields" in typedesc:
continue for field in typedesc["fields"]:
converter += " lua_getfield(L, idx, \"%s\");\n" % (field["name"]);
if field["type"] == "float":
converter += " %s.%s = (float)lua_tonumber(L, -1);\n" % (typename.lower(), field["name"]);
elif field["type"] == "uint8_t":
converter += " %s.%s = (uint8_t)lua_tointeger(L, -1);\n" % (typename.lower(), field["name"]);
else:
raise BaseException("Unhandled converter field type '%s'" % (field["type"]))
converter += " lua_pop(L, 1);\n";
for field in typedesc["fields"]: # todo: use a hashtable instead
converter += " lua_getfield(L, idx, \"%s\");\n" % (field["name"]); elif "enums" in typedesc:
if field["type"] == "float": converter += " char *value = lua_tostring(L, -1);\n";
converter += " %s.%s = (float)lua_tonumber(L, -1);\n" % (typename.lower(), field["name"]); for index, enum in enumerate(typedesc["enums"]):
elif field["type"] == "uint8_t": if index == 0:
converter += " %s.%s = (uint8_t)lua_tointeger(L, -1);\n" % (typename.lower(), field["name"]); converter += " if (strncmp(\"%s\", value, sizeof(\"%s\")) == 0)\n" % (enum, enum)
else: else:
raise BaseException("Unhandled converter field type '%s'" % (field["type"])) converter += " else if (strncmp(\"%s\", value, sizeof(\"%s\")) == 0)\n" % (enum, enum)
converter += " %s = %s;\n" % (typename.lower(), typedesc["enums"][enum])
converter += " lua_pop(L, 1);\n"; converter += " lua_pop(L, 1);\n";
converter += " return %s;\n}\n" % (typename.lower()) converter += " return %s;\n}\n" % (typename.lower())
@ -83,17 +93,25 @@ for procedure, procedure_desc in api["procedures"].items():
elif parameter["type"] == "char *": elif parameter["type"] == "char *":
binding += " %s = lua_tostring(L, -1);\n" % (parameter["name"]); binding += " %s = lua_tostring(L, -1);\n" % (parameter["name"]);
elif basetype in api["types"]: elif basetype in api["types"]:
if "enums" in api["types"][basetype]: if parameter["type"].endswith("*"):
binding += " %s = lua_tointeger(L, -1);\n" % (parameter["name"]); binding += " { %s_value = to_%s(L, -1); %s = &%s_value; }\n" % (parameter["name"], basetype.lower(), parameter["name"], parameter["name"]);
elif parameter["type"].endswith("*"):
binding += " { %s_value = table_to_%s(L, -1); %s = &%s_value; }\n" % (parameter["name"], basetype.lower(), parameter["name"], parameter["name"]);
else: else:
binding += " %s = table_to_%s(L, -1);\n" % (parameter["name"], basetype.lower()); binding += " %s = to_%s(L, -1);\n" % (parameter["name"], basetype.lower());
else: else:
raise BaseException("Unhandled parameter type '%s'" % (parameter["type"])) raise BaseException("Unhandled parameter type '%s'" % (parameter["type"]))
binding += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) if "return" in procedure_desc:
binding += "}\n" if procedure_desc["return"] == "bool":
binding += " lua_pushboolean(L, (int)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "float":
binding += " lua_pushnumber(L, (double)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
else:
binding += " lua_warning(L, \"Unhandled return type!\", 0);\n"
binding += " return 1;\n}\n"
else:
binding += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
binding += " return 0;\n}\n"
print(binding) print(binding)

View File

@ -4,6 +4,11 @@ offset = { x = 0, y = 0 }
angle = 0 angle = 0
function game_tick() function game_tick()
input.action {
name = "press",
control = "A"
}
draw.rectangle { draw.rectangle {
rect = { x = 0, y = 0, w = 640, h = 360 }, rect = { x = 0, y = 0, w = 640, h = 360 },
color = { r = 127, g = 0, b = 127, a = 255 }, color = { r = 127, g = 0, b = 127, a = 255 },
@ -19,11 +24,13 @@ function game_tick()
}, },
} }
draw.text { if input.action_pressed { name = "press" } then
string = "it never happened", draw.text {
position = offset, string = "it never happened",
font = "/fonts/kenney-pixel.ttf", position = offset,
} font = "/fonts/kenney-pixel.ttf",
}
end
offset.x = ORIGIN.x + (math.cos(angle) * RADIUS) offset.x = ORIGIN.x + (math.cos(angle) * RADIUS)
offset.y = ORIGIN.y + (math.sin(angle) * RADIUS) offset.y = ORIGIN.y + (math.sin(angle) * RADIUS)

View File

@ -4,7 +4,7 @@
"procedures": { "procedures": {
"input_action": { "input_action": {
"module": "input", "module": "input",
"symbol": "bind_action", "symbol": "action",
"header": "twn_input.h", "header": "twn_input.h",
"params": [ "params": [
{ "name": "name", "type": "char *" }, { "name": "name", "type": "char *" },