Cocos2d-x 3.6 lua, cocos2d-xlua

Source: Internet
Author: User

Cocos2d-x 3.6 lua, cocos2d-xlua
Differences between cocos2d-x 2.x and cocos2d-x 3.x (tolua ++)


Cocos2d-x in 2. in Version x, toLua ++ and. pkg file so register yourself into the Lua environment, but from cocos2d-x 3. starting from x, toLua ++ is replaced by the bindings-generator script.


The operating mechanism of the bindings-generator script is as follows:
1. You don't need to write. pkg and. H files. Just define an INI file and register the module name in the Lua environment.
2. Find out the toLua ++ tool generation method, change the Python script to dynamically analyze the C ++ class, and automatically generate a bridge. h and. cpp Code, do not call the tolua ++ command

3. Although the tolua ++ command is no longer called, the underlying layer still uses the toLua ++ library function, such as tolua_function, the code generated by the bindings-generator script is almost the same as that generated by the toLua ++ tool.


The bindings-generator script has the initiative to generate toLua ++ Bridge Code, which not only saves a lot of time. pkg and. h file, and can better insert custom code, to achieve some special purpose in the cocos2d-x environment, such as memory recycling, so the cocos2d-x from 3. x gave up toLua ++ and. pkg switched to the self-written bindings-generator script, which is very commendable.

The following describes how to use the bindings-generator script:
1, write your own C ++ class, according to The cocos2d-x rules, inherit cocos2d: Ref class, in order to use the memory recycling mechanism of the cocos2d-x.
2. Compile A. ini file so that bindings-generator can know how to expose the C ++ class according to the configuration file.
3. Modify the bindings-generator script to read the. ini file.
4. Run the bindings-generator script to generate a Bridge C ++ class method.
5. Use VS2012 to add the custom C ++ class and the generated bridge file to the project. Otherwise, the compilation fails.
6. Modify AppDelegate. cpp and execute the bridge method. The custom C ++ class is registered in the Lua environment.

The first is the custom C ++ class. I am used to saving files in the frameworks/runtime-src/Classes/directory:

Frameworks/runtime-src/Classes/MyClass. h

#include "cocos2d.h"using namespace cocos2d;class MyClass : public Ref{public:  MyClass()   {};  ~MyClass()  {};  bool init() { return true; };  CREATE_FUNC(MyClass);  int foo(int i);};
Frameworks/runtime-src/Classes/MyClass. cpp

#include "MyClass.h"int MyClass::foo(int i){  return i + 100;}
Then write the. ini file. In the frameworks/cocos2d-x/tools/tolua/directory, you can see the genbindings. py script and a bunch of. ini files, which are the actual execution environment of bindings-generator. Find a. ini file with less content, copy it, and rename it MyClass. ini. Most of the content can be merged without modification. Here we only list the important parts that must be modified:
Framework/cocos2d-x/tools/tolua/MyClass. ini

[MyClass] prefix = MineClass # Add the prefix XX, register the file header and register the function name (XX), and name target_namespace = my # space. xxx, xxx is the custom class method headers = % (cocosdir) s /.. /runtime-src/Classes/MyClass. h # obtain the file header classes = MyClass for the custom class # You need to register the class YY (method) and register the name of the function in combination with the name (XX) _ YY.
Framework/cocos2d-x/tools/tolua/genbindings. py
cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \            'MyClass.ini' : ('MyClass', 'lua_MyClass_auto'), \            ...
(In fact, this step can be omitted, as long as the genbindings. py script to automatically search for all INI files in the current directory on the line, do not know the future of the cocos2d-x team will be so optimized)
Now, the preparation for generating the bridge file is ready. Run the genbindings. py script:

python genbindings.py
After the genbindings. py script is successfully executed, the new file is displayed in the frameworks/cocos2d-x/cocos/scripting/lua-bindings/auto/directory:



NOTE: If python reports an error, check whether the yaml and Cheetah packages are missing. If so, the installation package is ready.

The following is a brief description of lua_MyClass_auto.cpp.

TOLUA_API int register_all_MineClass (lua_State * tolua_S) {// entry point, which creates the managed internal variable tolua_open (tolua_S); // creates the new module tolua_module (tolua_S, "my ", 0); // register a module or class tolua_beginmodule (tolua_S, "my"); // register the class lua_register_MineClass_MyClass (tolua_S); tolua_endmodule (tolua_S); return 1 ;}
Registration type:

Int lua_register_MineClass_MyClass (lua_State * tolua_S) {tolua_usertype (tolua_S, "MyClass"); // tolua_cclass (tolua_S, "MyClass", "MyClass", "cc. ref ", nullptr); // registration class tolua_beginmodule (tolua_S," MyClass "); // registration module tolua_function (tolua_S," new ", lua_MineClass_MyClass_constructor ); // bind the function (bind "new" of MyClass object in Lua to your lua_MineClass_MyClass_constructor () function .) tolua_function (tolua_S, "init", identifier); tolua_function (tolua_S, "foo", identifier); tolua_function (tolua_S, "create", identifier); tolua_endmodule (tolua_S); std:: string typeName = typeid (MyClass ). name (); // Save the registration class g_luaType [typeName] = "MyClass"; g_typeCast ["MyClass"] = "MyClass"; return 1 ;}
To bind a function, note the following:

Cobj = (MyClass *) tolua_tousertype (tolua_S,); // the object in the data stack is popped up as a pointer to (CTest.
Pop up an object from the stack (in fact a custom Class Object), and then press the result returned by the object reference method (including parameters and handle) to implement mutual calls between c and lua.


2. Compile and run

Open the Classes/lua_module_register.h file and add the header file.

#include "tolua++/lua_MyClass_auto.hpp"
Add the registration function in static int lua_module_register (lua_State * L ).

register_all_MineClass(L);
If the vs2012 compilation is incorrect, it is estimated that the source file and the generated file are not added to the project; if there is special processing, such as replacing genbindings. the file path generated by the py script. Note that in the vs2012 environment-> properties-> c/c ++-> Add the include directory and add the path.

Lua code:

Function myadd (x, y) -- custom local test = my. MyClass: create () print ("lua bind:" .. test: foo (99) return x + yend
Compile and run:



Reference blog: http://segmentfault.com/a/1190000000631630

Article: http://cn.cocos2d-x.org/tutorial/show? Id = 2496

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.