Cc3.2+lua (8)--lua Call Custom C + + class

Source: Internet
Author: User

"nagging"

What this SectionTo says is that if you register your C + + class with the LUA environment, let Lua invoke the custom C + + class .

There are many online with the original tolua++ tools to register C + + class, I read a lot of such tutorials, feel very cumbersome to operate, and it is difficult to understand what they are talking about.

In fact, in the COCOS2DX v3.2 version, a bindings-generator script was provided to encapsulate tolua++ usage, thus saving effort.


"Acknowledgements"

HTTP://SEGMENTFAULT.COM/BLOG/HONGLIANG/1190000000718145 (speaking very well!) )

http://cn.cocos2d-x.org/article/index?type=code-ide&url=/doc/cocos-docs-master/manual/code-ide/ BINDING-CUSTOM-CLASS-TO-LUA/ZH.MD (Official document)


"Using Tools"

Windows7 x64

COCOS2DX v3.2

Cocos Code IDE 1.0.1 (Smart hints feature for custom classes)


python 2.7.x(v3.2 version does not have to be 2.7.3, I also succeeded with 2.7.8)

NDK r9d , unzip and configure the environment variable Ndk_root (v3.2 version does not have to r9b, I succeeded with r9d)

Pyyaml , install to "Python installation directory \lib\site-packages"

Http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe

Cheetah , and unzip to "Python's installation directory \lib\site-packages"

Https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip

Dos2unix , Windows may have this error while executing scripts.

Unzip to a directory below and set the value of the PATH environment variable to point to the bin directory.

Http://waterlan.home.xs4all.nl/dos2unix/dos2unix-7.1-win32.zip



"Bind method"

The following is a description of the Windows7 + VS2013 + Cocos Code IDE.

and use the Cocos Code IDE to create the LUA project, the binding method.


1. Place your custom C + + code under frameworks\runtime-src\classes

Of course where to put is casual, I like to put under the classes.


2. Add the. ini file for the custom class

In Frameworks\cocos2d-x\tools\tolua , copy The configuration information of the Cocos2dx.ini under this folder and modify some parameters to change the parameters of our custom class.

the parameter configurations that need to be modified are listed below:

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 [Custom_api] # prefix is added to the generated function. You can also choose not to add this to your template prefix = custom_api # All classes will be embedded into this namespace target_namespace = my # class In the path, if there are multiple, separated by a space headers =% (Cocosdir) s/: /runtime-src/classes/panzoomlayer.h # classes that need to be registered, if there are multiple, separated by spaces Classes = panzoomlayer # Public member functions that are not provided to LUA skip = panzooml Ayer::[ontouchesbegan ontouchesmoved ontouchesended init onEnter onExit UPDATE] # These are all empty. Rename_functions = Rename_cla sses = Remove_prefix = classes_have_no_parents = Base_classes_to_skip = abstract_classes = Script_control_cpp = no


3. genbindings.py Add custom configuration. ini

Find Cmd_args in line 129 of Frameworks\cocos2d-x\tools\tolua 's genbindings.py .

Add our custom Custom_api.ini and comment out the other. ini(These do not need to be regenerated)


4. Run the genbindings.py script

Run the genbindings.py script at the terminal.

here, I am writing a batch file directly. bat.

Note: as long as the tools needed to download, install, configure, and generally can generate success.

The build fails, basically because the tool is not configured properly.


After the build succeeds, we will find the bridge file of C + + generated in Frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto , lua_custom_api_ Auto. cpp and Lua_custom_api_auto. hpp .


The Register_all_custom_api in LUA_CUSTOM_API_AUTO.HPP is the key function we use to register the Panzoomlayer class with the LUA environment.


And in Frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\api , we can also find the interface files we provide to LUA calls.


5. Compile and register to LUA

The function of registering a custom class can be seen in our lua_custom_api_auto.hpp .

Register_all_custom_api (lua_state* tolua_s)


Use VS2013 to open the project under frameworks\runtime-src\proj.win32 .

(1) Add the custom class Panzoomlayer to the project classes.

(2) Add Lua_custom_api_auto.cpp, lua_custom_api_auto.hpp to the project Liblua Auto.

(3) Add File search path for Lualib project.

Will $ (engineroot): /runtime-src/classes path Add in.

(4) Edit the entry class under Frameworks\runtime-src\classes AppDelegate.cpp

> Add: Lua_custom_api_auto . HPP Header files

> Registration: Register_all_custom_api (state)

> Note: The next two words of REGISTER_ALL_CUSTOM_API must be added!

(5) Compile and run the entire project to complete the C + + class registration to Lua.

then you can use the custom class happily again in Lua!



"Turn on smart tips"

