1, Q:为什么没有用Luaplus或是其他的Binder?
A: nice question! Luaplus不能满足我在linux下使用,其他的太庞大,我需要一个轻量级的。
2,Q:是原创吗?
A:NO, thanks to Matthew Harmon
matt@matthewharmon.com,我按照自己的需求做了封装。
3,Q:它的优点是什么?
A:主要是它简单,基本能满足和C/C++交互的需求。它使用了lua_newthread来管理每一个脚本(性能有问题吗?)。它支持
事件的resume,另外把LuaDebugger也封装进来了,方便调试。
贴下接口头文件:
#ifndef ILUAENGINE_H
#define ILUAENGINE_H
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define GTC_LP 0x0000 // 指针
#define GTC_INT 0x0001 // 整型
#define GTC_DOUBLE 0x0002 // 浮点型
#define GTC_STRING 0x0003 // 字符串
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// 脚本数据类型定义
enum
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
SD_NUMBER = 0, // 数字类型
SD_STRING, // 字符串类型
SD_TABLE, //表
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct SSDTable
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int nNum;
void* pValue;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// 脚本参数对象
struct SScriptParamObj
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int nType; // 参数类型, SD_NUMBER 或者 SD_STRING
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
union UScriptParam // 参数值
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int nNumber; // 数字
char szString[64]; // 字符串
SSDTable stTable;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
} unValue;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
SScriptParamObj()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
memset(this, 0, sizeof(*this));
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void operator = (int nValue)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nType = SD_NUMBER;
unValue.nNumber = nValue;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void operator = (char *str)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nType = SD_STRING;
unValue.szString[0] = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if (str != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strncpy(unValue.szString, str, sizeof(unValue.szString));
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void operator = ( SSDTable pT )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nType = SD_TABLE;
unValue.stTable.nNum = pT.nNum;
unValue.stTable.pValue = (void *)pT.pValue;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct ILuaScript
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: szFileName - 脚本文件名
* @Return: NULL
* @Description: 调用一个脚本
*/
virtual bool CallFile(const char *szFileName) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: szFuncName - 函数名 pIn, nInNum - 输入参数列表指针以及个数
* @Return: pRet, nRetNum - 返回参数列表指针以及个数
* @Description: 调用一个函数
*/
virtual bool CallFunction(const char *szFuncName, SScriptParamObj *pIn,
int nInNum, SScriptParamObj *pRet, int nRetNum) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: szString - 字符串指针
* @Return: NULL
* @Description: 调用一个字符串
*/
virtual bool CallString(const char *szString) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual void ShowLuaCallStack() = 0;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct ILuaManager
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual bool InitManager(IGameWorld* pGameWorld) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual ILuaScript* CreateScript() = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual void Update(float fElapse) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: 注意如果是自己卸载一个script,请自己
手工管理这个script的指针,manager析构
的时候,会自动析构所有的script
*/
virtual void UnlinkScript(ILuaScript* pScript) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual ILuaScript* GetScript(const char* szScriptName) = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @Param: NULL
* @Return: NULL
* @Description: NULL
*/
virtual void Release() = 0;
};
#endif
另外,还有一个高手写的binder,针对c++的object做了很好的封装,但我没有采用,因为我想要灵活一点的。
链接在下面,大家可以对比一下,给出评论
http://www.codeproject.com/KB/cpp/luaincpp.aspx本文的下载文件