在cocos2d_lua_bindings项目的external目录下建立lpack目录
1. lpack目录中增加lpack.c
从https://github.com/LuaDist/lpack获取
2. lpack目录增加lpack.h
#ifndef LPACK_H
#define LPACK_H
//
// lpack.h
// cocos2d_lua_bindings
//
// Created by sherlock on 2017/3/6.
//
//
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif
TOLUA_API int luaopen_pack(lua_State *L);
#endif /* LPACK_H */
3. 在manual/network/lua_extensions.c中增加如下内容
//增加引用文件
#include "external/lua/lpack/lpack.h"
static luaL_Reg luax_exts[] = {
{"socket.core",luaopen_socket_core},{"mime.core",luaopen_mime_core},{"pack",luaopen_pack},//添加包到脚本系统
{NULL,NULL}
};
4. 测试
local pack = require"pack"
--
local function hex(s)
local s=string.gsub(s,"(.)",function (x) return string.format("%02X",string.byte(x)) end)
return s
end
--
local bpack = string.pack
local a = bpack("Ab8","\027Lua",5*16+1,0,1,4,8,0)
print(hex(a),string.len(a))
结论:基本思路是:c++通过tolua绑定到lua中使用
原文链接:https://www.f2er.com/cocos2dx/338844.html