diff --git a/lua/cocos2dx_support/CCLuaEngine.cpp b/lua/cocos2dx_support/CCLuaEngine.cpp index e864711cb6..f98232e4b5 100644 --- a/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/lua/cocos2dx_support/CCLuaEngine.cpp @@ -1,28 +1,28 @@ /**************************************************************************** -Copyright (c) 2011 cocos2d-x.org + Copyright (c) 2011 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "LuaEngine.h" +#include "CCLuaEngine.h" #include "tolua++.h" #include "tolua_fix.h" @@ -35,13 +35,56 @@ extern "C" { #include "LuaCocos2d.h" #include "LuaSimpleAudioEngine.h" #include "LuaGameInterfaces.h" +#include "CCArray.h" +#include "CCTimer.h" -using namespace cocos2d; +namespace cocos2d +{ + +CCSchedulerFuncEntry* CCSchedulerFuncEntry::entryWithFunctionRefID(int functionRefID, ccTime fInterval, bool bPaused) +{ + CCSchedulerFuncEntry* entry = new CCSchedulerFuncEntry(); + entry->initWithFunctionRefID(functionRefID, fInterval, bPaused); + entry->autorelease(); + return entry; +} + +bool CCSchedulerFuncEntry::initWithFunctionRefID(int functionRefID, ccTime fInterval, bool bPaused) +{ + m_timer = new CCTimer(); + m_timer->initWithScriptFunc(functionRefID, fInterval); + m_timer->autorelease(); + m_timer->retain(); + m_functionRefID = functionRefID; + m_paused = bPaused; + CCLOG("[LUA] ADD function refID: %04d, add schedule entryID: %d", m_functionRefID, m_entryID); + return true; +} + +CCSchedulerFuncEntry::CCSchedulerFuncEntry(void) +: m_timer(NULL) +, m_functionRefID(0) +, m_paused(true) +, m_isMarkDeleted(false) +{ + static int entryIDCount = 0; + ++entryIDCount; + m_entryID = entryIDCount; +} + +CCSchedulerFuncEntry::~CCSchedulerFuncEntry(void) +{ + m_timer->release(); + CCLuaEngine::sharedEngine()->removeLuaFunctionRef(m_functionRefID); + CCLOG("[LUA] DEL function refID: %04d, remove schedule entryID: %d", m_functionRefID, m_entryID); +} + +// ---------------------------- -LuaEngine* LuaEngine::s_engine = NULL; +CCLuaEngine* CCLuaEngine::s_engine = NULL; -LuaEngine::LuaEngine() +CCLuaEngine::CCLuaEngine() { m_state = lua_open(); luaL_openlibs(m_state); @@ -52,40 +95,39 @@ LuaEngine::LuaEngine() luax_loadexts(m_state); } -LuaEngine::~LuaEngine() +CCLuaEngine::~CCLuaEngine() { lua_close(m_state); s_engine = NULL; } -LuaEngine* LuaEngine::sharedEngine() +CCLuaEngine* CCLuaEngine::sharedEngine() { if (!s_engine) { - s_engine = new LuaEngine(); + s_engine = new CCLuaEngine(); } return s_engine; } -void LuaEngine::purgeSharedEngine() +void CCLuaEngine::purgeSharedEngine() { if (s_engine) delete s_engine; } // ------------------------------------------- -void LuaEngine::removeCCObject(CCObject *object) +void CCLuaEngine::removeCCObject(CCObject *object) { tolua_remove_ccobject_by_refid(m_state, object->m_refID); } -void LuaEngine::removeFunctionByRefId(int refid) +void CCLuaEngine::removeLuaFunctionRef(int functionRefID) { -// CCLOG("LuaEngine::removeFunctionByRefId() - refid: %d", refid); - tolua_remove_function_by_refid(m_state, refid); + tolua_remove_function_by_refid(m_state, functionRefID); } -void LuaEngine::addSearchPath(const char* path) +void CCLuaEngine::addSearchPath(const char* path) { lua_getglobal(m_state, "package"); /* stack: package */ lua_getfield(m_state, -1, "path"); /* get package.path, stack: package path */ @@ -96,21 +138,21 @@ void LuaEngine::addSearchPath(const char* path) lua_pop(m_state, 1); /* stack: - */ } -bool LuaEngine::executeScriptFile(const char* filename) +int CCLuaEngine::executeScriptFile(const char* filename) { int nRet = luaL_dofile(m_state, filename); - lua_gc(m_state, LUA_GCCOLLECT, 0); +// lua_gc(m_state, LUA_GCCOLLECT, 0); if (nRet != 0) { CCLOG("[LUA ERROR] %s", lua_tostring(m_state, -1)); lua_pop(m_state, 1); - return false; + return nRet; } - return true; + return 0; } -int LuaEngine::executeGlobalFunction(const char* function_name) +int CCLuaEngine::executeGlobalFunction(const char* function_name) { lua_getglobal(m_state, function_name); /* query function by name, stack: function */ if (!lua_isfunction(m_state, -1)) @@ -121,7 +163,7 @@ int LuaEngine::executeGlobalFunction(const char* function_name) } int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */ - lua_gc(m_state, LUA_GCCOLLECT, 0); +// lua_gc(m_state, LUA_GCCOLLECT, 0); if (error) { @@ -133,116 +175,76 @@ int LuaEngine::executeGlobalFunction(const char* function_name) // get return value if (!lua_isnumber(m_state, -1)) { - CCLOG("[LUA ERROR] '%s' return value is not a number", function_name); lua_pop(m_state, 1); return 0; } - + int ret = lua_tointeger(m_state, -1); lua_pop(m_state, 1); /* stack: - */ return ret; } -int LuaEngine::executeFunctionByRefId(int functionRefId) +int CCLuaEngine::executeFunctionByRefID(int functionRefId, int numArgs) { - lua_rawgeti(m_state, LUA_REGISTRYINDEX, functionRefId); /* stack: function */ + lua_pushstring(m_state, TOLUA_REFID_FUNC_MAPPING); + lua_rawget(m_state, LUA_REGISTRYINDEX); /* stack: refid_func */ + lua_pushinteger(m_state, functionRefId); /* stack: refid_func refid */ + lua_rawget(m_state, -2); /* stack: refid_func func */ + if (!lua_isfunction(m_state, -1)) { CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", functionRefId); lua_pop(m_state, 1); return 0; } - - int error = lua_pcall(m_state, 0, 1, 0); /* stack: ret */ + + if (numArgs > 0) + { + int lo = -2 - numArgs; + while (lo <= -3) + { + tolua_pushvalue(m_state, lo); /* stack: refid_func func (...) */ + ++lo; + } + } + + int error = lua_pcall(m_state, numArgs, 1, 0); /* stack: refid_func ret */ if (error) { CCLOG("[LUA ERROR] %s", lua_tostring(m_state, - 1)); - lua_pop(m_state, 1); // clean error message + lua_pop(m_state, 2); // clean error message return 0; } // get return value if (!lua_isnumber(m_state, -1)) { - CCLOG("[LUA ERROR] function '%d' return value is not a number", functionRefId); - lua_pop(m_state, 1); + lua_pop(m_state, 2); return 0; } int ret = lua_tointeger(m_state, -1); + lua_pop(m_state, 2); + return ret; +} + +// functions for excute touch event +int CCLuaEngine::executeTouchEvent(int functionRefId, CCTouch *pTouch) +{ + return false; +} + +int CCLuaEngine::executeTouchesEvent(int functionRefId, CCSet *pTouches) +{ + return false; +} + +int CCLuaEngine::executeSchedule(int functionRefID, ccTime dt) +{ + lua_pushnumber(m_state, dt); + int ret = executeFunctionByRefID(functionRefID, 1); lua_pop(m_state, 1); return ret; } -int LuaEngine::retainRefID(int refID) -{ - int r = ++m_refIDMap[refID]; - return r; -} - -int LuaEngine::releaseRefID(int refID) -{ - std::map::iterator it = m_refIDMap.find(refID); - if (it == m_refIDMap.end()) return 0; - - --(it->second); - if (it->second <= 0) - { - m_refIDMap.erase(it); - return 0; - } - return it->second; -} - -// functions for excute touch event -bool LuaEngine::executeTouchEvent(const char *pszFuncName, CCTouch *pTouch) -{ - return false; -} - -bool LuaEngine::executeTouchesEvent(const char *pszFuncName, CCSet *pTouches) -{ - return false; -} - -// functions for CCCallFuncX -bool LuaEngine::executeCallFunc(const char *pszFuncName) -{ - return false; -} - -bool LuaEngine::executeCallFuncN(const char *pszFuncName, CCNode *pNode) -{ - return false; -} - -bool LuaEngine::executeCallFuncND(const char *pszFuncName, CCNode *pNode, void *pData) -{ - return false; -} - -bool LuaEngine::executeCallFunc0(const char *pszFuncName, CCObject *pObject) -{ - return false; -} - -bool LuaEngine::executeSchedule(int refid, ccTime dt) -{ - lua_rawgeti(m_state, LUA_REGISTRYINDEX, refid); /* stack: function */ - if (!lua_isfunction(m_state, -1)) - { - CCLOG("[LUA ERROR] function refid %d invalid", refid); - lua_pop(m_state, 1); - return false; - } - - lua_pushnumber(m_state, dt); /* stack: function time */ - int error = lua_pcall(m_state, 1, 0, 0); /* stack: [error] */ - if (error) - { - CCLOG("[LUA ERROR] function %d, %s", refid, lua_tostring(m_state, -1)); - lua_pop(m_state, 1); - return false; - } - return true; -} +} // namespace cocos2d diff --git a/lua/cocos2dx_support/CCLuaEngine.h b/lua/cocos2dx_support/CCLuaEngine.h index 6daa2de4b5..48e350bba2 100644 --- a/lua/cocos2dx_support/CCLuaEngine.h +++ b/lua/cocos2dx_support/CCLuaEngine.h @@ -1,26 +1,26 @@ /**************************************************************************** -Copyright (c) 2011 cocos2d-x.org - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ + Copyright (c) 2011 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ #ifndef __LUA_ENGINE_H__ #define __LUA_ENGINE_H__ @@ -28,18 +28,66 @@ extern "C" { #include "lua.h" } -#include +#include "ccTypes.h" #include "CCObject.h" #include "CCTouch.h" #include "CCSet.h" #include "CCNode.h" -using namespace cocos2d; +namespace cocos2d +{ + +class CCTimer; -class LuaEngine +// Lua support for CCSchedule +class CCSchedulerFuncEntry : public CCObject { public: - ~LuaEngine(); + // functionRefID return by tolua_ref_function(), called from LuaCocos2d.cpp + static CCSchedulerFuncEntry* entryWithFunctionRefID(int functionRefID, ccTime fInterval, bool bPaused); + ~CCSchedulerFuncEntry(void); + + inline cocos2d::CCTimer* getTimer(void) { + return m_timer; + } + + inline bool isPaused(void) { + return m_paused; + } + + inline int getRefID(void) { + return m_refID; + } + + inline int getEntryID(void) { + return m_entryID; + } + + void markDeleted(void) { + m_isMarkDeleted = true; + } + + bool isMarkDeleted(void) { + return m_isMarkDeleted; + } + +private: + CCSchedulerFuncEntry(void); + bool initWithFunctionRefID(int refID, ccTime fInterval, bool bPaused); + + cocos2d::CCTimer* m_timer; + bool m_paused; + int m_functionRefID; // Lua function reference + int m_entryID; + bool m_isMarkDeleted; +}; + + +// Lua support for cocos2d-x +class CCLuaEngine +{ +public: + ~CCLuaEngine(); /** @brief Method used to get a pointer to the lua_State that the script module is attached to. @@ -48,26 +96,29 @@ public: lua_State* getLuaState(void) const { return m_state; } - + /** @brief Remove CCObject from lua state @param object to remove */ void removeCCObject(cocos2d::CCObject *object); - - void removeFunctionByRefId(int refid); - + + /** + @brief Remove Lua function reference + */ + void removeLuaFunctionRef(int functionRefID); + /** @brief Add a path to find lua files in @param path to be added to the Lua path */ void addSearchPath(const char* path); - + /** @brief Execute a script file. @param filename String object holding the filename of the script file that is to be executed */ - bool executeScriptFile(const char* filename); + int executeScriptFile(const char* filename); /** @brief Execute a scripted global function. @@ -76,42 +127,32 @@ public: @return The integer value returned from the script function. */ int executeGlobalFunction(const char* function_name); - + /** @brief Execute a function by ref id @param The function ref id + @param Number of parameters @return The integer value returned from the script function. */ - int executeFunctionByRefId(int functionRefId); - - int retainRefID(int refID); - int releaseRefID(int refID); - + int executeFunctionByRefID(int functionRefId, int numArgs = 0); + // functions for excute touch event - bool executeTouchEvent(const char *pszFuncName, cocos2d::CCTouch *pTouch); - bool executeTouchesEvent(const char *pszFuncName, cocos2d::CCSet *pTouches); - - // functions for CCCallFuncX - bool executeCallFunc(const char *pszFuncName); - bool executeCallFuncN(const char *pszFuncName, cocos2d::CCNode *pNode); - bool executeCallFuncND(const char *pszFuncName, cocos2d::CCNode *pNode, void *pData); - bool executeCallFunc0(const char *pszFuncName, cocos2d::CCObject *pObject); - - // excute a script function without params - int executeFuction(const char *pszFuncName); - + int executeTouchEvent(int functionRefId, cocos2d::CCTouch *pTouch); + int executeTouchesEvent(int functionRefId, cocos2d::CCSet *pTouches); + // execute a schedule function - bool executeSchedule(int refid, cocos2d::ccTime dt); + int executeSchedule(int functionRefID, cocos2d::ccTime dt); - static LuaEngine* sharedEngine(); + static CCLuaEngine* sharedEngine(); static void purgeSharedEngine(); - -private: - LuaEngine(); - static LuaEngine* s_engine; +private: + CCLuaEngine(); + + static CCLuaEngine* s_engine; lua_State* m_state; - std::map m_refIDMap; }; + +} // namespace cocos2d #endif // __LUA_ENGINE_H__ diff --git a/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id b/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id index 2e1d6d7ef3..be781829fe 100644 --- a/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id +++ b/lua/cocos2dx_support/LuaCocos2d.cpp.REMOVED.git-id @@ -1 +1 @@ -a46cbc125f8e16c21be5739ec2c5db286d8fa6cd \ No newline at end of file +7589c568b9a507fded4c5c3118c5bf29dc8a00e9 \ No newline at end of file