Lua encapsulated into C + + style class __c++

Source: Internet
Author: User
Tags lua

Forwarding, please keep address: http://blog.csdn.net/stalendp/article/details/8920018

The process of using LUA for development in COCOS2DX will certainly involve an expansion of the LUA functionality, which already has articles on the web, but not so straightforward. So I have written this article deliberately, but also as a summary of their own learning. Here I will use eclipse to develop LUA. Using eclipse to develop LUA, you can use its powerful scalability to dramatically accelerate development. The article will describe how to customize a tool to generate LUA and make calls. Development Tools Download

There are two ways to make Eclipse development Lua: 1 Install LDT 2 to download the integrated eclipse;

LDT installation will not be introduced, you can go to the Internet to search for the Eclipse plug-in installation tutorial; The official also has the corresponding explanation: http://www.eclipse.org/koneki/ldt/#documentation

If you want to install C + + related features, you also need to install CDT,CDT's official as follows: http://www.eclipse.org/cdt/downloads.php


the creation of the projectCreate a project called Luatester; Then edit the Main.lua and run the results as follows:

Debug Interface:

Configure the tolua++ toolTo develop LUA in COCOS2DX, you need to use tolua++ to generate files. There are already related tutorials on the internet, but they are not very comprehensive and the operation is not very convenient. Here I use Eclipse's powerful tool-configuration capabilities to customize tolua++ to make development more efficient.
Click the menu run->external Tools-> External tools configurations ...; The pop-up dialog box appears as shown in the following illustration:

Add a tool under the program, called Luamaker Bar; configuration as above. A few notes (assuming COCOS2DX's root directory is/users/shhqw132/documents/cocos2d-2.1rc0-x-2.1.3): 1 There are tolua++ tools in the tools/tolua++ directory, The default is packaged (Windows is packaged into a RAR file; The Mac is packaged into a zip file). According to the situation decompression one to the tools/tolua++; in the figure above, the tool's location is positioned to the executable file that was just reduced. 2 The address of some variables built into eclipse. Here we use the variable ${resource_loc}, which is the absolute path of the currently selected resource in the system. 3 The tolua++ command format is as follows:./tolua++-L basic.lua-o xxx.cpp xxx.pkg; L: Specifies the location of the corresponding Lua file (for modifying the generated CPP file, COCOS2DX is used to fix the files produced by tolua++;-o Specifies the name of the generated CPP file; xxx.pkg is the rule file. The role of tolua++ is to read the xxx.pkg file and then combine the LUA specified in-L (to modify the generated code) to eventually generate the CPP file. In the arguments of the previous figure, add the corresponding tolua++ parameters.
For more information on-L. Because tolua++ does not support callback functions, and COCOS2DX uses callback functions such as Registerscripthandler, COCOS2DX makes some fix for code generated by tolua++. These fix rules are in tools/ Defined in Tolua++/basic.lua. So in order to support callback functions, use this file.
OK, the tool is configured to complete.
Integrated DevelopmentCreate a Cocos2d_lua project, named LuaTest01. Create a Pkg subfolder under the project's Classes folder, and then introduce it into the project (I added it in a reference way). Then create a MyTest.h and form a mytest.pkg file, as shown below:
MyTest.h

#ifndef luatest01_mytest_h
#define LUATEST01_MYTEST_H

#include "cocos2d.h"
using_ns_cc;

Class Mytest:public Cclayer {
private:
    int invokehandler;
Public:
    MyTest (): Invokehandler (0) {}
    
    Create_func (MyTest);
    
    void SetHandler (int _handler)   {
        this->invokehandler = _handler;
    }
    
    void Callfunc () {
        ccluastack* pstack = ccluaengine::d efaultengine ()->getluastack ();
        Pstack->pushint (123);
        Pstack->executefunctionbyhandler (This->invokehandler, 1);
    }
    
    void SayHello () {
        cclog ("Hello, world!");
    }
    
    static void ClassInfo () {
        Cclog ("This class is MyTest");
    }
    
    void Otherfunc () {
        //this function doesn ' t expose to the LUA
    }
};

#endif
The methods to be exposed in the MyTest.cpp are integrated into the mytest.pkg, as follows:
Class Mytest:public Cclayer {  
    static mytest* create ();
    
    void SetHandler (Lua_function _handler);
    
    void Callfunc ();
    
    void SayHello ();
    
    static void ClassInfo ();
Note points: 1 because the PKG does not recognize the macro, so the Create_func (MyTest), expand into static mytest* create (); 2 The callback function is a pointer type (that is, an int), However, in pkg you want to declare the custom type as Lua_function。 It is then processed in an extension of cocos2dx (which can be found in the Basic.lua file).
Create the LUA project in Eclipse and refer to the Pkg folder in Xcode. The following figure:



The final effect is as follows:
Right-click to select TextEditor, you can open mytest.pkg for editing.

Keep Mytest.pkg as selected, then click Luamaker on the toolbar. The MyTest.pkg.cpp file will be generated under the Pkg folder. In Xcode, drag the MyTest.pkg.cpp file to the project, as shown in the following figure:
To add a header file to MyTest.pkg.cpp:
#include "MyTest.h"

Copy the declaration of the Tolua_mytest_open function in MyTest.pkg.cpp to MyTest.h.
/* Exported function *
/TOLUA_API int  tolua_mytest_open (lua_state* tolua_s);
and introduce the following header file into the MyTest.h:
extern "C" {
#include "tolua++.h"
#include "tolua_fix.h"
}
#include "CCLuaEngine.h"

Register your custom C + + functions in the game startup code so that these functions can be used in Lua. Reference the header file MyTest.h in AppDelegate.cpp, and add the following code to the Appicationdidfinishlaunching method:
Tolua_mytest_open (Pengine->getluastack ()->getluastate ());
Compile it. OK, when we're done, we write the C + + end code, which is registered in the LUA environment through Tolua_mytest_open. The next thing to do is to write the Lua-side code. Switch to Eclipse and, as in the above method, introduce the resources directory in the Xcode project in Eclipse, as shown in the following figure:
Double hit Open Hello.lua, add the following code in the main function:
	Cclog ("====test static method=====");
	Mytest:classinfo ();
	Cclog ("====test member function========");
	Local MT = Mytest:create ();
	Mt:sayhello ();
	Cclog ("====test callback function=====");
	The local function callback (num)
		cclog ("This is" function from Lua, and Param is: ".) num);
	End
	Mt:sethandler (callback);
	Mt:callfunc ();
	
The results are as follows:

After the preparation is done, the next step in adding a function to the MyTest file is simple. 1 Modify pkg file in Eclipse, select its file, execute Luamaker command, 2 open MyTest.pkg.cpp in Xcode, add MyTest.h header file, compile 3) in Eclipse, edit the Lua file and use the newly added function. Ok,enjoy yourself.
If you encounter a coding problem, right-click the file-properties; Select the encoding in the pop-up dialog box, as shown in the following figure:


Reference article: http://blog.linguofeng.com/archive/2013/03/19/cocos2d-x-lua-callback-function.html
http://blog.csdn.net/xiaominghimi/article/details/8770396
http://www.cocos2d-x.org/boards/11/topics/4222



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.