Although we have registered our custom C + + classes in Lua, we do not have smart hints for our custom classes in the Cocos Code IDE .

We need to modify some configurations so that the Cocos Code IDE adds Smart hints to our custom classes.


1. Smart tips in the COCOS2DX engine

First, let's take a look at how the smart hints for the COCOS2DX engine in the Cocos Code IDE are doing.

Just find a cc. Label put!

We press and hold the CTRL key of the keyboard and then click cc and Labelto jump to where they are declared.

The file jumps to the following two images:



you can see the above two pictures, you know how to give us a custom C + + class Smart tips?


We first find the following file path:(may be different for everyone)

\cocoscodeide\configuration\org.eclipse.osgi\bundles\61\1\.cp\resource\cocos2dx-3.2

It can be found that there is a api.zip This compression package under this path.

Let's extract the Api.zip to see what's inside.



You can see that there are all the COCOS2DX C + + classes that are provided to the LUA interface declaration.

These are the reasons for smart hints in the IDE.


2. Add Smart Tips for custom classes (way one)

We write an interface declaration for a custom class, modeled after the Cc.lua and Label.lua in Api.zip.

> My.lua : Declaring namespaces

> Panzoomlayer.lua : Declares a custom class. (This is automatically generated when using script binding)

Where Panzoomlayer.lua is automatically generated when the genbindings.py script is used.

It's in the Frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\api .

Well, let's write another My.lua and put it in this directory.

where the My.lua code is as follows:

1 2 3 4 5 6 7 8 9 ----------------------------------@module my----------------------------------------------------------the My Panz Oomlayer--@field [parent= #my] panzoomlayer#panzoomlayer panzoomlayer preloaded module return nil



Panzoomlayer.lua The code is as follows:


Then we compress the My.lua and the Panzoomlayer.lua into the Myapi.zip .

Just put it in the \frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\api .


Then we open the Cocos Code IDE Project, configuration properties.

lua->build path->libraries->add External zips.

Add our myapi.zip compression Pack and click "OK".


In this way, you can happily play our custom classes in the Cocos Code IDE.

There are smart tips, is cool AH!!!


3. Add Smart Tips (way two)

Although there are smart hints that can be used in the way above, I later discovered that the namespace I defined was not recognized . Such a result will result in the creation of my. When Panzoomlayer:create () is assigned to the member of Self self.pzlayer , the corresponding function cannot be prompted when the Self.pzlayer continues to be used.

So here we can change the official smart Cue pack Api.zip , to achieve more intelligent hints of the effect.

How and how to do a similar, we first extract the official Cue pack api.zip, it is best to back up a copy.

files in:

\cocos Code ide\configuration\org.eclipse.osgi\bundles\61\1\.cp\resource\cocos2dx-3.2


We then put our custom cue pack My.lua and Panzoomlayer.lua into the API folder.


Then find the Global.lua Global declaration file in the API folder.

declare our custom namespace my to go in.

1 2 --the My module--@field [parent= #global] my#my my preloaded module



save off to compress the API folder into a Api.zip package.

Then refresh our project engineering , and then try our custom class smart cue effect.

It can be found that smart hints for custom classes have been fully aligned with the official Smart hints feature.

You can play happily again!!!



"Problems encountered"


1. Error in script generation

These errors are due to no configuration Pyyaml, Cheetah, Dos2unix caused by the next configuration can be.


2. Error compiling to Android phone

The iOS section will work after the above configuration is complete, but this time the Android will not be passed when compiling .

Because the Register_all_myclass (L) method called inside the AppDelegate.cpp is not present on Android, the Android project is not configured to compile the corresponding PanZoomLayer.cpp file and subsequent generated Lua_ Custom_api_auto.cpp.

So you need to configure the Android.mk file on the Android side to compile the two C + + files when the project compiles.


(1) First configure JNI under the Android.mk file, let the JNI part compile to compile the PanZoomLayer.cpp:

Edit frameworks/runtime-src/proj.android/jni/android.mk

Add after the local_src_files parameter:(note the following \, only the last line without the slash)

.. /.. /classes/panzoomlayer.cpp


Add after the local_c_includes parameter:(note the following \ , only the last line without the slash )

$ (Local_path)/.. /.. /classes


(2) then configure the Frameworks/cocos2d-x/cocos/scripting/lua-bindings/android.mk file.

Add after the local_src_files parameter:(note the following \ , only the last line without the slash )

auto/lua_custom_api_auto.cpp


Add after the local_c_includes parameter:(note the following \ , only the last line without the slash )

$ (Local_path)/.. /.. /.. /.. /runtime-src/classes


(3) then build-runtime, compile the project on the Android side.


(4) If the compilation is successful, you can test it on your Android phone!



Cc3.2+lua (8)--lua Call Custom C + + class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.