/TGame/TCommon/Scene/SceneManagerLua.cpp

http://awoe.googlecode.com/ · C++ · 121 lines · 100 code · 19 blank · 2 comment · 9 complexity · 6239139e127ccf1e29260b12a9289658 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "SceneLua.h"
  3. #include "SceneManagerLua.h"
  4. namespace SceneManagerLua
  5. {
  6. bool push(lua_State* L, ISceneManager* pSceneMgr)
  7. {
  8. ISceneManager** pp = (ISceneManager**)lua_newuserdata(L, sizeof(ISceneManager*));
  9. *pp = pSceneMgr;
  10. luaL_getmetatable(L, "woe.scenemgr");
  11. if (lua_istable(L, -1))
  12. {
  13. lua_setmetatable(L, -2);
  14. return true;
  15. }
  16. else
  17. {
  18. LOG_ERROR("Failed to push IScene to lua, metatable not found!");
  19. return false;
  20. }
  21. }
  22. ISceneManager* getUserData(lua_State* L)
  23. {
  24. ISceneManager**pp = (ISceneManager**)luaL_checkudata(L, 1, "woe.scenemgr");
  25. if (pp==NULL)
  26. {
  27. return NULL;
  28. }
  29. else
  30. {
  31. return *pp;
  32. }
  33. }
  34. int getScene(lua_State* L)
  35. {
  36. ISceneManager* pSceneMgr = getUserData(L);
  37. if (pSceneMgr)
  38. {
  39. int iid = luaL_checkint(L, 2);
  40. IScene* pScene = pSceneMgr->getScene(iid);
  41. if (pScene)
  42. {
  43. SceneLua::push(L, pScene);
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }
  49. int addScene(lua_State* L)
  50. {
  51. if (lua_gettop(L)<5)
  52. {
  53. return 0;
  54. }
  55. ISceneManager* pSceneMgr = getUserData(L);
  56. if (pSceneMgr)
  57. {
  58. int cid = 0xff & luaL_checkint(L, 2);
  59. int did = 0xff & luaL_checkint(L, 3);
  60. int sid = 0xff & luaL_checkint(L, 4);
  61. short ntype = short(luaL_checkint(L, 5));
  62. int nStaticID = (cid<<24) | (did<<16) | (sid<<8);
  63. if (nStaticID!=0)
  64. {
  65. IScene* pScene = pSceneMgr->addScene(nStaticID, ntype);
  66. if (pScene)
  67. {
  68. SceneLua::push(L, pScene);
  69. return 1;
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75. int rmvScene(lua_State* L)
  76. {
  77. ISceneManager* pSceneMgr = getUserData(L);
  78. if (pSceneMgr)
  79. {
  80. int iid = luaL_checkint(L, 2);
  81. pSceneMgr->rmvScene(iid);
  82. }
  83. return 0;
  84. }
  85. static const luaL_Reg scene_mgr_funcs[] = {
  86. {"getScene", getScene},
  87. {"addScene", addScene},
  88. {"rmvScene", rmvScene},
  89. {NULL, NULL}
  90. };
  91. int libaray(lua_State* L)
  92. {
  93. luaL_newmetatable(L, "woe.scenemgr");
  94. // fill member list into metatable
  95. luaL_register(L, NULL, scene_mgr_funcs);
  96. // metatable.__index = metatable
  97. lua_pushstring(L, "__index");
  98. lua_pushvalue(L, -2);
  99. lua_settable(L, -3);
  100. return 0;
  101. }
  102. } // end of scene lua