issue #528, improve CCScriptEngineManager. AppDelegate should hold the whole lifecycle of LuaEngine object.

This commit is contained in:
Walzer 2011-06-21 17:02:08 +08:00
parent ec2553d589
commit f3375f0dbb
4 changed files with 33 additions and 14 deletions

View File

@ -1,17 +1,18 @@
#include "AppDelegate.h"
#include "LuaEngine.h"
#include "cocos2d.h"
USING_NS_CC;
AppDelegate::AppDelegate()
:m_pLuaEngine(NULL)
{
}
AppDelegate::~AppDelegate()
{
CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine();
CC_SAFE_DELETE(m_pLuaEngine);
}
bool AppDelegate::initInstance()
@ -84,10 +85,10 @@ bool AppDelegate::applicationDidFinishLaunching()
pDirector->setAnimationInterval(1.0 / 60);
// register lua engine
CCScriptEngineManager::sharedScriptEngineManager()->registerScriptEngine(new LuaEngine());
m_pLuaEngine = new LuaEngine;
CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCLog("1");
unsigned long size;
char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size);

View File

@ -2,6 +2,7 @@
#define _APP_DELEGATE_H_
#include "CCApplication.h"
#include "LuaEngine.h"
/**
@brief The cocos2d Application.
@ -37,6 +38,9 @@ public:
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
private:
LuaEngine* m_pLuaEngine;
};
#endif // _APP_DELEGATE_H_

View File

@ -65,18 +65,24 @@ public:
virtual bool executeSchedule(const char* pszFuncName, ccTime t) = 0;
};
/**
CCScriptEngineManager is a singleton which holds an object instance of CCScriptEngineProtocl
It helps cocos2d-x and the user code to find back LuaEngine object
@since v0.99.5-x-0.8.5
*/
class CC_DLL CCScriptEngineManager
{
public:
CCScriptEngineManager();
virtual ~CCScriptEngineManager();
static CCScriptEngineManager* sharedScriptEngineManager();
void registerScriptEngine(CCScriptEngineProtocol *pScriptEngine);
void setScriptEngine(CCScriptEngineProtocol *pScriptEngine);
CCScriptEngineProtocol* getScriptEngine();
static CCScriptEngineManager* sharedScriptEngineManager();
void removeScriptEngine();
private:
CCScriptEngineManager();
virtual ~CCScriptEngineManager();
CCScriptEngineProtocol *m_pScriptEngine;
};

View File

@ -25,14 +25,17 @@ THE SOFTWARE.
NS_CC_BEGIN;
CCScriptEngineManager::CCScriptEngineManager() : m_pScriptEngine(NULL) {}
CCScriptEngineManager::CCScriptEngineManager()
:m_pScriptEngine(NULL)
{
}
CCScriptEngineManager::~CCScriptEngineManager()
{
CC_SAFE_DELETE(m_pScriptEngine);
m_pScriptEngine = NULL;
}
void CCScriptEngineManager::registerScriptEngine(CCScriptEngineProtocol *pScriptEngine)
void CCScriptEngineManager::setScriptEngine(CCScriptEngineProtocol *pScriptEngine)
{
this->m_pScriptEngine = pScriptEngine;
}
@ -42,6 +45,11 @@ CCScriptEngineProtocol* CCScriptEngineManager::getScriptEngine()
return m_pScriptEngine;
}
void CCScriptEngineManager::removeScriptEngine()
{
this->m_pScriptEngine = NULL;
}
CCScriptEngineManager* CCScriptEngineManager::sharedScriptEngineManager()
{
static CCScriptEngineManager scriptEngineManager;