Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.56 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /** lg2d PhysicalWorld class.
  2.  * Represents a "world" in which all physical objects exist. Used internally by
  3.  * the Scene class.
  4.  */
  5. #include <Box2D/Box2D.h>
  6.  
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10.  
  11. #include <lua5.1/lua.h>
  12. #include <lua5.1/lauxlib.h>
  13. #include <lua5.1/lualib.h>
  14. #include <string.h>
  15.  
  16. //Prefix of all Lua registry table keys to prevent collision with libraries
  17. #define __s(x) _s(x)
  18. #define _s(x) #x
  19. #define LUA_REGISTRY_PREFIX "LG2D" LUANAME ":"
  20.  
  21. /* Convenient macros. */
  22. /* see http://tinyurl.com/42mood if you don't understand while(0). */
  23. #define EXPORT_FUNC(f) {#f, Lua_PhysicalWorld_##f}
  24. #define LUA_FUNC_NAME(f) Lua_PhysicalWorld_##f
  25. #define LUA_FUNC(f) static int Lua_PhysicalWorld_##f(lua_State *Lua)
  26. #define GET_SELF() PhysicalWorld *self = \
  27.                 (PhysicalWorld*)((PhysicalWorld **)lua_touserdata(Lua, 1))[0]
  28. #define GET_PARENT() lua_pushstring(Lua, "parent");\
  29.         lua_rawget(Lua, 1);\
  30.         PhysicalWorld *parent = (PhysicalWorld*)lua_touserdata(Lua, -1);\
  31.         lua_pop(Lua, 1);
  32.  
  33. #define LUA_PUSH_TABLE_VALUE(luastate, keytype, key, valtype, val) do {\
  34.         lua_push##keytype((luastate), (key));\
  35.         lua_push##valtype((luastate), (val));\
  36.         lua_settable((luastate), -3);\
  37. } while(0)
  38.  
  39. #define LUA_PUSH_NAMED_OBJECT_FUNC(luastate, funcname, obj, name) do {\
  40.         lua_pushstring((luastate), (#funcname));\
  41.         lua_pushcfunction((luastate), (Lua_##obj##_##name));\
  42.         lua_settable((luastate), -3);\
  43. } while(0)
  44.  
  45. #define LUA_RETURN_ERRMSG(luastate, message) do {\
  46.         /* DebugOut(DO_ALL, "%s:%d: LUA_PUSH_ERRMSG: %s\n", __FILE__, __LINE__, (message));\ */\
  47.         lua_pushnil((luastate));\
  48.         lua_pushstring((luastate), (message));\
  49.         return 2;\
  50. } while(0)
  51.  
  52. #define LUA_RETURN_ERRMSGF(luastate, ...) do {\
  53.         lua_pushnil((luastate));\
  54.         lua_pushfstring((luastate), __VA_ARGS__);\
  55.         return 2;\
  56. } while(0)
  57.  
  58. #define LUA_RETURN_NIL(luastate) do {\
  59.         lua_pushnil(luastate);\
  60.         return 1;\
  61. } while(0)
  62.  
  63. /*
  64. #ifdef __cplusplus
  65. }
  66. #endif */
  67.  
  68. class PhysicalWorld {
  69.         public:
  70.                 static void GetRefsTable(lua_State *Lua);
  71.                
  72.                 PhysicalWorld(lua_State *Lua);
  73.                 ~PhysicalWorld();
  74.                
  75.                 //Lua metamethods
  76.                 int __index(lua_State *Lua);
  77.                
  78.                 //variables accessible to Lua. note that each instance also functions
  79.                 //like a table; arbitrary values can be stored in it.
  80.        
  81.         protected:
  82.                 lua_State *Lua;
  83.                 b2World *World;
  84.        
  85.         private:
  86.                
  87. };
  88.  
  89. /* #ifdef __cplusplus
  90. extern "C" {
  91. #endif */
  92.  
  93. /** Function prototypes.
  94.  */
  95. LUA_FUNC(Create);
  96. LUA_FUNC(Meta_index);
  97. LUA_FUNC(Meta_newindex);
  98. LUA_FUNC(Meta_gc);
  99.  
  100. LUALIB_API int LUAOPENFUNC (lua_State *Lua);
  101.  
  102. #ifdef __cplusplus
  103. }
  104. #endif