コード例 #1
0
ファイル: AppDelegate.cpp プロジェクト: wohow/pirate-cgame
bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        string title = "wt-cgame-a";
        glview = cocos2d::GLViewImpl::create(title.c_str());
        director->setOpenGLView(glview);
        director->startAnimation();
    }

    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

    // use Quick-Cocos2d-X
    quick_module_register(L);

    LuaStack* stack = engine->getLuaStack();
#if ANYSDK_DEFINE > 0
    lua_getglobal(stack->getLuaState(), "_G");
    tolua_anysdk_open(stack->getLuaState());
    tolua_anysdk_manual_open(stack->getLuaState());
    lua_pop(stack->getLuaState(), 1);
#endif

    //register custom function
    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());

    //FileUtils::getInstance()->setResourceEncryptKeyAndSign("test", "XXTEA");
#if 0
    // use luajit bytecode package
    stack->setXXTEAKeyAndSign("2dxLua", "XXTEA");

#ifdef CC_TARGET_OS_IPHONE
    if (sizeof(long) == 4) {
        stack->loadChunksFromZIP("res/game.zip");
    } else {
        stack->loadChunksFromZIP("res/game64.zip");
    }
#else
    // android, mac, win32, etc
    stack->loadChunksFromZIP("res/game.zip");
#endif
    stack->executeString("require 'main'");
#else // #if 0
    // use discrete files
    engine->executeScriptFile("src/main.lua");
#endif

    return true;
}
コード例 #2
0
static int SendBinaryMessageToLua(int nHandler,const unsigned char* pTable,int nLength)
{
    if (NULL == pTable || nHandler <= 0) {
        return 0;
    }
    
    if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) {
        return 0;
    }
    
    LuaStack *pStack = LuaEngine::getInstance()->getLuaStack();
    if (NULL == pStack) {
        return 0;
    }

    lua_State *tolua_s = pStack->getLuaState();
    if (NULL == tolua_s) {
        return 0;
    }
    
    int nRet = 0;
    LuaValueArray array;
    for (int i = 0 ; i < nLength; i++) {
        LuaValue value = LuaValue::intValue(pTable[i]);
        array.push_back(value);
    }
    
    pStack->pushLuaValueArray(array);
    nRet = pStack->executeFunctionByHandler(nHandler, 1);
    pStack->clean();
    return nRet;
}
コード例 #3
0
bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    auto pDirector = Director::getInstance();
    pDirector->setOpenGLView(EGLView::getInstance());

    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
    
    auto screenSize = EGLView::getInstance()->getFrameSize();
    
    auto designSize = Size(480, 320);
    
    auto pFileUtils = FileUtils::getInstance();
    
    if (screenSize.height > 320)
    {
        auto resourceSize = Size(960, 640);
        std::vector<std::string> searchPaths;
        searchPaths.push_back("hd");
        pFileUtils->setSearchPaths(searchPaths);
        pDirector->setContentScaleFactor(resourceSize.height/designSize.height);
    }
    
    EGLView::getInstance()->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT);
    
    // register lua engine
    LuaEngine* pEngine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
    LuaStack* stack = pEngine->getLuaStack();
    register_assetsmanager_test_sample(stack->getLuaState());
#endif
    
    std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
    searchPaths.insert(searchPaths.begin(), "Images");
    searchPaths.insert(searchPaths.begin(), "cocosbuilderRes");
    if (screenSize.height > 320)
    {
        searchPaths.insert(searchPaths.begin(), "hd/scenetest/LoadSceneEdtiorFileTest");
    }
    else
    {
        searchPaths.insert(searchPaths.begin(), "scenetest/LoadSceneEdtiorFileTest");
    }

#if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY
    searchPaths.push_back("TestCppResources");
    searchPaths.push_back("script");
