• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            colorful

            zc qq:1337220912

             

            Lua注冊回調到C++

            http://cn.cocos2d-x.org/tutorial/show?id=1896

            思路

            像所有語言一樣,綁定回調主要是執行的任務執行到特定情形的時候,調用對用回調方法。 本文也一樣,Lua注冊回調到C++的核心思路是,當C代碼執行到特定特定情形的時候,調用Lua的方法。

            我這里使用的是用lua_stack直接調用lua的方法,沒有使用Cocos2d-x封裝的那個dispatcher,因為熟悉那個格式太墨跡了。


            主要步驟如下

            • 緩存Lua函數在Lua環境中的引用

            • 在C代碼的地方用C的方式設置好回調

            • 在C代碼回調函數執行的時候,調用lua函數


            實現

            • C代碼綁定回調,調用Lua函數

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            void ArmatureNode::registerMovementEventHandler(int handler)
            {
                unregisterMovementEventHandler();  //移除之前注冊的監聽
                _movementHandler = handler;         //緩存lua函數的引用 這個后邊說
                 
                auto dispatcher = getCCEventDispatcher();
                 
                auto f = [this](cocos2d::EventCustom *event) //注冊c代碼形式的回調 這里用function做
                {
                    auto eventData = (dragonBones::EventData*)(event->getUserData());
                    auto type = (int) eventData->getType();
                    auto movementId = eventData->animationState->name;
                    auto lastState = eventData->armature->getAnimation()->getLastAnimationState();
                     
                    auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack();
                    stack->pushObject(this"db.ArmatureNode");
                    stack->pushInt(type);
                    stack->pushString(movementId.c_str(), movementId.size());        
                    //通過LuaStack調用lua里的函數    最后一個參數設置參數個數
                    stack->executeFunctionByHandler(_movementHandler, 3);
                };
                 
                dispatcher->addCustomEventListener(dragonBones::EventData::COMPLETE, f);
            }
            void ArmatureNode::unregisterMovementEventHandler(void)
            {
                if (0 != _movementHandler)
                {
                    cocos2d::LuaEngine::getInstance()->removeScriptHandler(_movementHandler); //移除lua函數的綁定
                    _movementHandler = 0;
                }
            }
            • 提供Lua函數綁定到C的方法   

            上邊的這個函數直接用cocos里的genbinding.py 是無法正確生成Lua里可調用的接口的,需要手動編寫綁定方法.

            說這個得用到Cocos2d-x中提供的一個方法:toluafix_ref_function會把一個Lua棧中的方法轉成一個int,以便C++中調用。我會在最后面說這個

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            int tolua_db_DBCCArmature_registerMovementEventHandler(lua_State* tolua_S)
            {
                if (NULL == tolua_S)
                    return 0;
                int argc = 0;
                 
                dragonBones::ArmatureNode* self = nullptr;
                self = static_cast<dragonBones::ArmatureNode*>(tolua_tousertype(tolua_S,1,0)); //第一個參數 就是lua里的self
                 
                argc = lua_gettop(tolua_S) - 1;
                 
                if (1 == argc)
                {
                    //第二個參數,就是Lua里的function 這里要通過toluafix_ref_function這個函數映射成一個Int值
                    int handler = (toluafix_ref_function(tolua_S,2,0)); 
                    self->registerMovementEventHandler(handler);
                     
                    return 0;
                }
                return 0;
            }

             

            • 將綁定方法綁定到Lua環境里

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            int extends_ArmatureNode(lua_State* tolua_S)
            {
                lua_pushstring(tolua_S, "db.ArmatureNode");//之前db.ArmatureNode是通過腳本綁定在lua里。這里只做擴展
                lua_rawget(tolua_S, LUA_REGISTRYINDEX);
                if (lua_istable(tolua_S,-1))
                {
                    lua_pushstring(tolua_S,"registerMovementEventHandler");
                    lua_pushcfunction(tolua_S,tolua_db_DBCCArmature_registerMovementEventHandler);
                    lua_rawset(tolua_S,-3);
                }
                 
                lua_pop(tolua_S, 1);
                return 0;
            }
            • Lua里設置回調到C++

            1
            2
            3
            4
            5
            6
            7
            8
             local arm = db.ArmatureNode:create("Dragon")
                local animation = arm:getAnimation()
                animation:gotoAndPlay("walk")
                arm:registerMovementEventHandler(
                    function(...)
                        print(...) 
                    end
                )


            -測試

            打印回調輸出,測試通過 userdata 8 walk


            其他

            • toluafix_ref_function 以及 toluafix_get_function_by_refid

            這 兩個方法是相互對應的 toluafix_ref_function這個方法在注冊表上將一個lua的function與一個function_id生成映射 toluafix_get_function_by_refid 方法可以通過前一個方法生成的function_id來講綁定的lua function放到棧頂

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            //
            TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def)
            {
                if (!lua_isfunction(L, lo)) return 0;
                s_function_ref_id++;                            //function_id 加1
                lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);//在注冊表上,存放luafunction 映射table 的key壓棧
                lua_rawget(L, LUA_REGISTRYINDEX);               //獲取方法映射表,放在棧頂
                lua_pushinteger(L, s_function_ref_id);          //function_id壓棧
                lua_pushvalue(L, lo);                           //lo有效處索引處是lua方法,lua方法拷貝,壓棧
             
             
                lua_rawset(L, -3);                        //生成映射 
                lua_pop(L, 1);                                              
                return s_function_ref_id;
            }
            TOLUA_API void toluafix_get_function_by_refid(lua_State* L, int refid)
            {
                lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);            //存放luafunction 映射table 的key壓棧
                lua_rawget(L, LUA_REGISTRYINDEX);                           //獲取方法映射表,放在棧頂
                lua_pushinteger(L, refid);                                  //function_id壓棧
                lua_rawget(L, -2);                                          //獲取到的luafunction 放到棧頂
                lua_remove(L, -2);                                          //
            }
            • executeFunctionByHandler

            executeFunctionByHandler 這個方法只是通過toluafix_get_function_by_refid 獲取到function然后通過lua_pcall 方法調用,代碼就不寫了。

            posted on 2015-01-17 11:12 多彩人生 閱讀(2851) 評論(0)  編輯 收藏 引用 所屬分類: lua 、android cocos2dx

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久青草线蕉综合超碰| 久久久久免费看成人影片| 久久精品?ⅴ无码中文字幕| 亚洲国产成人精品女人久久久| 欧美亚洲国产精品久久| 97久久超碰成人精品网站| 99热精品久久只有精品| 亚洲国产欧洲综合997久久| segui久久国产精品| 伊人久久大香线蕉综合影院首页| 99久久免费只有精品国产| 亚洲国产精品无码久久一区二区| 久久高潮一级毛片免费| 久久久久亚洲精品无码蜜桃| 一本色道久久88综合日韩精品 | 国产一区二区久久久| 国产精品久久久久久久久免费| 久久久久成人精品无码| 国产亚洲欧美成人久久片| 人人妻久久人人澡人人爽人人精品 | 国产精品免费久久久久电影网| 亚洲精品无码久久久久去q| 无码国内精品久久人妻麻豆按摩| 久久这里只有精品首页| 久久国产乱子伦免费精品| 亚洲精品乱码久久久久久蜜桃不卡| 久久综合色区| 开心久久婷婷综合中文字幕| 久久久99精品成人片中文字幕| 久久久久夜夜夜精品国产| 99久久这里只有精品| 国产91色综合久久免费| 九九99精品久久久久久| av无码久久久久不卡免费网站 | 国产成人无码精品久久久久免费| 97久久超碰国产精品2021| 99久久人妻无码精品系列| 777米奇久久最新地址| 久久久综合九色合综国产| 久久久久免费精品国产| 久久久久亚洲AV综合波多野结衣|