COCOS2DX 2.x version: Simplified refinement tolua++ binding custom classes to LUA use

Source: Internet
Author: User

COCOS2DX's 3.x version has provided a better way to bind, there are many related tutorials on the web, here is a link: http://www.cocoachina.com/bbs/read.php?tid=196416.

   Because our project currently uses 2.x version, and has already developed half, this time does not fit to turn 3.x version, therefore can only use the 2.x old method tolua++ to implement the binding. This has a problem of egg ache, too troublesome! Every time you write pkg, use the Tolua++.exe interpreter to build a new CPP, add your own class header file reference, and then overwrite the new CPP file in the Liblua before, and then compile the library, generate new obj, is finished. Here is the article about the great God of wood, which is clearly written: http://blog.csdn.net/musicvs/article/details/8166572/.

   But I don't want to be in such trouble every time, I want to add a bound interface to my project so that I can just update my own project each time I run it, and I'll let Lua identify my class. The idea is to put the "LuaCocos2d.cpp" class into the project, which is entirely possible, that is, our new binding is no longer added to the "LuaCocos2d.cpp" here, but a separate generation of a new, only our own binding class interface of a class to put into the project. All right, crap, no more talking, look at the flow.

1 "First create a folder in our own project that specifically stores the interface classes for LUA and related files: Luaapi

  

2 "Create a pkg file, this file we only create one, as a base call to use for tolua++, we will explain in detail later

  

3 "Copy the three files in the cocos2d-x-2.2.3\tools\tolua++ directory to 1" in the file created, this is the tool used to generate, this can not be less

  

The role of these files in the relevant binding articles are described in detail, here a little bit:

①Basic.lua The additional rules of the tolua++ interpreter when parsing the pkg file

The ②build.sh script command, used to start the tolua++ interpreter parsing pkg generates the CPP file we need. This needs to be changed, because a copy is in this directory, so the path is changed, here to pay attention.

#!/bin/bash## invoked Build.xml, overriding the lolua++ propertyscript_dir=$ (CD "$ (dirname" ${bash_source[0]} ")" && Amp PWD) tolua= ' D:\cocos2d-x-2.2.3\projects\actgamelua\Classes\LuaAPI ' if [-Z ' ${tolua} "]; Then    tolua= ' D:\cocos2d-x-2.2.3\projects\actgamelua\Classes\LuaAPI ' fiif [-Z ' ${tolua} ']; then    echo "unable To find tolua++ (or tolua++5.1) in your PATH. "    Exit 1ficd ${script_dir}${tolua}-L Basic.lua-o "Api/luaapi.cpp" api.pkg

Here is a "api/luaapi.cpp", this is the CPP file we specified to generate, that is, the build LuaAPI.cpp is placed in the "api/" directory (under the Luaapi folder we created)

③build.bat Batch process, is actually the implementation of build.sh settings, the same, the path to change:

tolua++-L Basic.lua-o "Api/luaapi.cpp" api.pkg

④tolua++.exe interpreter, used to parse the PKG-generated CPP

4 "At this time, the tool is ready, the command is also written, and then we want to define a class, this class is similar to the pkg description file, but later our binding class are written in the format pkg in this inside, rather than write a lot of pkg, and add to cocos2d.pkg.

Create a header file in the API directory: PkgDiscription.h

  

#ifndef _test_property_h__#define _test_property_h__#include "cocos2d.h" using_ns_cc;/*  * below "tolua_begin" and " Tolua_end "These two lines of comment must not be less, because they are the beginning and end of the recognition  *///Tolua_begin//@pkg1_begin First PKG profile begins class Myproperty:public ccobject{    myproperty (void);    ~myproperty (void);    void Setuniqueid (int id);    int Getuniqueid ();    void Setrolename (const char *name);    Const char* getrolename ();};/ /@pkg1_end//@pkg2_begin//The PKG description of the next custom class is stored here//@pkg2_end//Tolua_end#endif
So what's the use of creating this class: for tolua++ to parse and compile, and tolua++ only to compile pkg, so we're going to add this class to Api.pkg .
typedef unsigned char       byte;typedef int                 bool;typedef unsigned long       dword;typedef unsigned short      WORD; typedef char*               lpstr;typedef const char*         LPCSTR; $cfile "Api/pkgdiscription.h"
5 "After the previous four steps, now directly click on the" BUILD.bat ", will be based on our written" PkgDiscription.h ", generate the" LuaAPI.cpp "file we want, is in the api/directory

  

There's a function interface that we declare in PkgDiscription.h to bind. But found that there are errors!!

  The reason: The custom class MyProperty written in PkgDiscription.h is not our real custom class, it's just a pkg description, so the immediate solution is to introduce our header file in LuaAPI.cpp " MyProperty.h "

  But this is not good, every time the generation need to introduce the header file again, trouble, but the PKG provides a way to introduce a header file when generated, is to add a header file in the api.pkg .

typedef unsigned char       byte;typedef int                 bool;typedef unsigned long       dword;typedef unsigned short      WORD; typedef char*               lpstr;typedef const char*         LPCSTR; $cfile "Api/pkgdiscription.h" $ #include "MyProperty.h"

But this is not good, because each has a new need to bind the custom class, we need this api.pkg in the introduction, so troublesome, then we will create a total header file class, specifically put the header file: PkgHeadFiles.h

Code:

#ifndef __pkg_head_files_h_#define __pkg_head_files_h_#include "MyProperty.h"//Here introduce the classes defined in our project (Ready for LUA) #endif
so in the api.pkg,
typedef unsigned char       byte;typedef int                 bool;typedef unsigned long       dword;typedef unsigned short      WORD; typedef char*               lpstr;typedef const char*         LPCSTR; $cfile "Api/pkgdiscription.h" $ #include "PkgHeadFiles.h"
6 "It's almost done here, but there's one more step, which is how we use the LuaAPI.cpp file for Lua, so we just need to know how the LuaCocos2d.cpp of the engine itself is being used for LUA."

   key points in Ccluastack, look at the init () function in the CCLuaStack.cpp file, modify the init () function

BOOL Ccluastack::init (void) {    //....    Tolua_cocostudio_open (m_state);        A new sentence code    tolua_api_open (m_state);        #if (Cc_target_platform = = Cc_platform_ios | | Cc_target_platform = = Cc_platform_mac)    ccluaobjcbridge::luaopen_luaoc (m_state); #endif    //...    return true;}

Directly add will error, said can not find this function, then the new addition of Tolua_api_open where, in fact, in our generated LuaAPI.cpp, so for convenience, we write a LuaAPI.h, the inside of the statement of this function (for convenience only)

LuaAPI.h Code:

#ifndef __lua_api_h_#define __lua_api_h_#ifdef __cplusplusextern "C" {#endif # include "Tolua++.h" #ifdef __cplusplus}# Endiftolua_api int Tolua_api_open (lua_state* tolua_s); #endif
and introduce this LuaAPI.h in the Ccluastack.

#include "CCLuaStack.h" #include "luaapi/api/luaapi.h"//the path here, because I have introduced the relevant directory in the Liblua Project library directory, so with a relative path

Well, here's the file in our API directory:

After the completion of the project two header files can be deleted, now it is only convenient to write.

In this way, each time you need to modify the file, we introduce our custom class in "PkgHeadFiles.h" and add the pkg description in "PkgDiscription.h".then run the BUILD.bat and you can。 are all operating in our own projects, uploading SVN.

COCOS2DX 2.x version: Simplified refinement tolua++ binding custom classes to LUA use

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.