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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值