Cocos2dx--lua Automatic and manual binding

Source: Internet
Author: User

"Auto Bind"

Reference: http://my.oschina.net/skyhacker2/blog/298397

Mainly through the engine comes with the Tools/tolua, the main steps are as follows:

1. Write the C + + class to be exported, if it is added in libcocos2d, you need to add the export tag: class Cc_dll Test

2. To the Tolua directory, configure the environment according to the Readme.mdown:

* Make sure so you had installed ' android-ndk-r9b '. * Download python2.7.3 (32bit) from (Http://www.python.org/ftp/pytho N/2.7.3/python-2.7.3.msi). * ADD the installed path of Python (e.g. C:\Python27) to Windows environment variable named ' PAT H '. * Download Pyyaml from Http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe and install it.* Download Pycheetah from Https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages" * Set environment variables (' ndk_root ') * Go to "Cocos2d-x/tools/tolua" folder, and Run "G Enbindings.py ". The generated codes would be under "cocos\scripting\auto-generated\js-bindings".

3. Create an INI file, such asCocos2dx_custom.ini, refer to the other INI to make some changes yourself:

# The prefix to being added to the generated functions. You might or might not the use this in your own# Templatesprefix = cocos2dx_custom# Create a target namespace (in JavaScript, This would create some code like the equiv. to ' NS = NS | | {} ') # All classes'll be embedded in, Namespacetarget_namespace = cc# what headers to Parseheaders =% (Cocosdir) S/COC os/for_lua/test.h# what classes to produce code for. You can use the regular expressions here. When testing the regular# expression, it'll be is enclosed in "^$" and like this: "^menu*$". Classes = Test.*skip = # classes F Or which there would be no "parent" lookupclasses_have_no_parents = Testabstract_classes =
4. Copy an genbindings_custom.py and modify the Cmd_args:

Cmd_args = {' Cocos2dx_custom.ini ': (' cocos2dx_custom ', ' Lua_cocos2dx_custom_auto '),                    }
5. Runninggenbindings_custom.py will generate xx_auto.h/cpp to Cocos\scripting\lua-bindings\auto directory and then you add Libluacocos2d project to the engine to go

6. To be used in Lua, it is also registered at startup. Now in the 3.7 version appdelegate::applicationdidfinishlaunching will call Lua_module_register, so:

#include "lua_cocos2dx_custom_auto.hpp" #include "fun.h" int lua_module_register (lua_state* L) {    register_ Cocosdenshion_module (l); Register_all_cocos2dx_custom (L); Register_foo (l    ); return 1;}
7. Then you can use it in Lua:

--Test Custom local msg = CC. Test:hellomsg () print (msg)

"Manual Binding"

Reference: http://www.tairan.com/archives/5493

1. Create a C + + class (Fun.h):

#pragma once#include <iostream> #include <sstream>extern "C" {#include <lua.h> #include <lauxlib.h > #include <lualib.h>}class foo{public:foo (const std::string & name): Name (name) {std::cout << "Foo is Born "<< Std::endl;} std::string Add (int a, int b) {std::stringstream ss;ss << name << ":" << a << "+" << b < ;< "=" << (a + b); return Ss.str ();} ~foo () {std::cout << "Foo is Gone" << Std::endl;} private:std::string name;}; void Register_foo (Lua_state *l);
2. Export to Lua (FUN.CPP):

#include "fun.h" int l_foo_constructor (lua_state *l) {const char *name = lual_checkstring (l, 1); Foo **udata = (foo**) lua_newuserdata (l, sizeof (foo*)), *udata = new Foo (name), lual_getmetatable (L, "Lual_foo");//Stack :// -1metatable "Lual_foo"// -2userdata// -3string paramlua_setmetatable (L,-2); return 1;} foo* L_checkfoo (lua_state *l, int n) {return * (foo**) Lual_checkudata (l, N, "Lual_foo");} int L_foo_add (lua_state *l) {Foo *foo = L_checkfoo (l, 1); int a = Lual_checknumber (l, 2); int b = Lual_checknumber (L, 3); STD :: String s = Foo->add (A, b); Lua_pushstring (L, S.c_str ());//stack://-1 result string//-2 metatable "Lual_foo"//-3 US erdata//-4 string Paramreturn 1;} int L_foo_destructor (lua_state *l) {Foo *foo = L_checkfoo (l, 1);d elete Foo;return 0;} void Register_foo (Lua_state *l) {Lual_reg sfoorefs[] = {"New", L_foo_constructor},{"Add", L_foo_add},{"__gc", L_Foo_ destructor},{null, null}};lual_newmetatable (L, "Lual_foo"), Lual_register (L, NULL, sfoorefs); Lua_pushvalue (L,-1);// Stack:// -1:metatable "Lual_foo"// -2:metatable "Lual_foo"//This pops the Stacklua_setfield (L,-1, "__index"); Lua_setglobal ( L, "Foo");}
3. Register at startup

4. Use in Lua:

function Foo:speak () print ("Hello, I am a Foo") end local Foo = Foo.new ("Adfan") Local m = Foo:add (3, 4) print (m) foo:speak ()  foo.add_ = Foo.add function Foo:add (A, B) return "Magic:". Self:add_ (A, b) end m = Foo:add (9, 8) print (m)

Description: Manual binding, the reference engine to export those manual.cpp, with Tolua of those interfaces is very convenient, using the above method is mainly to show this process

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2dx--lua Automatic and manual binding

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.