#include "tolua_fix.h"
#include <stdlib.h>
static int s_function_ref_id = 0;
TOLUA_API void toluafix_open(lua_State* L)
{
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
}
TOLUA_API int toluafix_pushusertype_ccobject(lua_State* L,
int refid,
int* p_refid,
void* ptr,
const char* type)
{
if (ptr == NULL || p_refid == NULL)
{
lua_pushnil(L);
return -1;
}
if (*p_refid == 0)
{
*p_refid = refid;
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_pushlightuserdata(L, ptr); /* stack: refid_ptr refid ptr */
lua_rawset(L, -3); /* refid_ptr[refid] = ptr, stack: refid_ptr */
lua_pop(L, 1); /* stack: - */
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_pushstring(L, type); /* stack: refid_type refid type */
lua_rawset(L, -3); /* refid_type[refid] = type, stack: refid_type */
lua_pop(L, 1); /* stack: - */
//printf("[LUA] push CCObject OK - refid: %d, ptr: %x, type: %s\n", *p_refid, (int)ptr, type);
}
tolua_pushusertype(L, ptr, type);
return 0;
}
TOLUA_API int toluafix_remove_ccobject_by_refid(lua_State* L, int refid)
{
void* ptr = NULL;
const char* type = NULL;
void** ud = NULL;
if (refid == 0) return -1;
// get ptr from tolua_refid_ptr_mapping
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_rawget(L, -2); /* stack: refid_ptr ptr */
ptr = lua_touserdata(L, -1);
lua_pop(L, 1); /* stack: refid_ptr */
if (ptr == NULL)
{
lua_pop(L, 1);
// Lua stack has closed, C++ object not in Lua.
// printf("[LUA ERROR] remove CCObject with NULL ptr, refid: %d\n", refid);
return -2;
}
// remove ptr from tolua_refid_ptr_mapping
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_pushnil(L); /* stack: refid_ptr refid nil */
lua_rawset(L, -3); /* delete refid_ptr[refid], stack: refid_ptr */
lua_pop(L, 1); /* stack: - */
// get type from tolua_refid_type_mapping
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_rawget(L, -2); /* stack: refid_type type */
if (lua_isnil(L, -1))
{
lua_pop(L, 2);
printf("[LUA ERROR] remove CCObject with NULL type, refid: %d, ptr: %x\n", refid, (int)ptr);
return -1;
}
type = lua_tostring(L, -1);
lua_pop(L, 1); /* stack: refid_type */
// remove type from tolua_refid_type_mapping
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_pushnil(L); /* stack: refid_type refid nil */
lua_rawset(L, -3); /* delete refid_type[refid], stack: refid_type */
lua_pop(L, 1); /* stack: - */
// get ubox
luaL_getmetatable(L, type); /* stack: mt */
lua_pushstring(L, "tolua_ubox"); /* stack: mt key */
lua_rawget(L, -2); /* stack: mt ubox */
if (lua_isnil(L, -1))
{
// use global ubox
lua_pop(L, 1); /* stack: mt */
lua_pushstring(L, "tolua_ubox"); /* stack: mt key */
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: mt ubox */
};
lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */
lua_rawget(L,-2); /* stack: mt ubox ud */
if (lua_isnil(L, -1))
{
// Lua object has released (GC), C++ object not in ubox.
//printf("[LUA ERROR] remove CCObject with NULL ubox, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type);
lua_pop(L, 3);
return -3;
}
ud = (void**)lua_touserdata(L, -1);
lua_pop(L, 1); /* stack: mt ubox */
if (ud == NULL)
{
printf("[LUA ERROR] remove CCObject with NULL userdata, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type);
lua_pop(L, 2);
return -1;
}
// clean userdata
*ud = NULL;
lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */
lua_pushnil(L); /* stack: mt ubox ptr nil */
lua_rawset(L, -3); /* ubox[ptr] = nil, stack: mt ubox */
lua_pop(L, 2);
//printf("[LUA] remove CCObject, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type);
return 0;
}
TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def)
{
// function at lo
if (!lua_isfunction(L, lo)) return 0;
s_function_ref_id++;
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: fun ... refid_fun */
lua_pushinteger(L, s_function_ref_id); /* stack: fun ... refid_fun refid */
lua_pushvalue(L, lo); /* stack: fun ... refid_fun refid fun */
lua_rawset(L, -3); /* refid_fun[refid] = fun, stack: fun ... refid_ptr */
lua_pop(L, 1); /* stack: fun ... */
return s_function_ref_id;
// lua_pushvalue(L, lo); /* stack: ... func */
// return luaL_ref(L, LUA_REGISTRYINDEX);
}
TOLUA_API void toluafix_get_function_by_refid(lua_State* L, int refid)
{
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: ... refid_fun */
lua_pushinteger(L, refid); /* stack: ... refid_fun refid */
lua_rawget(L, -2); /* stack: ... refid_fun fun */
lua_remove(L, -2); /* stack: ... fun */
}
TOLUA_API void toluafix_remove_function_by_refid(lua_State* L, int refid)
{
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_rawget(L, LUA_REGISTRY
没有合适的资源?快使用搜索试试~ 我知道了~
Cocos2d-x之C++和Lua通信5个入门Demo
共71个文件
tlog:22个
h:8个
obj:7个
5星 · 超过95%的资源 需积分: 12 424 下载量 90 浏览量
2013-01-01
10:11:23
上传
评论 7
收藏 2.2MB ZIP 举报
温馨提示
木头Cocos2d-x教程 Lua篇 Demo源代码。 教程地址: 第1章:http://blog.csdn.net/musicvs/article/details/8440707 第2章:http://blog.csdn.net/musicvs/article/details/8440919 第3章:http://blog.csdn.net/musicvs/article/details/8445027 第4章:http://blog.csdn.net/musicvs/article/details/8445079 第5章:http://blog.csdn.net/musicvs/article/details/8451361 第6章:http://blog.csdn.net/musicvs/article/details/8451369
资源推荐
资源详情
资源评论
收起资源包目录
LuaStudy.zip (71个子文件)
LuaStudy
Resources
hello.lua 7KB
farm.jpg 113KB
background.mp3 125KB
Default.png 87KB
land.png 24KB
Icon.png 9KB
menu2.png 69KB
helloLua.lua 163B
crop.png 53KB
effect1.wav 10KB
dog.png 26KB
hello2.lua 41B
menu1.png 6KB
proj.win32
res
LuaStudy.ico 47KB
LuaStudy.win32.vcxproj.user 763B
LuaStudy.win32.vcxproj.filters 4KB
LuaStudy.win32.vcxproj 9KB
Debug.win32
vc100.idb 1.6MB
LuaStudy.win32.log 4KB
CL.write.1.tlog 10KB
CL.read.1.tlog 495KB
mt.read.1.tlog 846B
rc.write.1.tlog 1KB
link.5616-cvtres.read.1.tlog 2B
rc.read.1.tlog 6KB
link.5616.read.1.tlog 2B
HelloLua.obj 110KB
AppDelegate.obj 97KB
link.5988-cvtres.write.1.tlog 2B
LuaStudy.win32.exe.embed.manifest 406B
link.5988.read.1.tlog 2B
link.5616.write.1.tlog 2B
mt.command.1.tlog 976B
link.5616-cvtres.write.1.tlog 2B
cl.command.1.tlog 30KB
link-cvtres.read.1.tlog 2B
main.obj 70KB
LuaStudy.win32.lastbuildstate 88B
link.write.1.tlog 4KB
CCLuaEngine.obj 770KB
LuaStudy.res 47KB
LuaStudy.win32.exe.embed.manifest.res 472B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 7KB
rc.command.1.tlog 3KB
link.read.1.tlog 12KB
link.5988.write.1.tlog 2B
LuaStudy.win32_manifest.rc 230B
mt.write.1.tlog 374B
tolua_fix.obj 19KB
LuaStudy.win32.exe.intermediate.manifest 381B
LuaCocos2d.obj 3.78MB
link.5988-cvtres.read.1.tlog 2B
Cocos2dxLuaLoader.obj 135KB
vc100.pdb 748KB
LuaStudy.rc 2KB
resource.h 496B
main.cpp 863B
main.h 270B
Classes
cocos2dx_support
CCLuaEngine.cpp 18KB
tolua_fix.h 1KB
Cocos2dxLuaLoader.cpp 2KB
LuaCocos2d.cpp 1.79MB
Cocos2dxLuaLoader.h 1KB
CCLuaEngine.h 7KB
LuaCocos2d.h 382B
tolua_fix.c 10KB
HelloLua.cpp 3KB
AppDelegate.cpp 2KB
HelloLua.h 687B
AppDelegate.h 960B
共 71 条
- 1
笨木头
- 粉丝: 2260
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页