#endif
    FileUtils::getInstance()->setSearchPaths(searchPaths);

    pEngine->executeScriptFile("luaScript/controller.lua");
    
    return true;
}
コード例 #4
0
ファイル: AppDelegate.cpp プロジェクト: AnySDK/Sample_Lua
bool AppDelegate::applicationDidFinishLaunching()
{
    // register lua engine
    LuaEngine* pEngine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(pEngine);

    
    LuaStack* stack = pEngine->getLuaStack();
    stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
    
    lua_State* L = stack->getLuaState();
    
    lua_module_register(L);

    lua_getglobal(L, "_G");
    if (lua_istable(L,-1))//stack:...,_G,
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        register_assetsmanager_test_sample(L);
#endif
        register_test_binding(L);
    }
    lua_pop(L, 1);


    pEngine->executeScriptFile("src/controller.lua");

    return true;
}
コード例 #5
0
ファイル: AppDelegate.cpp プロジェクト: skyhacker2/FilpBlocks
bool AppDelegate::applicationDidFinishLaunching()
{

//#if (COCOS2D_DEBUG>0)
//    initRuntime();
//#endif

    if (!ConfigParser::getInstance()->isInit()) {
            ConfigParser::getInstance()->readConfig();
        }

    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();    
    if(!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height,bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(),viewSize.width,viewSize.height,isLanscape);
#else
        glview = GLView::createWithRect(title.c_str(), Rect(0,0,viewSize.width,viewSize.height));
        director->setOpenGLView(glview);
#endif
    }

    // turn on display FPS
    director->setDisplayStats(false);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
   
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    
    //register custom function
    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());
    LuaStack* stack = engine->getLuaStack();
    register_all_custom_manual(stack->getLuaState());
    
    auto backListener = EventListenerKeyboard::create();
    backListener->onKeyReleased = [=](EventKeyboard::KeyCode code, Event* event) {
        if (code == EventKeyboard::KeyCode::KEY_BACKSPACE) {
            director->end();
        }
    };
    director->getEventDispatcher()->addEventListenerWithFixedPriority(backListener, -1000);

//#if (COCOS2D_DEBUG>0)
//    if (startRuntime())
//        return true;
//#endif
    
    CCLOG("%s",ConfigParser::getInstance()->getEntryFile().c_str());
    engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
    return true;
}
コード例 #6
0
bool AppDelegate::applicationDidFinishLaunching()
{
    
#if (COCOS2D_DEBUG>0)
    initRuntime();
#endif
    
    if (!ConfigParser::getInstance()->isInit()) {
            ConfigParser::getInstance()->readConfig();
        }

    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();    
    if(!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height,bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(),viewSize.width,viewSize.height,isLanscape);
#else
        glview = GLView::createWithRect(title.c_str(), Rect(0,0,viewSize.width,viewSize.height));
        director->setOpenGLView(glview);
#endif
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
   
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    
    //register custom function
    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());
    
	//register super anim function
	LuaStack* stack = engine->getLuaStack();
    auto state = stack->getLuaState();
    lua_getglobal(state, "_G");
    register_all_sa(state);
    lua_pop(state, 1);
	
#if (COCOS2D_DEBUG>0)
    if (startRuntime())
        return true;
#endif

    engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
    return true;
}
コード例 #7
0
ファイル: AppDelegate.cpp プロジェクト: Alex345/muffin_quest
bool AppDelegate::applicationDidFinishLaunching()
{
    
#if (COCOS2D_DEBUG>0)
    initRuntime();
#endif
    
    if (!ConfigParser::getInstance()->isInit()) {
            ConfigParser::getInstance()->readConfig();
        }

    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();    
    if(!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height,bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(),viewSize.width,viewSize.height,isLanscape);
#else
        glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0,0,viewSize.width,viewSize.height));
        director->setOpenGLView(glview);
#endif
    }

   
    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
   
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);

    LuaStack* stack = engine->getLuaStack();
    stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
    
    //register custom function
    auto luaState = stack->getLuaState();
    lua_getglobal(luaState, "_G");
    register_all_pluginx_protocols(luaState);
    register_all_pluginx_manual_callback(luaState);
    register_all_pluginx_manual_protocols(luaState);
    
