Lua嵌入到自己的程序中(转)

摘要:
LuaLua是什么?它是一种具有简单数据描述的扩展编程语言(动态解析语言)。它提供了非常好的面向对象编程、函数式编程和数据驱动编程。它可以用作任何需要它的程序的强大、轻量级脚本语言。Lua是以cleanC编写的库的形式提供的。(所谓的CleanC是指ANSI C和C++通用的子集。)Lua示例(FOR

什么是Lua
Lua是具有简单数据描述的扩展编程语言(动态解析语言)。它提供了非常好的面向对象编程, 函数式编程(functional programming),数据驱动式编程(data-driven programming),它可以作为一个强大、轻量的脚本语言,供任何需要的程序使用。 Lua 以一个用 clean C 写成的库形式提供。(所谓 Clean C ,指的 ANSI C 和 C++ 中共通的一个子集)

Lua例子(FOR 循环)

for i=1,10 do -- the first program in every language io.write("Hello world, from ",_VERSION,"!\n") end

详细描述可以参考Lua网站http://www.lua.org/about.html

背景
本例子是WTL程序(简单的HTML帮助系统),结合Lua脚本作为参数和内容的定制。
定义的Lua函数如下:

-- # MessageBox -------------------------------------------------------- -- int MessageBox( -- string msg, |= Message to display -- string capition |= Capition of Box -- ); -- Return Value: -- if 1 the user click in OK or user close the box -- # ShowContentPainel ----------------------------------------------------------------- -- void ShowContentPainel( -- bool bShow |= If true, the painel start opened, if false not. -- ); -- # SetWindowStartSize ----------------------------------------------------------------- -- void SetWindowStartSize( -- number w, |= Start W size of window -- number h, |= Start H size of window -- ); -- Remarks: -- if this function is not called, the default size is 800 x 600 -- # SetMinWindowSize ----------------------------------------------------------------- -- void SetMinWindowSize( -- number w, |= Minimum W size of window -- number h, |= Minimum H size of window -- ); -- # SetTitle ----------------------------------------------------------------- -- void SetTitle( -- string title |= Text that be title of window. -- ); -- # Navigate ----------------------------------------------------------------- -- void Navigate( -- string url |= Url -- ); -- # InsertItemInPainel ----------------------------------------------------------------- -- void InsertItemInPainel( -- string title, |= Text displayed in tree -- string url, |= Url -- number icon, |= Icon of item, the possible values ---are: 0 = BOOK, 1 = FILE, 2 = NETFILE -- number id, |= Id of item, this has to be unique and start in 1 -- number idp |= Parent item, this is a ID of a item that is ---the parent or '0' for root item. -- ); -- sample: -- ICON BOOK / ID 1 / In ROOT -- InsertItemInPainel("Trinity Systems", "http://www.novaamerica.net/trinitysystems/", 0, 1, 0); -- ICON NETFILE / ID 2 / In ID1 (Trinity Systems) -- InsertItemInPainel("Orion", "http://www.novaamerica.net/trinitysystems/Orion", 2, 2, 1); -- # ExpandItemInPainel ------------------------------------------------------------------ -- void ExpandItemInPainel( -- string id |= Id of item -- ); -- Remarks: -- This function need to be called after InsertItemInPainel's

现在我将展示如何使用Lua/C++创建这些函数。

代码

1. 首先要做的是创建含Lua的DLL
2. 在工程里做如下链接:

// // For sample: // //---------------------------------------------                                                                                                                                                                                     

记得:要更改项目的属性Project Property -> linker -> general -> additional library directory
到lua lib所在目录。

3. 添加Lua包含文件:

extern "C" { #include "lua.h" }

记得:要更改项目的属性 Project Property -> C/C++ -> general -> additional include directories
到lua include所在目录。
注意:lua lib里的所有文件保持"c"的扩展名,因为Lua采用ANSI C编写。

4. 现在我们需要按如下方式启动Lua VM

LRESULT OnCreate(UINT , WPARAM , LPARAM , BOOL& ) { //... // Lua // lua_State *luaVM = lua_open(); // luaopen_base(luaVM ); luaopen_table(luaVM ); luaopen_io(luaVM ); luaopen_string(luaVM ); luaopen_math(luaVM ); if (NULL == luaVM) { MessageBox("Error Initializing lua\n"); } //... // Do things with lua code. // see below //... lua_close(luaVM); // // End //... }

5. 现在我们写Lua 和 C/C++ 结合的函数
Lua API函数可以这样写:

lua_register(s, n, g)

这里:
s:是lua_State
n:暴露给Lua的函数名称
g: C/C++中的结合函数

请看下面例子:

//... // Do things with lua code. lua_register( luaVM, "SetHome", l_SetHome ); //... // ------------------------------------------- // #Lua Functions // ------------------------------------------ // static int l_SetTitle( lua_State* luaVM) { const char* title = luaL_checkstring(luaVM, 1); theMainFrame->SetWindowText(title); return 0; }

6. 现在我们需要加载并执行Lua脚本

//... // Do things with lua code. lua_register( luaVM, "SetHome", l_SetHome ); //more glue functions lua_dofile(luaVM, "hrconf.lua"); //...

执行Lua API函数可以这样:

lua_dofile(s, p)

这里:
s: 是lua_State
p: 是Lua脚本文件

为了完全理解Lua API,可以参考Lua 参考手册http://www.lua.org/manual/5.1/manual.html

趣点:
Lua是免费软件free software
下载Lua:http://www.lua.org/download.html

免责声明:文章转载自《Lua嵌入到自己的程序中(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Jenkins Docker安装及Docker build step插件部署配置Cache Server缓存服务器下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python 连接redis密码中特殊字符问题

连接方法: self.pool = redis.ConnectionPool.from_url(self.redis_url)opredis = redis.Redis(connection_pool=self.pool)redis_url = 'redis://:cot$#D4^&1234@172.31.26.174:6379/0' 直接连red...

用calibre和python制作电子书---Python和Git教程

calibre - E-book management是一个很强大的电子书管理软件,可以打开和转换各种格式的电子书,可以抓取新闻到本地阅读,允许用户自定义新闻源,可以通过编写自己recipe让其将网页上的内容抓取下来并且生成电子书,利用这个功能我将廖雪峰老师的Python教程和Git教程做成了epub电子书,使用firefox的epubReader插件就可...

lua if 流程控制

Lua认为false和nil为假,true和非nil为真。 要注意的是Lua中 0 为 true --[ 0 为 true ] if(0) then print("0 为 true") end 输出 0 为 true 实例 --[ 定义变量 --] a = 10--[ 使用 if 语句 --] if( a < 20 ) then...

CentOS terminal 安装 matlab(mode=silent)

1. 下载matlab for Unix 2014 ,需要crack文件 2. 挂载iso文件, mount -o loop,ro Mathworks.Matlab.R2014a.iso /media 3. 将crack中的install.jar 覆盖/media/java/jar下面的install.jar,这步骤必须正确完成 sudo cp Crack...

Python安装sqlite3

今天使用PYthon时,发现错误 ImportError: No module named sqlite 这是因为缺少 SQLITE3的缘故。 下面分享一下解决此问题的方法步骤: 1. 查看是Python是否有该库 使用http://django-china.cn/topic/413/ 查看自己Python库的路径, >>> impor...

Cocos2d-x Lua 阅读Csv文件,使用数据更方便

在我的书或出售之前,我的源代码,有Csvshadow文件。 也许这是偏见。我与工作将是最长的轮廓Csv,所以,我会帮助不大喜欢它的游戏。 Csv文件,非常格式easy,也就是说,一个数据线,字段之间用逗号分隔,策划也能够方便地使用Excel进行编辑。 Csv格式的文件。解析起来也非常easy,所以自己动手写写非常快~(小若:我就喜欢拿来主义,你怎么着)...