summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2011-12-17 21:30:00 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2011-12-17 21:30:00 (GMT)
commit8ae0384f09f8f94eb9cc3f07e235114e5d589b36 (patch)
tree6060931a0e591127527fb883ec2e6bddc8565590 /src
parent03352ca1f4115eff5efac7547c3e507bce656496 (diff)
downloadpowder-8ae0384f09f8f94eb9cc3f07e235114e5d589b36.zip
powder-8ae0384f09f8f94eb9cc3f07e235114e5d589b36.tar.gz
Add option for lua parts array to use native data using ffi
Diffstat (limited to 'src')
-rw-r--r--src/luaconsole.c41
-rw-r--r--src/main.c7
2 files changed, 35 insertions, 13 deletions
diff --git a/src/luaconsole.c b/src/luaconsole.c
index 8151328..42495e7 100644
--- a/src/luaconsole.c
+++ b/src/luaconsole.c
@@ -101,16 +101,22 @@ void luacon_open(){
lua_setfield(l, tptPropertiesVersion, "build");
lua_setfield(l, tptProperties, "version");
- /*lua_newtable(l);
- tptParts = lua_gettop(l);
- lua_newtable(l);
- tptPartsMeta = lua_gettop(l);
- lua_pushcfunction(l, luacon_partswrite);
- lua_setfield(l, tptPartsMeta, "__newindex");
- lua_pushcfunction(l, luacon_partsread);
- lua_setfield(l, tptPartsMeta, "__index");
- lua_setmetatable(l, tptParts);
- lua_setfield(l, tptProperties, "parts");*/
+#ifdef FFI
+ //LuaJIT's ffi gives us direct access to parts data, no need for nested metatables. HOWEVER, this is in no way safe, it's entirely possible for someone to try to read parts[-10]
+ lua_pushlightuserdata(l, parts);
+ lua_setfield(l, tptProperties, "partsdata");
+
+ luaL_dostring (l, "ffi = require(\"ffi\")\n\
+ffi.cdef[[\n\
+typedef struct { int type; int life, ctype; float x, y, vx, vy; float temp; float pavg[2]; int flags; int tmp; int tmp2; unsigned int dcolour; } particle;\n\
+]]\n\
+tpt.parts = ffi.cast(\"particle *\", tpt.partsdata)\n\
+ffi = nil\n\
+tpt.partsdata = nil");
+ //Since ffi is REALLY REALLY dangrous, we'll remove it from the environment completely (TODO)
+
+#else
+ //This uses a lot of memory (60MB+), but very good performance
lua_newtable(l);
tptParts = lua_gettop(l);
for(i = 0; i < NPART; i++)
@@ -132,6 +138,19 @@ void luacon_open(){
}
lua_setfield(l, tptProperties, "parts");
+ //Poor performance (nested metatabled created on get/set) but good little memory usage
+ /*lua_newtable(l);
+ tptParts = lua_gettop(l);
+ lua_newtable(l);
+ tptPartsMeta = lua_gettop(l);
+ lua_pushcfunction(l, luacon_partswrite);
+ lua_setfield(l, tptPartsMeta, "__newindex");
+ lua_pushcfunction(l, luacon_partsread);
+ lua_setfield(l, tptPartsMeta, "__index");
+ lua_setmetatable(l, tptParts);
+ lua_setfield(l, tptProperties, "parts");*/
+#endif
+
lua_newtable(l);
tptElements = lua_gettop(l);
for(i = 1; i < PT_NUM; i++)
@@ -192,6 +211,7 @@ void luacon_open(){
lua_el_mode[i] = 0;
}
}
+#ifndef FFI
int luacon_partread(lua_State* l){
int format, offset;
char * tempstring;
@@ -294,6 +314,7 @@ int luacon_partsread(lua_State* l){
int luacon_partswrite(lua_State* l){
return luaL_error(l, "Not writable");
}
+#endif
int luacon_particle_getproperty(char * key, int * format)
{
int offset;
diff --git a/src/main.c b/src/main.c
index ef150ac..f732f2a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1666,9 +1666,6 @@ int main(int argc, char *argv[])
fmt.callback = mixaudio;
fmt.userdata = NULL;
-#ifdef LUACONSOLE
- luacon_open();
-#endif
#ifdef MT
numCores = core_count();
#endif
@@ -1679,6 +1676,10 @@ int main(int argc, char *argv[])
init_can_move();
clear_sim();
+#ifdef LUACONSOLE
+ luacon_open();
+#endif
+
colour_mode = COLOUR_DEFAULT;
init_display_modes();