diff --git a/includes/luaconsole.h b/includes/luaconsole.h index ce8f6b1c6..4494ddb97 100644 --- a/includes/luaconsole.h +++ b/includes/luaconsole.h @@ -40,7 +40,7 @@ #define LUACON_EL_MODIFIED_GRAPHICS 0x2 #define LUACON_EL_MODIFIED_MENUS 0x4 -int *lua_el_func, *lua_el_mode; +int *lua_el_func, *lua_el_mode, *lua_gr_func; void luacon_open(); int luacon_step(int mx, int my, int selectl, int selectr); @@ -48,6 +48,7 @@ int luacon_mouseevent(int mx, int my, int mb, int event); int luacon_keyevent(int key, int modifier, int event); int luacon_eval(char *command); int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt); +int luacon_graphics_update(int t, int i); char *luacon_geterror(); void luacon_close(); int luacon_partsread(lua_State* l); @@ -70,6 +71,7 @@ int getPartIndex_curIdx; int luatpt_test(lua_State* l); int luatpt_getelement(lua_State *l); int luatpt_element_func(lua_State *l); +int luatpt_graphics_func(lua_State *l); int luatpt_drawtext(lua_State* l); int luatpt_create(lua_State* l); int luatpt_setpause(lua_State* l); diff --git a/src/graphics.c b/src/graphics.c index c47c44e55..60d23b071 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -47,6 +47,9 @@ #include #include #include "hmap.h" +#ifdef LUACONSOLE +#include +#endif #if defined(LIN32) || defined(LIN64) #include "icon.h" @@ -1854,8 +1857,17 @@ void render_parts(pixel *vid) } else if(!(colour_mode & COLOUR_BASC)) //Don't get special effects for BASIC colour mode { +#ifdef LUACONSOLE + if (lua_gr_func[t]) + { + colr = luacon_graphics_update(t,i); + } + else if (ptypes[t].graphics_func) + { +#else if (ptypes[t].graphics_func) { +#endif if ((*(ptypes[t].graphics_func))(&(parts[i]), nx, ny, &pixel_mode, &cola, &colr, &colg, &colb, &firea, &firer, &fireg, &fireb)) //That's a lot of args, a struct might be better { graphicscache[t].isready = 1; diff --git a/src/luaconsole.c b/src/luaconsole.c index e0aab5f47..46168af5a 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -96,6 +96,7 @@ void luacon_open(){ {"screenshot",&luatpt_screenshot}, {"element",&luatpt_getelement}, {"element_func",&luatpt_element_func}, + {"graphics_func",&luatpt_graphics_func}, {NULL,NULL} }; @@ -213,11 +214,13 @@ tpt.partsdata = nil"); } lua_setfield(l, tptProperties, "eltransition"); - lua_el_func = calloc(PT_NUM, sizeof(int)); - lua_el_mode = calloc(PT_NUM, sizeof(int)); + lua_el_func = (int*)calloc(PT_NUM, sizeof(int)); + lua_el_mode = (int*)calloc(PT_NUM, sizeof(int)); + lua_gr_func = (int*)calloc(PT_NUM, sizeof(int)); for(i = 0; i < PT_NUM; i++) { lua_el_mode[i] = 0; + lua_gr_func[i] = 0; } lua_sethook(l, &lua_hook, LUA_MASKCOUNT, 4000000); } @@ -785,6 +788,20 @@ int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt) } return retval; } +int luacon_graphics_update(int t, int i) +{ + int retval = 0; + if(lua_gr_func[t]){ + lua_rawgeti(l, LUA_REGISTRYINDEX, lua_gr_func[t]); + lua_pushinteger(l, i); + lua_pcall(l, 1, 1, 0); + if(lua_isnumber(l, -1)){ + retval = (int)lua_tonumber(l, -1); + } + lua_pop(l, 1); + } + return retval; +} char *luacon_geterror(){ char *error = lua_tostring(l, -1); if(error==NULL || !error[0]){ @@ -869,6 +886,29 @@ int luatpt_element_func(lua_State *l) } return 0; } +int luatpt_graphics_func(lua_State *l) +{ + if(lua_isfunction(l, 1)) + { + int element = luaL_optint(l, 2, 0); + int function; + lua_pushvalue(l, 1); + function = luaL_ref(l, LUA_REGISTRYINDEX); + if(element > 0 && element < PT_NUM) + { + lua_gr_func[element] = function; + graphicscache[element].isready = 0; + return 0; + } + else + { + return luaL_error(l, "Invalid element"); + } + } + else + return luaL_error(l, "Not a function"); + return 0; +} int luatpt_error(lua_State* l) { char *error = "";