#if (COCOS2D_DEBUG>0)
    if (startRuntime())
        return true;
#endif

    engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
    return true;
}
コード例 #8
0
LUA_STRING Crypto::decryptXXTEALua(const char* plaintext,
                                     int plaintextLength,
                                     const char* key,
                                     int keyLength)
{
    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
    stack->clean();
    
    int resultLength;
    unsigned char* result = decryptXXTEA((unsigned char*)plaintext, plaintextLength, (unsigned char*)key, keyLength, &resultLength);
    
    if (resultLength <= 0)
    {
        lua_pushnil(stack->getLuaState());
    }
    else
    {
        lua_pushlstring(stack->getLuaState(), (const char*)result, resultLength);
        free(result);
    }
    return 1;
}
コード例 #9
0
ファイル: SocketClient.cpp プロジェクト: 289/DouPo
// 不在LuaStack类中增加接口了。
// 带参数(参数已经push好)执行全局lua函数,忽略返回值。
static void executeLuaGlobalFunction(const char* functionName,int numArgs)
{
	LuaStack* ls = LuaEngine::getInstance()->getLuaStack();
	lua_State* L = ls->getLuaState();
	lua_getglobal(L,functionName);
	if(!lua_isfunction(L,-1))
	{
		CCLOG("[LUA ERROR] name '%s' does not represent a Lua function",functionName);
		lua_settop(L,0);
		return;
	}
	if(numArgs>0)
	{
		lua_insert(L,-numArgs-1);
	}
	ls->executeFunction(numArgs);
}
コード例 #10
0
    virtual void onLoadGameData(const sdkbox::SavedGameData* savedData, const std::string& error) {
        LuaStack* stack = LUAENGINE->getLuaStack();

        LuaValueDict dict;
        dict.insert(std::make_pair("name", LuaValue::stringValue("onLoadGameData")));
        dict.insert(std::make_pair("error", LuaValue::stringValue(error)));
        stack->pushLuaValueDict(dict);

        if (nullptr != savedData && 0 == error.size()) {
            lua_State* ls = stack->getLuaState();
            lua_pushstring(ls, "savedData"); /* L: table key */
            lua_newtable(ls); /* L: table key table */

            lua_pushstring(ls, "name"); /* L: table key table "name" */
            lua_pushstring(ls, savedData->name.c_str()); /* L: table key table "name" namevalue */
            lua_rawset(ls, -3); /* L: table key table */

            lua_pushstring(ls, "deviceName"); /* L: table key table "deviceName" */
            lua_pushstring(ls, savedData->deviceName.c_str()); /* L: table key table "deviceName" deviceNameValue */
            lua_rawset(ls, -3); /* L: table key table */

            lua_pushstring(ls, "lastModifiedTimestamp"); /* L: table key table "lastModifiedTimestamp" */
            lua_pushnumber(ls, savedData->lastModifiedTimestamp); /* L: table key table "lastModifiedTimestamp" timestamp */
            lua_rawset(ls, -3); /* L: table key table */

            lua_pushstring(ls, "dataLength"); /* L: table key table "dataLength" */
            lua_pushnumber(ls, savedData->dataLength); /* L: table key table "dataLength" length */
            lua_rawset(ls, -3); /* L: table key table */

            lua_pushstring(ls, "data"); /* L: table key table "data" */
            lua_pushlstring(ls, (const char*)savedData->data, savedData->dataLength); /* L: table key table "data" datavalue */
            lua_rawset(ls, -3); /* L: table key table */

            lua_rawset(ls, -3); /* L: table*/
        }

        stack->executeFunctionByHandler(mLuaHandler, 1);
    }
