progress on twnlua bindgen

This commit is contained in:
veclavtalica 2025-01-09 21:47:08 +03:00
parent 8c401eda75
commit f3848d2d52
3 changed files with 66 additions and 9 deletions

View File

@ -31,6 +31,20 @@ def default(parameter):
raise BaseException("Unhandled default value of type '%s'" % parameter["type"]) 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('#include "twn_game_api.h"\n')
print('#define STB_DS_IMPLEMENTATION') # TODO: reuse implementation from the engine print('#define STB_DS_IMPLEMENTATION') # TODO: reuse implementation from the engine
print('#include <stb_ds.h>') print('#include <stb_ds.h>')
@ -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"])) binding += " lua_pushboolean(L, (int)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "float": elif procedure_desc["return"] == "float":
binding += " lua_pushnumber(L, (double)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) 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: else:
binding += " lua_warning(L, \"Unhandled return type!\", 0);\n" raise BaseException("Unhandled return type '%s'" % (procedure_desc["return"]))
binding += " return 1;\n}\n" binding += " return 1;\n}\n"
else: else:
binding += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) 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" loader += " lua_newtable(L);\n"
for procedure, procedure_desc in api["procedures"].items(): for procedure, procedure_desc in api["procedures"].items():
if procedure_desc["module"] == module: if procedure_desc["module"] == module:
loader += " lua_pushstring(L, \"%s\");\n" % procedure_desc["symbol"]
loader += " lua_pushcfunction(L, binding_%s);\n" % procedure 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 += " lua_setglobal(L, \"%s\");\n" % module
loader += "}" loader += "}"

View File

@ -64,7 +64,7 @@ TWN_API void draw_billboard(const char *texture,
/* sets a perspective 3d camera to be used for all 3d commands */ /* 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); 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) */ /* 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) */ /* return value is direction and up vectors, so that you can use them in logic (such as controllers) */
typedef struct DrawCameraFromPrincipalAxesResult { typedef struct DrawCameraFromPrincipalAxesResult {

View File

@ -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": { "draw_skybox": {
"module": "draw", "module": "draw",
"symbol": "skybox", "symbol": "skybox",
@ -194,7 +226,8 @@
"fields": [ "fields": [
{ "name": "x", "type": "float" }, { "name": "x", "type": "float" },
{ "name": "y", "type": "float" } { "name": "y", "type": "float" }
] ],
"c_type": "Vec2"
}, },
"Vec3": { "Vec3": {
@ -202,7 +235,8 @@
{ "name": "x", "type": "float" }, { "name": "x", "type": "float" },
{ "name": "y", "type": "float" }, { "name": "y", "type": "float" },
{ "name": "z", "type": "float" } { "name": "z", "type": "float" }
] ],
"c_type": "Vec3"
}, },
"Vec4": { "Vec4": {
@ -211,7 +245,8 @@
{ "name": "y", "type": "float" }, { "name": "y", "type": "float" },
{ "name": "z", "type": "float" }, { "name": "z", "type": "float" },
{ "name": "w", "type": "float" } { "name": "w", "type": "float" }
] ],
"c_type": "Vec4"
}, },
"Color": { "Color": {
@ -220,7 +255,8 @@
{ "name": "g", "type": "uint8_t" }, { "name": "g", "type": "uint8_t" },
{ "name": "b", "type": "uint8_t" }, { "name": "b", "type": "uint8_t" },
{ "name": "a", "type": "uint8_t" } { "name": "a", "type": "uint8_t" }
] ],
"c_type": "Color"
}, },
"Rect": { "Rect": {
@ -229,7 +265,8 @@
{ "name": "y", "type": "float" }, { "name": "y", "type": "float" },
{ "name": "w", "type": "float" }, { "name": "w", "type": "float" },
{ "name": "h", "type": "float" } { "name": "h", "type": "float" }
] ],
"c_type": "Rect"
}, },
"Control": { "Control": {