Cocos2dx-lua binding custom c ++ class (1)

Source: Internet
Author: User

This article describes how to bind a custom c ++ class to lua on mac.

1. Tools first

Find your cocos2d-x/tools/tolua ++, where files are roughly divided by type:

(1) *. pkg: used to define the c ++ class to be bound and the interfaces to be exposed.

(2) *. lua: used to define the conversion rules used for the generated lua binding file.

(3) *. sh: Use tolua ++ to output the class defined in. pkg as A. cpp file containing the bound interface according to the rules in. lua.

(4) README file. This is important and involves the specific rules used by pkg files.

2. pkg

(1) A total pkg file is usually defined for the project, which contains the pkg file of the specific class used in the project. Test_project.pkg:

1 $ pfile "class_A.pkg" 2 $ pfile "class_ B .pkg"View Code

1 2) remove CC_DLL for the class defines, 3 4 5 6 7) remove memeber functions that declared as private or protectedView Code

You can understand it at a Glance. Simple translation is as follows:

1 2 3 4 5 6 7) Remove the method that is limited to private or protectedView Code

For example, class_A.pkg:

Static std: string test1 (const char * name) void test2 ()}View Code

 

3. lua

For more information, see basic. lua or basic_cocostudio.lua. You can copy the file and change it to test_project.lua. Explain the following:

(1) CCObjectTypes is an array containing the type name to be bound.

Local CCObjectTypes = "class_A", "class_ B ",}View Code

(2) The following is a long section about binding interface files and automatically generated rules. Focus on # include.

Replace (# include "stdlib. h "# include" string. h "# include" tolua ++. h "], extern" C "# include" tolua_fix.h "# include" class_A.h "# include" class_ B .h "])View Code

 

4. sh

You can copy a basic. sh file directly, and modify the corresponding file name:. lua,. cpp,. h,. pkg

# Invoked build. xml, SCRIPT_DIR = $ (cd "$ (dirname" $ {BASH_SOURCE }") "TOLUA = if TOLUA = 'which tolua ++ 5.1 if echo" Unable to find tolua ++ (or tolua ++ 5.1) in your PATH. "exit 1 test_project.pkgView Code

Briefly explain the meaning of each parameter:

•/Tolua ++: lua binding tool •-L: lua file (. lua) •-o: generate a file (. cpp) •-H: generate the header file (. h) • If the pkg file is used and the script is running, the system prompts "tolua ++ cannot be found in the environment ". You can write $ {TOLUA} directly. /tolua ++ Summary: in order not to be confused with the content that comes with the cocos2d-x, under your cocos2d-x/tools/tolua ++/path, create the directory "your project name ", then, configure the pkg, lua, and sh content according to the above method. Run sh. The following is a script I changed. After tolua ++ runs, the path of the generated bound code file is opened. # Invoked build. xml, SCRIPT_DIR = $ (cd "$ (dirname" $ {BASH_SOURCE} ")" open.../../scripting/lua/cocos2dx_supportView Code bound cpp file, how to use in the project, see: cocos2dx-lua binding custom c ++ class (2)

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.