程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Cocos2d-x3.0 lua綁定C++類

Cocos2d-x3.0 lua綁定C++類

編輯:C++入門知識

這裡記錄下我實現Lua綁定的全過程。

 

准備工作:

1、創一個一個Lua的2dx工程。(這個網上已經有好多了)

2、創一個C++類。

TestScene.h 這個只是一個簡單的場景

 

//
//  TestScene.h
//  uitestLua
//
//  Created by 杜甲 on 14-5-17.
//
//

#ifndef __uitestLua__TestScene__
#define __uitestLua__TestScene__

#include cocos2d.h
USING_NS_CC;



class TestScene :public Layer{
    
public:
    static Scene* createScene();
    virtual bool init();
    
    CREATE_FUNC(TestScene);
};

#endif /* defined(__uitestLua__TestScene__) */

 

 

TestScene.cpp

 

//
//  TestScene.cpp
//  uitestLua
//
//  Created by 杜甲 on 14-5-17.
//
//

#include TestScene.h
Scene* TestScene::createScene()
{
    auto layer= TestScene::create();
    auto scene = Scene::create();
    scene->addChild(layer);
    return scene;
    
}

bool TestScene::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!Layer::init());
        
        auto sprite = Sprite::create(res/dog.png);
        sprite->setPosition(Point(100, 200));
        addChild(sprite);
        bRet = true;
    } while (0);
    return bRet;
}

上面的代碼很簡單沒什麼好說的。接下是本文的主要內容。將上面的C++類綁定Lua

 

步驟:

1、綁定基本變量:這個按照readme中的去做,注意NDK要用:r9b一定要用這個。

2、編譯自己的ini文件。這個需要參照其他的ini。我參考的是cocos2dx_physics.ini

自己創建一個文本文件將cocos2dx_physics.ini中的內容全部粘過來,之後我們只需要修改幾個地方。

下面是修改好的腳本:

testscene.ini

 

[testscene]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = testscene

# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = cc

android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include
android_flags = -D_SIZE_T_DEFINED_ 

clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include 
clang_flags = -nostdinc -x c++ -std=c++11

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/2d -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/physics -I%(cocosdir)s/cocos/2d/platform -I%(cocosdir)s/cocos/2d/platform/android -I%(cocosdir)s/cocos/math/kazmath -I%(cocosdir)s/cocos/physics
cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT -DCC_USE_PHYSICS=1

cxxgenerator_headers = 

# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s 

# what headers to parse
headers = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.h

# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in ^$, like this: ^Menu*$.
classes = TestScene

# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: ^ClassName$ functions are also
# regular expressions, they will not be surrounded by ^$. If you want to skip a whole class, just
# add a single * as functions. See bellow for several examples. A special class name is *, which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.

skip = 
       

rename_functions = 

rename_classes = 

# for all class names, should we remove something when registering in the target VM?
remove_prefix = 

# classes for which there will be no parent lookup
classes_have_no_parents = 

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip = Layer

# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = 

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no

主要修改的地方:

 

 

[testscene]  
prefix = testscene  
target_namespace =  
headers = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.h  --這裡我用的是絕對路徑用,如果用之前的變量路徑找不到 TestScene.h
classes = TestScene  
skip =  
abstract_classes =  

之後我們再自己寫一個genbindings_testscene.py腳本這個腳本就是在genbindings.py這個腳本的基礎上改的。

 

 

只要修改命令參數就OK。

將:

 

 cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), 
                    'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), 
                    'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), 
                    'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), 
                    'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), 
                    'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), 
                      
                    }

修改成:

 

 

cmd_args = {'testscene.ini': ('testscene','lua_testscene_auto') }

 

 

之後打開命令行工具:

cd 到tolua文件夾

./genbindings_testscene.py

 

編譯就完成了。

我們接下來使用以下我們自己的類。

首先,導入我們編譯出來的.hpp和.cpp文件。他們在哪裡呢?

見下圖:

1、
\

2、\

 

之後在AppDelegate.cpp

加入頭文件:

#include lua_testscene_auto.hpp

在 bool AppDelegate::applicationDidFinishLaunching()中加入如下代碼。

 

auto engine = LuaEngine::getInstance();
	ScriptEngineManager::getInstance()->setScriptEngine(engine);
	engine->executeScriptFile(src/test2.lua);
    /***************這裡加入如下代碼*/
    register_all_testscene(engine->getLuaStack()->getLuaState());


 

 

.lua

 

	scene = TestScene:createScene()
cc.Director:getInstance():runWithScene(scene)

運行效果:

 

\

 

 

 

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved