Official Tutorial Portal Http://www.cocos2d-x.org/docs/manual/framework/native/wiki/how-to-use-bindings-generator/zh
.. Edit INI file
Modifying the contents of the Py script
Run the PY tool
You can use it.
There is currently no LUA transfer function for C + + class execution
INI file [network_srv]# the prefix to be added to the generated functions. you might or might not use this in your own# templatesprefix = network_srv# create a target namespace (in javascript, this would create some code like the equiv. to ' ns = ns | | {} ') # all classes will be embedded in that namespacetarget_ namespace = 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 -i% (androidndkdir) s/sources/cxx-stl/gnu-libstdc ++/4.8/libs/armeabi-v7a/include -i% (Androidndkdir) s/sources/cxx-stl/gnu-libstdc++/4.8/includeandroid_ Flags = -d_size_t_defined_ clang_headers = -i% (Clangllvmdir) s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -u __sse__cocos_headers = -i% ( Cocosdir) s/cocos -i% (cocosdir) s/cocos/platform/android -i% (cocosdir) s/cocos/editor-support -i% ( Cocosdir) s/external -i% (cocosdir) s/cocos/scripting/lua-bindings/manual -i% (cocosdir) s/external/lua/ lua -i% (Cocosdir) s/external/luacocos_flags = -dandroidcxxgenerator_headers = # extra arguments for clangextra_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 parseheaders = c:/users/hk/ Documents/cocos/cocosprojects/luatest/frameworks/runtime-src/classes/network_srv.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 = network_srv netmgr# 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 " lookupclasses_have_no_parents = # base classes which will be skipped when their sub-classes found them.base_classes_to_skip = # classes that create no constructor# set is special and we will use a hand-written constructorabstract_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
genbindings.py file #!/usr/bin/python# this script is used to generate Luabinding glue codes.# android ndk version must be ndk-r9b.import sysimport os, os.pathimport shutilimport configparserimport subprocessimport refrom contextlib import contextmanagerdef _check_ndk_root_env (): ' Checking the environment NDK_ROOT, which will be used for building ' try: ndk_root = os.environ[' Ndk_root '] except exception: print "Ndk_root not defined. please define ndk_root in your environment. " sys.exit (1) return&nbSp Ndk_rootdef _check_python_bin_env (): " checking the environment PYTHON_BIN, which will be used for building " try: python_bin = os.environ[ ' Python_bin '] except exception: print "Python_bin not defined, use current python." PYTHON_BIN = sys.executable Return python_binclass cmderror (Exception): pass@contextmanagerdef _ PUSHD (Newdir):     PREVIOUSDIR = OS.GETCWD () os.chdir ( Newdir) yield os.chdir (previousdir) def _run_cmd (command): ret = subprocess.call (Command, shell=true) if ret != 0: message = "Error running command" raise cmderror (Message) def main (): cur_platform= '?? ' llvm_path = '?? ' ndk_root = _check_ndk_root_env () # del the " in the path ndk_root = re.sub (r" \ "", "",  NDK _root) python_bin = _check_python_bin_env () platform = sys.platform if platform == ' Win32 ': cur_platform = ' windows ' elif platform == ' Darwin ': cur_platform = platform elif ' Linux ' in platform: cur_platform = ' Linux ' else: print ' your platform is not supported! ' sys.exit (1) if platform == ' Win32 ': x86_llvm_path = os.path.abspath ( Os.path.join (ndk_root, ' toolchains/llvm-3.3/prebuilt ', '%s ' % cur_platform)) else: x86_llvm_path = os.path.abspath ( Os.path.join (ndk_root, ' toolchains/llvm-3.3/prebuilt ', '%s-%s ' % (cur_platform, ' x86 ') )) x64_llvm_path = os.path.abspath (Os.path.join (ndk_root, ' toolchains/ Llvm-3.3/prebuilt ', '%s-%s ' % (cur_platform,&nbSP; ' X86_64 ')) if os.path.isdir (X86_llvm_path): llvm_path = x86_llvm_path elif os.path.isdir (X64_llvm_path): llvm_path = x64_llvm_path else: print ' llvm toolchain not found! ' print ' Path: %s or path: %s are not valid! ' % (x86_llvm_path, x64_llvm_path) sys.exit (1) project_root = os.path.abspath (Os.path.join ( Os.path.dirname (__file__), ' ... ', ')) cocos_root = os.path.abspath (Os.path.join (project_root, ")) cxx_generator_root = os.path.abspath (Os.path.join (project_root, ' Tools/bindings-generator ')) # save config to file config = configparser.configparser () config.set (' DEFAULT ', ' Androidndkdir ', ndk_root) config.set (' DEFAULT ', ' Clangllvmdir ', Llvm_path) config.set (' DEFAULT ', ' Cocosdir ', cocos_root) config.set (' DEFAULT ', ' Cxxgeneratordir ', cxx_generator_root) config.set (' DEFAULT ', ' extra_flags ', ') # To fix parse error on windows, we must difine __wchar_max__ and undefine __mingw32__ . if platform == ' Win32 ': config.set (' DEFAULT ', ' extra_flags ', '-d__wchar_max__=0x7fffffff -u__mingw32__ ') conf_ini_file = os.path.abspath (Os.path.join (Os.path.dirname (__file__), ' Userconf.ini ') print ' Generating userconf.ini ' with open (Conf_ini_file, ' W ') as configfile: config.write (configfile) # set proper environment variables if ' Linux ' in platform or platform == ' Darwin ': os.putenv (' Ld_library_path ', '%s/libclang ' % cxx_generator_root) if platform == ' Win32 ': path_env = os.environ[' path '] os.putenv (' path ', r '%s;%s\libclang;%s\ Tools\win32; ' % (path_env, cxx_generator_root, cxx_generator_root) try: tolua_root = '%s/tools/tolua ' % project_root output_dir = ' c:/users/hk/documents/cocos/cocosprojects/luatest/ Frameworks/runtime-src/classes ' cmd_args = {' Network_srv.ini ' : (' network_srv ', ' Lua_network_srv_auto ') } target = ' Lua ' generator_py = '%s/generator.py ' % cxx_generator_root for key in cmd_args.keys (): args = cmd_args[key] cfg = '%s/%s ' % (tolua_root, key) print ' generating bindings for %s ' % (key[:-4]) command = '%s %s %s -s %s - t %s -o %s -n %s ' % (Python_bin, generator_py, cfg, args[0],  TARGET, OUTPUT_DIR, ARGS[1]) _run_cmd (command) if platform == ' Win32 ': with _pushd (Output_dir): _run_cmd (' Dos2unix * ') print '---------------------------------' print ' Generating lua bindings succeeds. ' print '---------------------------------' except exception as e: if e.__ class__.__name__ == ' Cmderror ': print '---------------------------------' print ' Generating lua bindings fails. ' print '-------------------------- -------' os.system ("pause") else: raise# -------------- main --------------if __name__ == ' __main__ ': main ()
Test code
network_srv.h file #ifndef network_srv___ #define network_srv___#include "Cocos2d.h" # include "Network/httprequest.h" #include "network/httpclient.h" #include "network/httpresponse.h" using namespace cocos2d;using namespace cocos2d::network;using namespace std;class network_srv :p Ublic cocos2d::ref{public:create_func (network_srv); Bool init () { return true; }network_srv () {This->retain ();} Void run ();}; #define URL_BASE "http://127.0.0.1:8080/cocos/"//#define URL_BASE "HTTP/ qq771911064.oicp.net:8080/cocos/"#define DATA_MAX_LENGTH 100class NetMgr{public:static Netmgr*getinstance ();/** * @brief new a HttpRequest * @param action such as "Login?name=1&&pass=1" * @ */void newrequest (const char* action);/** * @brief  IF&NBsp;isdone please call this to get data * @return data From srv * @ */const char *getdata ();/** * @brief check network is done or not * @return 0 is ' t completed * @ Retuen 1 completed */int isdone ();p rivate:netmgr (); Int __isdone;/*0 is ' s done,1 done*/char _data[data_max_length];}; #endif //
CPP code #include "Network_srv.h" Void network_srv::run () {netmgr::getinstance ()->newrequest ("Login? Name=1&&pass=1 ");} Void netmgr::newrequest (const char* action) {__isdone = 0;string url = url_base;url += action; Httprequest*request = new httprequest;request->seturl (Url.c_str ());request-> Setrequesttype (Httprequest::type::get); Request->setresponsecallback ([=] (httpclient*client, Httpresponse *respone) {if (Respone->getresponsecode () != 200) return;vector<char>* buffer = respone->getresponsedata (); Cc_assert (Data_max_length > buffer->size (), " NetMgr buffer size Overfloaw ");for (Int i = 0; i < buffer->size (); i++) {_data[i] = (*buffer) [i];} _data[buffer->size ()] = ' + '; __isdone = 1;}); Httpclient::getinstance ()->settimeoutforconnect (10);Httpclient::getinstance ()->send (request); Request->release ();} Netmgr* netmgr::getinstance () {static netmgr* _netmgr__ = 0;if (_netmgr__ == 0) {_netmgr__ = new netmgr;} return _netmgr__;} Const char * netmgr::getdata () {__isdone = 0;return _data;} Int netmgr::isdone () {return __isdone;} Netmgr::netmgr () {__isdone = 0;}
Lua code local function menucallbackclose () if Netmgr:getinstance (): IsDone () ==1 then Release_print ("recv Data ". Netmgr:getinstance (): GetData ()) End end--add handler for close item local Menutoolsitem = Rootnode:getchi Ldbyname ("button_1") Menutoolsitem:addtoucheventlistener (menucallbackclose) SRV = Network_srv:create (); Srv:run ();
COCOS2DX exporting C + + classes for LUA use