コード例 #11
0
ファイル: RuntimeLuaImpl.cpp プロジェクト: ntotani/princess
static void resetLuaModule(const string& fileName)
{
    if (fileName.empty())
    {
        return;
    }
    auto engine = LuaEngine::getInstance();
    LuaStack* luaStack = engine->getLuaStack();
    lua_State* stack = luaStack->getLuaState();
    lua_getglobal(stack, "package");                         /* L: package */
    lua_getfield(stack, -1, "loaded");                       /* L: package loaded */
    lua_pushnil(stack);                                     /* L: lotable ?-.. nil */
    while (0 != lua_next(stack, -2))                     /* L: lotable ?-.. key value */
    {
        //CCLOG("%s - %s \n", tolua_tostring(stack, -2, ""), lua_typename(stack, lua_type(stack, -1)));
        std::string key = tolua_tostring(stack, -2, "");
        std::string tableKey = key;
        size_t found = tableKey.rfind(".lua");
        if (found != std::string::npos)
        tableKey = tableKey.substr(0, found);
        tableKey = replaceAll(tableKey, ".", "/");
        tableKey = replaceAll(tableKey, "\\", "/");
        tableKey.append(".lua");
        found = fileName.rfind(tableKey);
        if (0 == found || (found != std::string::npos && fileName.at(found - 1) == '/'))
        {
            lua_pushstring(stack, key.c_str());
            lua_pushnil(stack);
            if (lua_istable(stack, -5))
            {
                lua_settable(stack, -5);
            }
        }
        lua_pop(stack, 1);
    }
    lua_pop(stack, 2);
}
コード例 #12
0
static int lua_get_XMLHttpRequest_response(lua_State* L)
{
    LuaMinXmlHttpRequest* self = nullptr;
    
#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
    if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror;
#endif
    
    self = (LuaMinXmlHttpRequest*)  tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
    if (nullptr == self)
    {
        tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_response'\n", nullptr);
		return 0;
    }
#endif
    
    if (self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::JSON)
    {
        if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag())
            return 0;
        
        lua_pushstring(L, self->getDataStr().c_str());
        return 1;
    }
    else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER)
    {
        if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag())
            return 0;
        
        LuaStack *pStack = LuaEngine::getInstance()->getLuaStack();
        if (NULL == pStack) {
            return 0;
        }
        
        lua_State *tolua_s = pStack->getLuaState();
        if (NULL == tolua_s) {
            return 0;
        }
        
        LuaValueArray array;
        
        uint8_t* tmpData = new uint8_t[self->getDataSize()];
        if (nullptr == tmpData)
        {
            return 0;
        }
        
        self->getByteData(tmpData);
        
        for (int i = 0 ; i < self->getDataSize(); i++)
        {
            LuaValue value = LuaValue::intValue(tmpData[i]);
            array.push_back(value);
        }
        
        pStack->pushLuaValueArray(array);
        
        CC_SAFE_DELETE_ARRAY(tmpData);
        return 1;
    }
    else
    {
        lua_pushstring(L, self->getDataStr().c_str());
        return 1;
    }
    
#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_response'.",&tolua_err);
    return 0;
