PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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