From 4769de179717a935d008caaaa224530b83c33c48 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sun, 14 May 2023 18:29:55 +0200 Subject: [PATCH] Update Lua implementation --- assets/scripts/main.lua | 5 ++-- modules/engine/addon/lua.c | 59 +++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/assets/scripts/main.lua b/assets/scripts/main.lua index 48e1012..c402243 100644 --- a/assets/scripts/main.lua +++ b/assets/scripts/main.lua @@ -1,6 +1,7 @@ -texture = engine_loadTexture("assets/textures/fier0.rgba",8,32) +texture = engine_texture_create(8,32) +engine_texture_from_file(texture,"assets/textures/fier0.rgba") a = 0 function engine_onFrame() - engine_renderTexture2D(texture,math.random(-8,102),math.random(-8,72)) + engine_texture_render_2d(texture,math.random(-8,102),math.random(-8,72)) end diff --git a/modules/engine/addon/lua.c b/modules/engine/addon/lua.c index 147a428..606f6cc 100644 --- a/modules/engine/addon/lua.c +++ b/modules/engine/addon/lua.c @@ -4,48 +4,67 @@ lua_State * engine_lua_state; -int engine_luafSleep(lua_State *L) { +// TIME +int engine_luaf_time_get(lua_State *L) { + lua_pushinteger(L,engine_time_get()); + return 1; +} +int engine_luaf_time_sleep(lua_State *L) { int time = luaL_checkinteger(L,1); engine_time_sleep(time); return 0; } -int engine_luafTimeGet(lua_State *L) { - lua_pushinteger(L,engine_time_get()); - return 1; -} -int engine_luafLoadTexture(lua_State *L) { - char * fpath = (char *)luaL_checkstring(L,1); - int width = luaL_checkinteger(L,2); - int height = luaL_checkinteger(L,3); +// 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_createTexture(width,height); - engine_texture_from_file(texture,fpath); lua_pushlightuserdata(L,texture); return 1; } - -int engine_luafRenderTexture2D(lua_State *L) { +int engine_luaf_texture_destroy(lua_State *L) { + struct ENGINE_TEXTURE * texture = (struct ENGINE_TEXTURE *)lua_touserdata(L,1); + engine_texture_destroy(texture); + 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); 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); + return 0; +} void engine_luaInit() { engine_lua_state = luaL_newstate(); luaL_openlibs(engine_lua_state); - // Functions - lua_pushcfunction(engine_lua_state,engine_luafSleep); - lua_setglobal (engine_lua_state,"engine_sleep"); - lua_pushcfunction(engine_lua_state,engine_luafLoadTexture); - lua_setglobal (engine_lua_state,"engine_loadTexture"); - lua_pushcfunction(engine_lua_state,engine_luafRenderTexture2D); - lua_setglobal (engine_lua_state,"engine_renderTexture2D"); + // // FUNCTIONS + // TIME + 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"); - // Start user script + // 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_destroy); + lua_setglobal (engine_lua_state,"engine_texture_destroy"); + lua_pushcfunction(engine_lua_state,engine_luaf_texture_render_2d); + lua_setglobal (engine_lua_state,"engine_texture_render_2d"); + 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); }