#endif
}
コード例 #13
0
static int SendSpineEventToLua(int nHandler, spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount)
{
    if (nHandler <= 0) {
        return 0;
    }

    if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) {
        return 0;
    }

    LuaStack *pStack = LuaEngine::getInstance()->getLuaStack();
    if (NULL == pStack) {
        return 0;
    }

    lua_State *tolua_s = pStack->getLuaState();
    if (NULL == tolua_s) {
        return 0;
    }

    int nRet = 0;

	spTrackEntry* entry = spAnimationState_getCurrent(node->state, trackIndex);
	std::string animationName = (entry && entry->animation) ? entry->animation->name : "";
	std::string eventType = "";

	switch (type) {
	case ANIMATION_START:
		eventType = "start";
		break;
	case ANIMATION_END:
		eventType = "end";
		break;
	case ANIMATION_COMPLETE:
		eventType = "complete";
		break;
	case ANIMATION_EVENT:
		eventType = "event";
		break;
	}

	LuaValueDict spineEvent;
	spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("type", LuaValue::stringValue(eventType)));
	spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("trackIndex", LuaValue::intValue(trackIndex)));
	spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("animation", LuaValue::stringValue(animationName)));
	spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("loopCount", LuaValue::intValue(loopCount)));

	if (NULL != event) {
		LuaValueDict eventData;
		eventData.insert(eventData.end(), LuaValueDict::value_type("name", LuaValue::stringValue(event->data->name)));
		eventData.insert(eventData.end(), LuaValueDict::value_type("intValue", LuaValue::intValue(event->intValue)));
		eventData.insert(eventData.end(), LuaValueDict::value_type("floatValue", LuaValue::floatValue(event->floatValue)));
		eventData.insert(eventData.end(), LuaValueDict::value_type("stringValue", LuaValue::stringValue(event->stringValue)));
		spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("eventData", LuaValue::dictValue(eventData)));
	}

	pStack->pushLuaValueDict(spineEvent);
    nRet = pStack->executeFunctionByHandler(nHandler, 1);
    pStack->clean();
    return nRet;

}
コード例 #14
0
ファイル: AppDelegate.cpp プロジェクト: gary-lee/timberman
bool AppDelegate::applicationDidFinishLaunching()
{
#if (COCOS2D_DEBUG > 0) && CC_USE_RUNTIME
    // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
    if (_launchMode)
    {
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8)
        initRuntime();
#endif
    }
#endif

    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if (!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height, bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(), viewSize.width, viewSize.height, isLanscape);
#else
        glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0, 0, viewSize.width, viewSize.height));
        director->setOpenGLView(glview);
#endif
    }

    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

    // use Quick-Cocos2d-X
    quick_module_register(L);

    LuaStack* stack = engine->getLuaStack();
#if ANYSDK_DEFINE > 0
    lua_getglobal(stack->getLuaState(), "_G");
    tolua_anysdk_open(stack->getLuaState());
    tolua_anysdk_manual_open(stack->getLuaState());
    lua_pop(stack->getLuaState(), 1);
#endif

    stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));

    //register custom function
    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());

#if (COCOS2D_DEBUG > 0) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && CC_USE_RUNTIME
    // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
    if (_launchMode)
    {
        startRuntime();
    }
    else
    {
        engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
    }
#else
    engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
#endif

    return true;
}
コード例 #15
0
ファイル: AppDelegate.cpp プロジェクト: airtaxi/cocos2d-lua
bool AppDelegate::applicationDidFinishLaunching()
{
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::createWithRect("Test Lua", Rect(0,0,900,640));
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
    
    auto screenSize = glview->getFrameSize();
    
    auto designSize = Size(480, 320);
    
    auto pFileUtils = FileUtils::getInstance();
    
    if (screenSize.height > 320)
    {
        auto resourceSize = Size(960, 640);
        std::vector<std::string> searchPaths;
        searchPaths.push_back("hd");
        pFileUtils->setSearchPaths(searchPaths);
        director->setContentScaleFactor(resourceSize.height/designSize.height);
    }
    
    glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT);
    
    // register lua engine
    LuaEngine* pEngine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
    LuaStack* stack = pEngine->getLuaStack();
    register_assetsmanager_test_sample(stack->getLuaState());
