Original post: October 18, 2006
This CPP macro allows me to take a #define’d constant and register it with Lua.
/* leet hax but remember to always pass a null buffer! */
#define RegisterConstant(L,buffer,c) \
{ \
buffer = malloc(strlen(#c “=”)+(sizeof(int)/2*3) + 1); \
sprintf(buffer,#c “=%i”,c); \
luaL_loadstring(L,buffer); \
lua_pcall(L,0,-1,0); \
free(buffer); buffer = NULL; \
}
I’ve used this with allegro by processing keyboard.h through a script and throwing the output file in a..would you call it a singleton header? Probably not.
cat /usr/include/allegro/keyboard.h | grep “\#define KEY_” | awk ‘{print “RegisterConstant(L,buf,” $1 “);”}’ >> constants.h
—-
cat constants.h | tail -n 4
RegisterConstant(L,buf,KEY_SCRLOCK);
RegisterConstant(L,buf,KEY_NUMLOCK);
RegisterConstant(L,buf,KEY_CAPSLOCK);
RegisterConstant(L,buf,KEY_MAX);
Now KEY_MAX, KEY_SCRLOCK, and everything else in constants.h are valid variables in Lua. I just #include “constants.h” in a lua setup function (where L and buf are declared).
My Main main(), which will probably replace the main in breakout tutorial when I rewrite it =\:
int main(int argc,char * argv[]) /*{+{*/ {
do(g_status = g_loops[g_status]());
while(g_status LESS_THAN STATUS_EXIT_LOOP);
return(gshutdown());
}END_OF_MAIN()/*}_}*/