From e06c879869a5c66a7ae265e95da6396761de18fd Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 23 Dec 2024 22:02:17 +0300 Subject: [PATCH] twnlua: partial impl for return propagation, input now works --- apps/twnlua/bindgen.py | 56 ++++++++++++++++++++----------- apps/twnlua/data/scripts/game.lua | 17 +++++++--- share/twn_api.json | 2 +- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/apps/twnlua/bindgen.py b/apps/twnlua/bindgen.py index 9079c21..1211fc1 100755 --- a/apps/twnlua/bindgen.py +++ b/apps/twnlua/bindgen.py @@ -31,27 +31,37 @@ def default(parameter): 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 ') print('#include ') print('#include \n') +print('#include \n') 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()); - if not "fields" in typedesc: - continue + if "fields" in typedesc: + 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"]: - 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"])) + # todo: use a hashtable instead + elif "enums" in typedesc: + converter += " char *value = lua_tostring(L, -1);\n"; + for index, enum in enumerate(typedesc["enums"]): + if index == 0: + converter += " if (strncmp(\"%s\", value, sizeof(\"%s\")) == 0)\n" % (enum, enum) + else: + 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 += " return %s;\n}\n" % (typename.lower()) @@ -83,17 +93,25 @@ for procedure, procedure_desc in api["procedures"].items(): elif parameter["type"] == "char *": binding += " %s = lua_tostring(L, -1);\n" % (parameter["name"]); elif basetype in api["types"]: - if "enums" in api["types"][basetype]: - binding += " %s = lua_tointeger(L, -1);\n" % (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"]); + if parameter["type"].endswith("*"): + binding += " { %s_value = to_%s(L, -1); %s = &%s_value; }\n" % (parameter["name"], basetype.lower(), parameter["name"], parameter["name"]); 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: raise BaseException("Unhandled parameter type '%s'" % (parameter["type"])) - binding += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) - binding += "}\n" + if "return" in procedure_desc: + 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) diff --git a/apps/twnlua/data/scripts/game.lua b/apps/twnlua/data/scripts/game.lua index f9ead71..12bc950 100644 --- a/apps/twnlua/data/scripts/game.lua +++ b/apps/twnlua/data/scripts/game.lua @@ -4,6 +4,11 @@ offset = { x = 0, y = 0 } angle = 0 function game_tick() + input.action { + name = "press", + control = "A" + } + draw.rectangle { rect = { x = 0, y = 0, w = 640, h = 360 }, color = { r = 127, g = 0, b = 127, a = 255 }, @@ -19,11 +24,13 @@ function game_tick() }, } - draw.text { - string = "it never happened", - position = offset, - font = "/fonts/kenney-pixel.ttf", - } + if input.action_pressed { name = "press" } then + draw.text { + string = "it never happened", + position = offset, + font = "/fonts/kenney-pixel.ttf", + } + end offset.x = ORIGIN.x + (math.cos(angle) * RADIUS) offset.y = ORIGIN.y + (math.sin(angle) * RADIUS) diff --git a/share/twn_api.json b/share/twn_api.json index ac9ffd1..a49cd84 100644 --- a/share/twn_api.json +++ b/share/twn_api.json @@ -4,7 +4,7 @@ "procedures": { "input_action": { "module": "input", - "symbol": "bind_action", + "symbol": "action", "header": "twn_input.h", "params": [ { "name": "name", "type": "char *" },