twnlua: partial impl for return propagation, input now works
This commit is contained in:
parent
e7ed72dfc0
commit
e06c879869
@ -31,19 +31,18 @@ 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"]:
|
for field in typedesc["fields"]:
|
||||||
converter += " lua_getfield(L, idx, \"%s\");\n" % (field["name"]);
|
converter += " lua_getfield(L, idx, \"%s\");\n" % (field["name"]);
|
||||||
if field["type"] == "float":
|
if field["type"] == "float":
|
||||||
@ -54,6 +53,17 @@ for typename, typedesc in api["types"].items():
|
|||||||
raise BaseException("Unhandled converter field type '%s'" % (field["type"]))
|
raise BaseException("Unhandled converter field type '%s'" % (field["type"]))
|
||||||
converter += " lua_pop(L, 1);\n";
|
converter += " lua_pop(L, 1);\n";
|
||||||
|
|
||||||
|
# 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())
|
converter += " return %s;\n}\n" % (typename.lower())
|
||||||
print(converter)
|
print(converter)
|
||||||
|
|
||||||
@ -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"]))
|
||||||
|
|
||||||
|
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 += " %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||||
binding += "}\n"
|
binding += " return 0;\n}\n"
|
||||||
|
|
||||||
print(binding)
|
print(binding)
|
||||||
|
|
||||||
|
|
||||||
|
@ -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()
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if input.action_pressed { name = "press" } then
|
||||||
draw.text {
|
draw.text {
|
||||||
string = "it never happened",
|
string = "it never happened",
|
||||||
position = offset,
|
position = offset,
|
||||||
font = "/fonts/kenney-pixel.ttf",
|
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)
|
||||||
|
@ -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 *" },
|
||||||
|
Loading…
Reference in New Issue
Block a user