diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-24 18:53:50 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-05-24 18:53:50 (GMT) |
| commit | 8196d2a645d386b5c97134c446e959e5e50a345b (patch) | |
| tree | 33e729715a6122fa5b8dac6f9abf7b784bfbfbc3 /src/luaconsole.c | |
| parent | 5a117c2d27f513942d7621980a79d357af122ba4 (diff) | |
| parent | 1fb778724329782bb5616da032742dce4dbea131 (diff) | |
| download | powder-8196d2a645d386b5c97134c446e959e5e50a345b.zip powder-8196d2a645d386b5c97134c446e959e5e50a345b.tar.gz | |
Merge pull request #66 from jacob1/Modstuff
Code structure improvements for graphics/drawing, improvements to prevent accidental infinite loops in Lua, Fixes for fusion, improvements for VOID types. And other minor improvements
Diffstat (limited to 'src/luaconsole.c')
| -rw-r--r-- | src/luaconsole.c | 140 |
1 files changed, 105 insertions, 35 deletions
diff --git a/src/luaconsole.c b/src/luaconsole.c index 759dd75..d32147c 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,12 +214,15 @@ 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); } #ifndef FFI int luacon_partread(lua_State* l){ @@ -248,11 +252,11 @@ int luacon_partread(lua_State* l){ switch(format) { case 0: - tempinteger = *((int*)(((void*)&parts[i])+offset)); + tempinteger = *((int*)(((char*)&parts[i])+offset)); lua_pushnumber(l, tempinteger); break; case 1: - tempfloat = *((float*)(((void*)&parts[i])+offset)); + tempfloat = *((float*)(((char*)&parts[i])+offset)); lua_pushnumber(l, tempfloat); break; } @@ -280,10 +284,10 @@ int luacon_partwrite(lua_State* l){ switch(format) { case 0: - *((int*)(((void*)&parts[i])+offset)) = luaL_optinteger(l, 3, 0); + *((int*)(((char*)&parts[i])+offset)) = luaL_optinteger(l, 3, 0); break; case 1: - *((float*)(((void*)&parts[i])+offset)) = luaL_optnumber(l, 3, 0); + *((float*)(((char*)&parts[i])+offset)) = luaL_optnumber(l, 3, 0); break; } return 1; @@ -410,11 +414,11 @@ int luacon_transitionread(lua_State* l){ switch(format) { case 0: - tempinteger = *((int*)(((void*)&ptransitions[i])+offset)); + tempinteger = *((int*)(((char*)&ptransitions[i])+offset)); lua_pushnumber(l, tempinteger); break; case 1: - tempfloat = *((float*)(((void*)&ptransitions[i])+offset)); + tempfloat = *((float*)(((char*)&ptransitions[i])+offset)); lua_pushnumber(l, tempfloat); break; } @@ -444,10 +448,10 @@ int luacon_transitionwrite(lua_State* l){ switch(format) { case 0: - *((int*)(((void*)&ptransitions[i])+offset)) = luaL_optinteger(l, 3, 0); + *((int*)(((char*)&ptransitions[i])+offset)) = luaL_optinteger(l, 3, 0); break; case 1: - *((float*)(((void*)&ptransitions[i])+offset)) = luaL_optnumber(l, 3, 0); + *((float*)(((char*)&ptransitions[i])+offset)) = luaL_optnumber(l, 3, 0); break; } return 0; @@ -597,19 +601,19 @@ int luacon_elementread(lua_State* l){ switch(format) { case 0: - tempinteger = *((int*)(((void*)&ptypes[i])+offset)); + tempinteger = *((int*)(((char*)&ptypes[i])+offset)); lua_pushnumber(l, tempinteger); break; case 1: - tempfloat = *((float*)(((void*)&ptypes[i])+offset)); + tempfloat = *((float*)(((char*)&ptypes[i])+offset)); lua_pushnumber(l, tempfloat); break; case 2: - tempstring = *((char**)(((void*)&ptypes[i])+offset)); + tempstring = *((char**)(((char*)&ptypes[i])+offset)); lua_pushstring(l, tempstring); break; case 3: - tempinteger = *((unsigned char*)(((void*)&ptypes[i])+offset)); + tempinteger = *((unsigned char*)(((char*)&ptypes[i])+offset)); lua_pushnumber(l, tempinteger); break; } @@ -641,10 +645,10 @@ int luacon_elementwrite(lua_State* l){ switch(format) { case 0: - *((int*)(((void*)&ptypes[i])+offset)) = luaL_optinteger(l, 3, 0); + *((int*)(((char*)&ptypes[i])+offset)) = luaL_optinteger(l, 3, 0); break; case 1: - *((float*)(((void*)&ptypes[i])+offset)) = luaL_optnumber(l, 3, 0); + *((float*)(((char*)&ptypes[i])+offset)) = luaL_optnumber(l, 3, 0); break; case 2: tempstring = mystrdup(luaL_optstring(l, 3, "")); @@ -667,11 +671,11 @@ int luacon_elementwrite(lua_State* l){ return luaL_error(l, "Name in use"); } } - *((char**)(((void*)&ptypes[i])+offset)) = tempstring; + *((char**)(((char*)&ptypes[i])+offset)) = tempstring; //Need some way of cleaning up previous values break; case 3: - *((unsigned char*)(((void*)&ptypes[i])+offset)) = luaL_optinteger(l, 3, 0); + *((unsigned char*)(((char*)&ptypes[i])+offset)) = luaL_optinteger(l, 3, 0); break; } if (modified_stuff) @@ -733,26 +737,39 @@ int luacon_step(int mx, int my, int selectl, int selectr){ lua_setfield(l, tptProperties, "mousey"); lua_setfield(l, tptProperties, "selectedl"); lua_setfield(l, tptProperties, "selectedr"); - if(step_functions[0]){ - //Set mouse globals - for(i = 0; i<6; i++){ - if(step_functions[i]){ - lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); - callret = lua_pcall(l, 0, 0, 0); - if (callret) + for(i = 0; i<6; i++){ + if(step_functions[i]){ + loop_time = SDL_GetTicks(); + lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); + callret = lua_pcall(l, 0, 0, 0); + if (callret) + { + // failed, TODO: better error reporting + printf("%s\n",luacon_geterror()); + if (!strcmp(luacon_geterror(),"Error: Infinite loop")) { - // failed, TODO: better error reporting - printf("%s\n",luacon_geterror()); + lua_pushcfunction(l,&luatpt_unregister_step); + lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); + lua_pcall(l, 1, 0, 0); } } } - return tempret; } return 0; } int luacon_eval(char *command){ + loop_time = SDL_GetTicks(); return luaL_dostring (l, command); } +void lua_hook(lua_State *L, lua_Debug *ar) +{ + if(ar->event == LUA_HOOKCOUNT && SDL_GetTicks()-loop_time > 3000) + { + if (confirm_ui(vid_buf,"Infinite Loop","The Lua code might have an infinite loop. Press OK to stop it","OK")) + luaL_error(l,"Error: Infinite loop"); + loop_time = SDL_GetTicks(); + } +} int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt) { int retval = 0; @@ -771,6 +788,29 @@ 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 *pixel_mode, int *cola, int *colr, int *colg, int *colb, int *firea, int *firer, int *fireg, int *fireb) +{ + int cache = 0; + lua_rawgeti(l, LUA_REGISTRYINDEX, lua_gr_func[t]); + lua_pushinteger(l, i); + lua_pushinteger(l, *colr); + lua_pushinteger(l, *colg); + lua_pushinteger(l, *colb); + lua_pcall(l, 4, 10, 0); + + cache = luaL_optint(l, 2, 0); + *pixel_mode = luaL_optint(l, 3, *pixel_mode); + *cola = luaL_optint(l, 4, *cola); + *colr = luaL_optint(l, 5, *colr); + *colg = luaL_optint(l, 6, *colg); + *colb = luaL_optint(l, 7, *colb); + *firea = luaL_optint(l, 8, *firea); + *firer = luaL_optint(l, 9, *firer); + *fireg = luaL_optint(l, 10, *fireg); + *fireb = luaL_optint(l, 11, *fireb); + lua_pop(l, 10); + return cache; +} char *luacon_geterror(){ char *error = lua_tostring(l, -1); if(error==NULL || !error[0]){ @@ -855,6 +895,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 = ""; @@ -1075,8 +1138,13 @@ int luatpt_reset_spark(lua_State* l) { if (parts[i].type==PT_SPRK) { - parts[i].type = parts[i].ctype; - parts[i].life = 4; + if (parts[i].ctype >= 0 && parts[i].ctype < PT_NUM) + { + parts[i].type = parts[i].ctype; + parts[i].life = 0; + } + else + kill_part(i); } } return 0; @@ -1270,7 +1338,7 @@ int luatpt_set_property(lua_State* l) } else { t = luaL_optint(l, 2, 0); } - if (format == 3 && (t<0 || t>=PT_NUM)) + if (format == 3 && (t<0 || t>=PT_NUM || !ptypes[t].enabled)) return luaL_error(l, "Unrecognised element number '%d'", t); } else { name = luaL_optstring(l, 2, "dust"); @@ -1462,9 +1530,9 @@ int luatpt_drawrect(lua_State* l) if (x<0 || y<0 || x>=XRES+BARSIZE || y>=YRES+MENUSIZE) return luaL_error(l, "Screen coordinates out of range (%d,%d)", x, y); if(x+w > XRES+BARSIZE) - w = XRES-x; + w = XRES+BARSIZE-x; if(y+h > YRES+MENUSIZE) - h = YRES-y; + h = YRES+MENUSIZE-y; if (r<0) r = 0; if (r>255) r = 255; if (g<0) g = 0; @@ -1496,9 +1564,9 @@ int luatpt_fillrect(lua_State* l) if (x<0 || y<0 || x>=XRES+BARSIZE || y>=YRES+MENUSIZE) return luaL_error(l, "Screen coordinates out of range (%d,%d)", x, y); if(x+w > XRES+BARSIZE) - w = XRES-x; + w = XRES+BARSIZE-x; if(y+h > YRES+MENUSIZE) - h = YRES-y; + h = YRES+MENUSIZE-y; if (r<0) r = 0; if (r>255) r = 255; if (g<0) g = 0; @@ -1893,6 +1961,8 @@ int luatpt_setdebug(lua_State* l) int luatpt_setfpscap(lua_State* l) { int fpscap = luaL_optint(l, 1, 0); + if (fpscap < 2) + return luaL_error(l, "fps cap too small"); limitFPS = fpscap; return 0; } |
