From f3848d2d5208f7a36773f5b77e0321f758d4015b Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 9 Jan 2025 21:47:08 +0300 Subject: [PATCH] progress on twnlua bindgen --- apps/twnlua/bindgen.py | 26 ++++++++++++++++++++--- include/twn_draw.h | 2 +- share/twn_api.json | 47 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/apps/twnlua/bindgen.py b/apps/twnlua/bindgen.py index c14114a..42d4096 100755 --- a/apps/twnlua/bindgen.py +++ b/apps/twnlua/bindgen.py @@ -31,6 +31,20 @@ def default(parameter): raise BaseException("Unhandled default value of type '%s'" % parameter["type"]) +def to_table(typedesc, variable, indent = 0): + binding = ' ' * indent + "lua_newtable(L);\n" + for field in typedesc["fields"]: + if field["type"] == "float": + binding += ' ' * indent + "lua_pushnumber(L, (double)(%s));\n" % (variable + ".%s" % field["name"]) + elif field["type"] in api["types"]: + binding += to_table(api["types"][field["type"]], variable + ".%s" % field["name"], indent + 4) + else: + raise BaseException("Unhandled return field type '%s'" % (field["type"])) + binding += ' ' * indent + "lua_setfield(L, -2, \"%s\");\n" % field["name"] + # binding += ' ' * indent + "lua_pop(L, 1);\n" + return binding + + print('#include "twn_game_api.h"\n') print('#define STB_DS_IMPLEMENTATION') # TODO: reuse implementation from the engine print('#include ') @@ -115,8 +129,15 @@ for procedure, procedure_desc in api["procedures"].items(): 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"])) + elif procedure_desc["return"] == "char *": + binding += " lua_pushstring(L, %s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) + elif type(procedure_desc["return"]) is dict or procedure_desc["return"] in api["types"]: + # TODO: handle enums + type_desc = procedure_desc["return"] if type(procedure_desc["return"]) is dict else api["types"][procedure_desc["return"]] + binding += " %s result = %s(%s);\n" % (type_desc["c_type"], procedure, ", ".join(param["name"] for param in procedure_desc["params"])) + binding += to_table(type_desc, "result", 4) else: - binding += " lua_warning(L, \"Unhandled return type!\", 0);\n" + raise BaseException("Unhandled return type '%s'" % (procedure_desc["return"])) binding += " return 1;\n}\n" else: binding += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) @@ -132,9 +153,8 @@ for module in modules: loader += " lua_newtable(L);\n" for procedure, procedure_desc in api["procedures"].items(): if procedure_desc["module"] == module: - loader += " lua_pushstring(L, \"%s\");\n" % procedure_desc["symbol"] loader += " lua_pushcfunction(L, binding_%s);\n" % procedure - loader += " lua_settable(L, -3);\n" + loader += " lua_setfield(L, -2, \"%s\");\n" % procedure_desc["symbol"] loader += " lua_setglobal(L, \"%s\");\n" % module loader += "}" diff --git a/include/twn_draw.h b/include/twn_draw.h index e0e85e6..757e55a 100644 --- a/include/twn_draw.h +++ b/include/twn_draw.h @@ -64,7 +64,7 @@ TWN_API void draw_billboard(const char *texture, /* sets a perspective 3d camera to be used for all 3d commands */ TWN_API void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction); -/* same as draw_camera(), but with specific use case */ +/* same as draw_camera(), but with first person controller in mind */ /* direction and up vectors are inferred from roll, pitch and yaw parameters (in radians) */ /* return value is direction and up vectors, so that you can use them in logic (such as controllers) */ typedef struct DrawCameraFromPrincipalAxesResult { diff --git a/share/twn_api.json b/share/twn_api.json index 1a73730..1d34fa2 100644 --- a/share/twn_api.json +++ b/share/twn_api.json @@ -167,6 +167,38 @@ ] }, + "draw_camera": { + "module": "draw", + "symbol": "camera", + "header": "twn_draw.h", + "params": [ + { "name": "position", "type": "Vec3" }, + { "name": "fov", "type": "float" }, + { "name": "up", "type": "Vec3" }, + { "name": "direction", "type": "Vec3" } + ] + }, + + "draw_camera_from_principal_axes": { + "module": "draw", + "symbol": "camera_from_principal_axes", + "header": "twn_draw.h", + "params": [ + { "name": "position", "type": "Vec3" }, + { "name": "fov", "type": "float" }, + { "name": "roll", "type": "float" }, + { "name": "pitch", "type": "float" }, + { "name": "yaw", "type": "float" } + ], + "return": { + "fields": [ + { "name": "direction", "type": "Vec3" }, + { "name": "up", "type": "Vec3" } + ], + "c_type": "DrawCameraFromPrincipalAxesResult" + } + }, + "draw_skybox": { "module": "draw", "symbol": "skybox", @@ -194,7 +226,8 @@ "fields": [ { "name": "x", "type": "float" }, { "name": "y", "type": "float" } - ] + ], + "c_type": "Vec2" }, "Vec3": { @@ -202,7 +235,8 @@ { "name": "x", "type": "float" }, { "name": "y", "type": "float" }, { "name": "z", "type": "float" } - ] + ], + "c_type": "Vec3" }, "Vec4": { @@ -211,7 +245,8 @@ { "name": "y", "type": "float" }, { "name": "z", "type": "float" }, { "name": "w", "type": "float" } - ] + ], + "c_type": "Vec4" }, "Color": { @@ -220,7 +255,8 @@ { "name": "g", "type": "uint8_t" }, { "name": "b", "type": "uint8_t" }, { "name": "a", "type": "uint8_t" } - ] + ], + "c_type": "Color" }, "Rect": { @@ -229,7 +265,8 @@ { "name": "y", "type": "float" }, { "name": "w", "type": "float" }, { "name": "h", "type": "float" } - ] + ], + "c_type": "Rect" }, "Control": {