#endif
    
    std::string resPrefix("res/");
    
    std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
    searchPaths.insert(searchPaths.begin(), resPrefix);
    searchPaths.insert(searchPaths.begin(), resPrefix + "Images");
    searchPaths.insert(searchPaths.begin(), resPrefix + "cocosbuilderRes");
    if (screenSize.height > 320)
    {
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/Images");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/ArmatureComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/AttributeComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/BackgroundComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/EffectComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/LoadSceneEdtiorFileTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/ParticleComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/SpriteComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/TmxMapComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/UIComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/TriggerTest");
    }
    else
    {
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/ArmatureComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/AttributeComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/BackgroundComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/EffectComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/LoadSceneEdtiorFileTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/ParticleComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/SpriteComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/TmxMapComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/UIComponentTest");
        searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/TriggerTest");
    }


    FileUtils::getInstance()->setSearchPaths(searchPaths);

    pEngine->executeScriptFile("src/controller.lua");
    
    return true;
}
コード例 #16
0
bool AppDelegate::applicationDidFinishLaunching()
{
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	InitCrashReport();
#endif

#if (COCOS2D_DEBUG>0)
    initRuntime();
#endif



    if (!ConfigParser::getInstance()->isInit()) {
            ConfigParser::getInstance()->readConfig();
        }
	
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();    
	
    if(!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height,bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(),viewSize.width,viewSize.height,isLanscape);
		//::SetWindowPos(glfwGetWin32Window(director->getOpenGLView()->getWindow()),HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
#else
        glview = GLView::createWithRect(title.c_str(), Rect(0,0,viewSize.width,viewSize.height));
        director->setOpenGLView(glview);
		
#endif
    }

   
    // set FPS. the default value is 1.0/60 if you don't call this
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)


#if (COCOS2D_DEBUG<=0)
	director->setAnimationInterval(1.0 / 30);
   
#else
	director->setAnimationInterval(1.0 / 60);


#endif

#else
	director->setAnimationInterval(1.0 / 60);

#endif

    
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);

    LuaStack* stack = engine->getLuaStack();
    stack->setXXTEAKeyAndSign("s3ExtaS", strlen("s3ExtaS"), "IH8wdkD", strlen("IH8wdkD"));
	//cocos luacompile -s G:\\mobile_arpg\\arpg\\src  -d I:\\securecrt\\xxeat -e -k s3ExtaS -b IH8wdkD  
    
    //register custom function
	CustomLuaCocos2d(engine->getLuaStack()->getLuaState());

    //LuaStack* stack = engine->getLuaStack();
    //register_custom_function(stack->getLuaState());

	//register_all_ccarpg(stack->getLuaState());
	//register_all_ccarpg_manual(stack->getLuaState());

//    
	std::string resdirname = "res/";
	std::string configdirname = "config/";
	vector<string> searchPath;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	searchPath.push_back("../../src/");
	searchPath.push_back("../../"+configdirname);
	searchPath.push_back("../../"+resdirname);

	//extern std::string getCurAppPath();
	//string resourcePath = getCurAppPath();
	//searchPath.push_back(resourcePath+"/");
	//searchPath.push_back("src/");

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
    searchPath.push_back("src/");
    searchPath.push_back(configdirname);
    searchPath.push_back(resdirname);

#else
	searchPath.push_back("src/");
	//searchPath.push_back("config/");
	searchPath.push_back(resdirname);
#endif

//	std::string pathToSave = FileUtils::getInstance()->getWritablePath();
//	pathToSave += "s3b6lhuxo";
//
//#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
//	DIR *pDir = NULL;
//
//	pDir = opendir (pathToSave.c_str());
//	if (pDir)
//	{
//		searchPath.insert(searchPath.begin(), pathToSave);
//	}
//#else
//	if ((GetFileAttributesA(pathToSave.c_str())) != INVALID_FILE_ATTRIBUTES)
//	{
//		searchPath.insert(searchPath.begin(), pathToSave);
//	}
//#endif
	 
	
	
	FileUtils::getInstance()->setSearchPaths(searchPath);
	CcarpgNet::getInstance()->start();

	#if (COCOS2D_DEBUG>0)
	MessageDispatcher::getInstance()->startLocalTerminal(0);
#endif

	//if(!ReceiveThread::GetInstance()->isRunning)
	//{
	//	ReceiveThread::GetInstance()->start();
	//}


	//FileUtils::getInstance()->printSearchPath();

#if (COCOS2D_DEBUG>0)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
	register_assetsmanager_test_sample(stack->getLuaState());
