From cecb06903c941d6f5497f0afbcb441ad5a9b34b8 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sun, 14 May 2023 20:40:19 +0200 Subject: [PATCH] Semi-automatically generate Lua bindings --- lua_translate | 81 ++++++++++++++++++++++++++ modules/engine/FUNCTIONS.toml | 57 ++++++++++++++++++ modules/engine/addon/lua.c | 106 +++++++++++++++++++++++++--------- run | 1 + 4 files changed, 219 insertions(+), 26 deletions(-) create mode 100755 lua_translate create mode 100644 modules/engine/FUNCTIONS.toml diff --git a/lua_translate b/lua_translate new file mode 100755 index 0000000..0f0bf59 --- /dev/null +++ b/lua_translate @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +import toml +typesIn = { + "__unknown": "lua_touserdata", + "char": "luaL_checkinteger", + "int": "luaL_checkinteger", + "long long": "luaL_checkinteger", + "char *": "(char *)luaL_checkstring" +} + +typesOut = { + "__unknown": "lua_pushlightuserdata", + "char": "lua_pushinteger", + "int": "lua_pushinteger", + "long long": "lua_pushinteger", + "char *": "lua_pushstring" +} + +functions = toml.loads(open("modules/engine/FUNCTIONS.toml").read()) +ofile = open("modules/engine/addon/lua.c","w") + +ofile.write('''\ +#include +#include +#include + +lua_State * engine_lua_state; +''') + +for func in functions: + invarCount = 1 + funcnew = "engine_luaf_" +func.replace("engine_","",1) + ofile.write('int ' +funcnew+ '(lua_State *L) {\n') + for arg in functions[func]["arguments"]: + if not arg in typesIn: + checkfunc = typesIn["__unknown"] + else: + checkfunc = typesIn[arg] + ofile.write("\t" +arg+ ' invar' +str(invarCount)+ ' = ' +checkfunc+ '(L,' +str(invarCount)+ ');\n') + invarCount += 1 + + invarCount = 1 + argstring = "" + for arg in functions[func]["arguments"]: + argstring += ",invar" +str(invarCount) + invarCount += 1 + argstring = "(" +argstring.strip(",")+ ")" + + outtype = functions[func]["type"] + if outtype == "void": + ofile.write('\t' + func + argstring + ";") + ofile.write('\n\treturn 0;') + else: + if not outtype in typesOut: + pushfunc = typesOut["__unknown"] + else: + pushfunc = typesOut[arg] + + ofile.write('\t' +outtype+ ' outvar = ' +func + argstring + ";") + ofile.write("\n\t" +pushfunc+ '(L,outvar);') + ofile.write('\n\treturn 1;') + + ofile.write('\n}\n\n') + +ofile.write('''\ +void engine_luaInit() { + engine_lua_state = luaL_newstate(); + luaL_openlibs(engine_lua_state); + +''') + +for func in functions: + funcnew = "engine_luaf_" +func.replace("engine_","",1) + ofile.write('\tlua_pushcfunction(engine_lua_state,' +funcnew+ ');\n') + ofile.write('\tlua_setglobal (engine_lua_state,"' +func+ '");\n') + +ofile.write('''\ + + luaL_loadfile(engine_lua_state,"assets/scripts/main.lua"); + lua_call(engine_lua_state,0,0); +}''') diff --git a/modules/engine/FUNCTIONS.toml b/modules/engine/FUNCTIONS.toml new file mode 100644 index 0000000..b0874f7 --- /dev/null +++ b/modules/engine/FUNCTIONS.toml @@ -0,0 +1,57 @@ +# WINDOW +[engine_window_init] +type = "void" +arguments = ["int","int","char *"] + +[engine_window_present] +type = "void" +arguments = [] + +# SURFACE +[engine_surface_color_set] +type = "void" +arguments = ["char","char","char","char"] + +[engine_surface_draw_pixel] +type = "void" +arguments = ["int","int"] + +# TIME +[engine_time_get] +type = "long long" +arguments = [] + +[engine_time_sleep] +type = "void" +arguments = ["long long"] + +# LOGIC +# TODO: Make this into a pointer +#[engine_event_get] +#type = "struct ENGINE_EVENT" +#arguments = [] + +# TEXTURES +[engine_texture_create] +type = "struct ENGINE_TEXTURE *" +arguments = ["int","int"] + +[engine_texture_color_set] +type = "void" +arguments = ["char","char","char","char"] + +[engine_texture_draw_pixel] +type = "void" +arguments = ["struct ENGINE_TEXTURE *","int","int"] + +[engine_texture_destroy] +type = "void" +arguments = ["struct ENGINE_TEXTURE *"] + +[engine_texture_render_2d] +type = "void" +arguments = ["struct ENGINE_TEXTURE *","int","int"] + +[engine_texture_from_file] +type = "void" +arguments = ["struct ENGINE_TEXTURE *","char *"] diff --git a/modules/engine/addon/lua.c b/modules/engine/addon/lua.c index e7d18b2..5f670f6 100644 --- a/modules/engine/addon/lua.c +++ b/modules/engine/addon/lua.c @@ -3,60 +3,115 @@ #include lua_State * engine_lua_state; +int engine_luaf_window_init(lua_State *L) { + int invar1 = luaL_checkinteger(L,1); + int invar2 = luaL_checkinteger(L,2); + char * invar3 = (char *)luaL_checkstring(L,3); + engine_window_init(invar1,invar2,invar3); + return 0; +} + +int engine_luaf_window_present(lua_State *L) { + engine_window_present(); + return 0; +} + +int engine_luaf_surface_color_set(lua_State *L) { + char invar1 = luaL_checkinteger(L,1); + char invar2 = luaL_checkinteger(L,2); + char invar3 = luaL_checkinteger(L,3); + char invar4 = luaL_checkinteger(L,4); + engine_surface_color_set(invar1,invar2,invar3,invar4); + return 0; +} + +int engine_luaf_surface_draw_pixel(lua_State *L) { + int invar1 = luaL_checkinteger(L,1); + int invar2 = luaL_checkinteger(L,2); + engine_surface_draw_pixel(invar1,invar2); + return 0; +} -// TIME int engine_luaf_time_get(lua_State *L) { - lua_pushinteger(L,engine_time_get()); + long long outvar = engine_time_get(); + lua_pushinteger(L,outvar); return 1; } + int engine_luaf_time_sleep(lua_State *L) { - int time = luaL_checkinteger(L,1); - engine_time_sleep(time); + long long invar1 = luaL_checkinteger(L,1); + engine_time_sleep(invar1); return 0; } - -// TEXTURES int engine_luaf_texture_create(lua_State *L) { - int width = luaL_checkinteger(L,1); - int height = luaL_checkinteger(L,2); - struct ENGINE_TEXTURE * texture = engine_texture_create(width,height); - lua_pushlightuserdata(L,texture); + int invar1 = luaL_checkinteger(L,1); + int invar2 = luaL_checkinteger(L,2); + struct ENGINE_TEXTURE * outvar = engine_texture_create(invar1,invar2); + lua_pushlightuserdata(L,outvar); return 1; } + +int engine_luaf_texture_color_set(lua_State *L) { + char invar1 = luaL_checkinteger(L,1); + char invar2 = luaL_checkinteger(L,2); + char invar3 = luaL_checkinteger(L,3); + char invar4 = luaL_checkinteger(L,4); + engine_texture_color_set(invar1,invar2,invar3,invar4); + return 0; +} + +int engine_luaf_texture_draw_pixel(lua_State *L) { + struct ENGINE_TEXTURE * invar1 = lua_touserdata(L,1); + int invar2 = luaL_checkinteger(L,2); + int invar3 = luaL_checkinteger(L,3); + engine_texture_draw_pixel(invar1,invar2,invar3); + return 0; +} + int engine_luaf_texture_destroy(lua_State *L) { - struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1); - engine_texture_destroy(texture); + struct ENGINE_TEXTURE * invar1 = lua_touserdata(L,1); + engine_texture_destroy(invar1); return 0; } + int engine_luaf_texture_render_2d(lua_State *L) { - struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1); - int x = luaL_checkinteger(L,2); - int y = luaL_checkinteger(L,3); - engine_texture_render_2d(texture,x,y); + struct ENGINE_TEXTURE * invar1 = lua_touserdata(L,1); + int invar2 = luaL_checkinteger(L,2); + int invar3 = luaL_checkinteger(L,3); + engine_texture_render_2d(invar1,invar2,invar3); return 0; } + int engine_luaf_texture_from_file(lua_State *L) { - struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1); - char * fpath = (char *)luaL_checkstring(L,2); - engine_texture_from_file(texture,fpath); + struct ENGINE_TEXTURE * invar1 = lua_touserdata(L,1); + char * invar2 = (char *)luaL_checkstring(L,2); + engine_texture_from_file(invar1,invar2); return 0; } void engine_luaInit() { engine_lua_state = luaL_newstate(); luaL_openlibs(engine_lua_state); - - // // FUNCTIONS - // TIME + + lua_pushcfunction(engine_lua_state,engine_luaf_window_init); + lua_setglobal (engine_lua_state,"engine_window_init"); + lua_pushcfunction(engine_lua_state,engine_luaf_window_present); + lua_setglobal (engine_lua_state,"engine_window_present"); + lua_pushcfunction(engine_lua_state,engine_luaf_surface_color_set); + lua_setglobal (engine_lua_state,"engine_surface_color_set"); + lua_pushcfunction(engine_lua_state,engine_luaf_surface_draw_pixel); + lua_setglobal (engine_lua_state,"engine_surface_draw_pixel"); lua_pushcfunction(engine_lua_state,engine_luaf_time_get); lua_setglobal (engine_lua_state,"engine_time_get"); lua_pushcfunction(engine_lua_state,engine_luaf_time_sleep); lua_setglobal (engine_lua_state,"engine_time_sleep"); - - // TEXTURES lua_pushcfunction(engine_lua_state,engine_luaf_texture_create); lua_setglobal (engine_lua_state,"engine_texture_create"); + lua_pushcfunction(engine_lua_state,engine_luaf_texture_color_set); + lua_setglobal (engine_lua_state,"engine_texture_color_set"); + lua_pushcfunction(engine_lua_state,engine_luaf_texture_draw_pixel); + lua_setglobal (engine_lua_state,"engine_texture_draw_pixel"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_destroy); lua_setglobal (engine_lua_state,"engine_texture_destroy"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_render_2d); @@ -64,7 +119,6 @@ void engine_luaInit() { lua_pushcfunction(engine_lua_state,engine_luaf_texture_from_file); lua_setglobal (engine_lua_state,"engine_texture_from_file"); - // // USER SCRIPT luaL_loadfile(engine_lua_state,"assets/scripts/main.lua"); lua_call(engine_lua_state,0,0); -} +} \ No newline at end of file diff --git a/run b/run index 4f1f285..7e2c4a0 100755 --- a/run +++ b/run @@ -1,4 +1,5 @@ #!/usr/bin/env bash set -e gcc main.c -g -lSDL2 -llua5.3 -o engine -O3 -Werror -Wall +./lua_translate exec ./engine