quick cocos2dx c-lua(1)

boolAppDelegate::applicationDidFinishLaunching()

{

    // register lua engine

   CCLuaEngine *pEngine = CCLuaEngine::defaultEngine(); // 創建lua引擎  quick cocos2dx c-lua(2) 

    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

   CCLuaStack *pStack = pEngine->getLuaStack();


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    // load framework

    pStack->loadChunksFromZIP("res/framework_precompiled.zip");  //framework_precompiled.zip(framework文件夾下得lua2進制文件壓縮包)。 在這裏執行chunk代碼 並把對應2進制lua文件名保存到全局 


    // set script path

    string path =CCFileUtils::sharedFileUtils()->fullPathForFilename("scripts/main.lua");

#else

    // load framework

   if (m_projectConfig.isLoadPrecompiledFramework())

    {

       const string precompiledFrameworkPath = SimulatorConfig::sharedDefaults()->getPrecompiledFrameworkPath();

        pStack->loadChunksFromZIP(precompiledFrameworkPath.c_str());

    }


    // set script path

    string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(m_projectConfig.getScriptFileRealPath().c_str());

#endif


   size_t pos;

   while ((pos = path.find_first_of("\\")) !=std::string::npos)

    {

        path.replace(pos,1, "/");

    }

   size_t p = path.find_last_of("/\\");

   if (p != path.npos)  //static const size_type npos = -1;

    {

       const string dir = path.substr(0, p);

        pStack->addSearchPath(dir.c_str());


        p = dir.find_last_of("/\\");

       if (p != dir.npos)

        {

            pStack->addSearchPath(dir.substr(0, p).c_str());

        }

    }

//eg: path =  /Users/gnar/Library/Developer/CoreSimulator/Devices/78DFF2AE-C266-4EB3-A708-CE7D79CA814F/data/Containers/Bundle/Application/AE68B13C-7613-4B7C-950B-01650E6A8D85/test.app/scripts/main.lua

    string env ="__LUA_STARTUP_FILE__=\"";

    env.append(path); //附加

    env.append("\"");

    pEngine->executeString(env.c_str());   //直接調用chunk :  __LUA_STARTUP_FILE__=\xxxxxx


    CCLOG("------------------------------------------------");

    CCLOG("LOAD LUA FILE: %s", path.c_str());

    CCLOG("------------------------------------------------");

    pEngine->executeScriptFile(path.c_str());   //調用 xxx路徑指向的問件的 chunk


    return true;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章