#endif
	 if (ConfigParser::getInstance()->useCocosIde()){
    if (startRuntime())
	{
		#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 
		EventListenerKeyboard *listener = EventListenerKeyboard::create();  
		listener->onKeyReleased = AppDelegate::keyEventCallback;  
		director->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);  
#endif
        return true;
	}
	 }
	 else
	 {
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
//		 register_assetsmanager_test_sample(stack->getLuaState());
//#endif
#ifdef USE_ANY_SDK_IN_IOS
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
         LuaStack* stackios = engine->getLuaStack();
         lua_getglobal(stackios->getLuaState(), "_G");
         tolua_anysdk_open(stackios->getLuaState());
         tolua_anysdk_manual_open(stackios->getLuaState());
         lua_pop(stackios->getLuaState(), 1);
#endif
#endif
		engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
        //  engine->executeScriptFile("src/main.lua");
         
		
	 }
#else
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
	register_assetsmanager_test_sample(stack->getLuaState());
    #endif


	
	//vector<string> searchPath;
	//searchPath.push_back("../../src");
	//searchPath.push_back("../../res");
	//FileUtils::getInstance()->setSearchPaths(searchPath);
	//CcarpgNet::getInstance()->start();

	//long lasttt = getTimer();
	//CcarpgMcm::getInstance()->setCurrentMapData(51001);
	//CCLOG("mcm need time = %d",(getTimer()-lasttt));
	//SocketThread* th=  SocketThread::GetInstance();
	//th->start();
#ifdef USE_ANY_SDK_IN_IOS
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    LuaStack* stackios = engine->getLuaStack();
    lua_getglobal(stackios->getLuaState(), "_G");
    tolua_anysdk_open(stackios->getLuaState());
    tolua_anysdk_manual_open(stackios->getLuaState());
    lua_pop(stackios->getLuaState(), 1);
#endif
#endif
	engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
	
#endif

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
	//::SetWindowPos(glfwGetWin32Window(director->getOpenGLView()->getWindow()),HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
	//BringWindowToTop(glfwGetWin32Window(director->getOpenGLView()->getWindow()));
	//SetConsoleTitle(L"your title");
	//SetWindowText(glfwGetWin32Window(director->getOpenGLView()->getWindow()), L"cllc");
	// HDC hdc = GetWindowDC(glfwGetWin32Window(director->getOpenGLView()->getWindow()));
	//SetTextColor(hdc, 0xFFFF0000);
#endif
	#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 
	EventListenerKeyboard *listener = EventListenerKeyboard::create();  
	listener->onKeyReleased = AppDelegate::keyEventCallback;  
	director->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1); 
#endif
    return true;
}
コード例 #17
0
static int tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler(lua_State* tolua_S)
{
    if (nullptr == tolua_S)
        return 0;
    
    int argc = 0;
    EventListenerPhysicsContact* self = nullptr;
#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
    if (!tolua_isusertype(tolua_S, 1, "cc.EventListenerPhysicsContact", 0, &tolua_err))  goto tolua_lerror;
#endif
    
    self = static_cast<EventListenerPhysicsContact*>(tolua_tousertype(tolua_S,1,0));
#if COCOS2D_DEBUG >= 1
    if (nullptr == self) {
        tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler'\n", nullptr);
        return 0;
    }
#endif
    argc = lua_gettop(tolua_S) - 1;
    
    if (argc == 2)
    {
#if COCOS2D_DEBUG >= 1
        if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) ||
            !tolua_isnumber(tolua_S, 3, 0, &tolua_err))
        {
            goto tolua_lerror;
        }
