【自己主动绑定】

參考:http://my.oschina.net/skyhacker2/blog/298397

主要是通过引擎自带的tools/tolua,主要过程例如以下:

1.编写好要导出的c++类。假设是libcocos2d里加入,须要加入导出标记:class CC_DLL Test

2.到tolua文件夹依据README.mdown配置好环境:

* Make sure that you have installed `android-ndk-r9b`.
* Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* 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 "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\js-bindings".

3.创建一个ini文件,比方cocos2dx_custom.ini,參照其它的ini自己做一些改动:

# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = cocos2dx_custom # create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = cc # what headers to parse
headers = %(cocosdir)s/cocos/for_lua/Test.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 = Test.* skip = # classes for which there will be no "parent" lookup
classes_have_no_parents = Test abstract_classes =

4.拷贝一份genbindings_custom.py。改动cmd_args:

cmd_args = {'cocos2dx_custom.ini' : ('cocos2dx_custom', 'lua_cocos2dx_custom_auto'),\
}

5.执行genbindings_custom.py会生成xx_auto.h/cpp到cocos\scripting\lua-bindings\auto文件夹,然后你加入到引擎的libluacocos2dproject去

6.要在lua中使用,还在启动时注冊。如今3.7的版本号里AppDelegate::applicationDidFinishLaunching会调用lua_module_register,所以:

#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.然后你就能够在lua里使用了:

-- test custom
local msg = cc.Test:helloMsg()
print(msg)

【手动绑定】

參考:http://www.tairan.com/archives/5493

1.创建c++类(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.导出到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:
// -1 metatable "luaL_Foo"
// -2 userdata
// -3 string param
lua_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 userdata
// -4 string param return 1;
} int l_Foo_destructor(lua_State *L) {
Foo *foo = l_CheckFoo(L, 1);
delete 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 stack
lua_setfield(L, -1, "__index"); lua_setglobal(L, "Foo");
}

3.启动时注冊

4.在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)

说明:手动绑定的话。參照引擎导出的那些manual.cpp就可以,用tolua的那些接口非常方便。用上面的这样的方式主要是展示这个流程

cocos2dx——lua自己主动和手动绑定的更多相关文章

  1. cocos2dx lua 绑定之二:手动绑定自定义类中的函数

    cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...

  2. [cocos2dx笔记008]cocos2d 用luabridge手动绑定类

    基于cocos2dx 2.2.2版本号.这几天使用了cocostudio实现了,动画.骨骼动画.UI编辑.粒子效果,尽管有些不足,但已经算是很好了.今天尝试用lua.这个很easy.创建的时候.设置语 ...

  3. cocos2dx lua 绑定之一:自动绑定自定义类中的函数

    cococs2dx 3.13.1 + vs2013 + win10 1.首先定义C++类Student 在cocos2d-x\cocos文件夹下新建一个user_define的文件夹放置两个文件. 注 ...

  4. 【cocos2d-x + Lua(1) 绑定Lua并使用tolua++】

    为什么要使用Lua进行游戏开发?转载请注明出处http://www.cnblogs.com/zisou/p/cocos2dx-lua1.html 上面一个问题我觉得在我们使用Lua之前需要深入思考的, ...

  5. 开源基于lua gc管理c++对象的cocos2dx lua绑定方案

    cocos2dx目前lua对应的c++对象的生命周期管理,是基于c++析构函数的,也就是生命周期可能存在不一致,比如c++对象已经释放,而lua对象还存在,如果这时候再使用,会有宕机的风险,为此我开发 ...

  6. cocos2d-x lua绑定解析

    花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...

  7. Cocos2d-x JSB 自己主动绑定bindings

    Javascript Binding (简称JSB) 自己主动绑定教程. Cocos2d-x JSB 自己主动绑定bindings-generator (以下简称B-G) 使用心得 假设想弄清深入原理 ...

  8. [cocos2dx] lua注册回调到c++

    思路 像所有语言一样,绑定回调主要是执行的任务执行到特定情形的时候,调用对用回调方法. 这里也一样.核心思路是,当c代码执行到特定特定情形的时候,调用lua的方法 我这里使用的是用lua_stack直 ...

  9. cocos2d-x lua 中使用protobuf并对http进行处理

    cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...

随机推荐

  1. 同门不同类—创新Aurvana Live2/Air简评(附随身视听设备心路历程)

    (注,本文把live2/air并成一起写的,同时本人是木耳,请轻拍) 本命年各种坏东西,很是无语,终于坏到耳塞耳机了来了,之前用的拜亚DT235无缘无故就一边不响了,无奈只能扔了. 纠结了好几个月,终 ...

  2. lsof---查看你进程开打的文件

    lsof命令用于查看你进程开打的文件,打开文件的进程,进程打开的端口(TCP.UDP).找回/恢复删除的文件.是十分方便的系统监视工具,因为lsof命令需要访问核心内存和各种文件,所以需要root用户 ...

  3. POJ1158 城市交通Traffic lights IOI 1999 (最短路)

    POJ1158 城市交通Traffic lights IOI 1999 (最短路) (1) 问题描述(probolem) 在d城里交通的安排不同寻常,城中有路口和路口之间的道路,再任意两个不同的路口之 ...

  4. java关闭资源,自制关闭资源工具类

    在网上看到一篇关于关闭资源的正确方式:http://blog.csdn.net/bornforit/article/details/6896775 该博文中的总结: (1)使用finally块来关闭物 ...

  5. jfinal 后台文件上传(结合上一篇(h5 图片回显))

    前端用了jquery.form.js插件异步提交表单 $("#imgForm").ajaxSubmit();//户主头像 /** * * @description 上传户主头像 * ...

  6. BZOJ3376: [Usaco2004 Open]Cube Stacking 方块游戏

    [传送门:BZOJ3376] 简要题意: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000 ...

  7. Sub Thread to update main Thread (UI) 2

    Sub Thread to update main Thread (UI)  2 Handler.post(somethread); Handler.sendMessage("Msg&quo ...

  8. CentOS 源设置

    安装完CentOS后,系统默认的源可能有限满,这时我们需要添加国内比较好的源. 一.国内比较好的源https://opsx.alibaba.com/mirror                  #阿 ...

  9. 解决电信或网通的DNS劫持

    大家有没有碰到访问一些不存在域名或者网站时,浏览器本应显示一个网址不存在之类的信息,但是因为现在很多ISP做了DNS劫持将不存在的域名或网址重定向到ISP的广告页面,烦人的狠.其实tomato可以解决 ...

  10. JSP中 input type 用法

    JSP中 input type 用法 Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=te ...