#endif
        LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0);
        ScriptHandlerMgr::HandlerType type        = static_cast<ScriptHandlerMgr::HandlerType>((int)tolua_tonumber(tolua_S, 3, 0));
        switch (type)
        {
            case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_BEGIN:
            {
                ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
                
                self->onContactBegin = [handler](PhysicsContact& contact) -> bool{
                    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
                    stack->pushObject(&contact, "cc.PhysicsContact");
                    bool ret = stack->executeFunctionByHandler(handler, 1);
                    stack->clean();
                    
                    return ret;
                };
            }
                break;
            case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_PRESOLVE:
            {
                ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
                
                self->onContactPreSolve = [handler](PhysicsContact& contact, PhysicsContactPreSolve& solve) -> bool{
                    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
                    stack->pushObject(&contact, "cc.PhysicsContact");
                    tolua_pushusertype(stack->getLuaState(), &solve, "cc.PhysicsContactPreSolve");
                    bool ret = stack->executeFunctionByHandler(handler, 2);
                    stack->clean();
                    
                    return ret;
                };
            }
                break;
            case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_POSTSOLVE:
            {
                ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
                
                self->onContactPostSolve = [handler](PhysicsContact& contact, const PhysicsContactPostSolve& solve){
                    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
                    stack->pushObject(&contact, "cc.PhysicsContact");
                    tolua_pushusertype(stack->getLuaState(), const_cast<PhysicsContactPostSolve*>(&solve), "cc.PhysicsContactPostSolve");
                    stack->executeFunctionByHandler(handler, 2);
                    stack->clean();
                };
            }
                break;
            case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_SEPERATE:
            {
                ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
                
                self->onContactSeperate = [handler](PhysicsContact& contact){
                    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
                    stack->pushObject(&contact, "cc.PhysicsContact");
                    stack->executeFunctionByHandler(handler, 1);
                    stack->clean();
                };
            }
                break;
            default:
                break;
        }
        return 0;
    }
    
    CCLOG("'registerScriptHandler' has wrong number of arguments: %d, was expecting %d\n", argc, 2);
    return 0;
    
#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err);
    return 0;
#endif
}
コード例 #18
0
bool AppDelegate::applicationDidFinishLaunching()
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
	initRuntime();
#elif (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
	// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
	if (_launchMode)
	{
		initRuntime();
	}
#endif

    initResourcePath();
    
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();    
    if(!glview) {
        Size viewSize = ConfigParser::getInstance()->getInitViewSize();
        string title = ConfigParser::getInstance()->getInitViewName();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
        extern void createSimulator(const char* viewName, float width, float height, bool isLandscape = true, float frameZoomFactor = 1.0f);
        bool isLanscape = ConfigParser::getInstance()->isLanscape();
        createSimulator(title.c_str(),viewSize.width,viewSize.height, isLanscape);
#else
        glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0, 0, viewSize.width, viewSize.height));
        director->setOpenGLView(glview);
#endif
        director->startAnimation();
    }
   
    auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);

    // use Quick-Cocos2d-X
    quick_module_register(L);

    LuaStack* stack = engine->getLuaStack();
#if ANYSDK_DEFINE > 0
    lua_getglobal(stack->getLuaState(), "_G");
    tolua_anysdk_open(stack->getLuaState());
    tolua_anysdk_manual_open(stack->getLuaState());
    lua_pop(stack->getLuaState(), 1);
#endif

    stack->setXXTEAKeyAndSign("ilovecocos2dx", strlen("ilovecocos2dx"), "XXTEA", strlen("XXTEA"));

    stack->addSearchPath("src");
	
#if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
    // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
    if (_launchMode)
    {
        startRuntime();
    }
#endif
    FileUtils *utils = FileUtils::getInstance();

	const char *updateFileName = "code/launcher.zip";
	std::string updateFilePath = utils->fullPathForFilename(updateFileName);

    bool isUpdate = false;

	if (updateFilePath.compare(updateFileName) != 0) //check if update file exist
    {
		printf("%s\n", updateFilePath.c_str());
        isUpdate = true;
        engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
    }

    if (!isUpdate) //no update file
    {
        const char *zipFilename ="code/game.zip";
        
        std::string zipFilePath = utils->fullPathForFilename(zipFilename);

        if (zipFilePath.compare(zipFilename) == 0) //no game zip file use default lua file
        {
            engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
        }
        else
        {
            stack->loadChunksFromZIP(zipFilename);
            stack->executeString("require 'main'");
        }
    }
    return true;
}