分享

VC知识库文章

 herowuking 2015-04-03

      介绍

本文介绍将Lua嵌入到自己程序中的方法。

什么是Lua

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

Lua例子(FOR 循环)

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

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

背景

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

定义的Lua函数如下:

01.-- # MessageBox
02.--------------------------------------------------------
03.-- int MessageBox(
04.-- string msg, |= Message to display
05.-- string capition |= Capition of Box
06.-- );
07.-- Return Value:
08.-- if 1 the user click in OK or user close the box
09. 
10. 
11.-- # ShowContentPainel
12.-----------------------------------------------------------------
13.-- void ShowContentPainel(
14.-- bool bShow |= If true, the painel start opened, if false not.
15.-- );
16. 
17.-- # SetWindowStartSize
18.-----------------------------------------------------------------
19.-- void SetWindowStartSize(
20.-- number w, |= Start W size of window
21.-- number h, |= Start H size of window
22.-- );
23.-- Remarks:
24.-- if this function is not called, the default size is 800 x 600
25. 
26.-- # SetMinWindowSize
27.-----------------------------------------------------------------
28.-- void SetMinWindowSize(
29.-- number w, |= Minimum W size of window
30.-- number h, |= Minimum H size of window
31.-- );
32. 
33.-- # SetTitle
34.-----------------------------------------------------------------
35.-- void SetTitle(
36.-- string title |= Text that be title of window.
37.-- );
38. 
39.-- # Navigate
40.-----------------------------------------------------------------
41.-- void Navigate(
42.-- string url |= Url
43.-- );
44. 
45.-- # InsertItemInPainel
46.-----------------------------------------------------------------
47.-- void InsertItemInPainel(
48.-- string title, |= Text displayed in tree
49.-- string url, |= Url
50.-- number icon, |= Icon of item, the possible values
51.---are: 0 = BOOK, 1 = FILE, 2 = NETFILE
52.-- number id, |= Id of item, this has to be unique and start in 1
53.-- number idp |= Parent item, this is a ID of a item that is
54.---the parent or '0' for root item.
55.-- );
56.-- sample:
57.-- ICON BOOK / ID 1 / In ROOT
58.-- InsertItemInPainel("Trinity Systems",
60.-- ICON NETFILE / ID 2 / In ID1 (Trinity Systems)
61.-- InsertItemInPainel("Orion",
63. 
64.-- # ExpandItemInPainel
65.------------------------------------------------------------------
66.-- void ExpandItemInPainel(
67.-- string id |= Id of item
68.-- );
69.-- Remarks:
70.-- This function need to be called after InsertItemInPainel's

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

代码

1. 首先要做的是创建含Lua的DLL

2. 在工程里做如下链接:

01.//
02.// For sample:
03.//
04.//---------------------------------------------
05.// Library Linkage
06.//---------------------------------------------
07.//-
08.#if defined (_DEBUG)
09.#pragma comment( lib, "lua.lib" ) // Lua Support
10.#else
11.#pragma comment( lib, "lua.lib" ) // Lua Support
12.#endif
13.//-

记得:要更改项目的属性Project Property -> linker -> general -> additional library directory

到lua lib所在目录。

3. 添加Lua包含文件:

1.extern "C"
2.{
3.#include "lua.h"
4.}

记得:要更改项目的属性 Project Property -> C/C++ -> general -> additional include directories

到lua include所在目录。

注意:lua lib里的所有文件保持"c"的扩展名,因为Lua采用ANSI C编写。

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

01.LRESULT OnCreate(UINT
02./*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
03./*bHandled*/)
04.{
05.//...
06.// Lua
07.//
08.lua_State *luaVM = lua_open(); /*
09.Open Lua */
10.//
11.luaopen_base(luaVM );     /* opens the
12.basic library */
13.luaopen_table(luaVM );    /* opens the table
14.library */
15.luaopen_io(luaVM );       /* opens
16.the I/O library */
17.luaopen_string(luaVM );   /* opens the string
18.lib. */
19.luaopen_math(luaVM );     /* opens the math lib.
20.*/
21.if (NULL ==
22.luaVM)
23.{
24. 
25.MessageBox("Error Initializing lua\n");
26.}
27.//...
28.// Do things with lua
29.code.
30.// see below
31.//...
32.lua_close(luaVM); /* Close Lua */
33.//
34.//
35.End
36.//...
37.}

5. 现在我们写Lua 和 C/C++ 结合的函数

Lua API函数可以这样写:

1.lua_register(s, n, g)

这里:

s:是lua_State

n:暴露给Lua的函数名称

g: C/C++中的结合函数

请看下面例子:

01.//...
02.// Do things with
03.lua code.
04.lua_register( luaVM, "SetHome", l_SetHome );
05.//...
06.//
07.-------------------------------------------
08.//
09.#Lua Functions
10.//
11.------------------------------------------
12.//
13.static
14.int l_SetTitle( lua_State*
15.luaVM)
16.{
17.const char*
18.title = luaL_checkstring(luaVM,
19.1);
20. 
21.theMainFrame->SetWindowText(title);
22. 
23.return 0;
24.}

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

1.//...
2.// Do things with lua
3.code.
4.lua_register( luaVM, "SetHome", l_SetHome );
5.//more glue
6.functions
7.lua_dofile(luaVM, "hrconf.lua");
8.//...

执行Lua API函数可以这样:

1.lua_dofile(s, p)

这里:

s: 是lua_State

p: 是Lua脚本文件

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

兴趣点:

Lua是免费软件free software

下载Lua:http://www./download.html

手册:http://www./manual/5.1/

参考:http://www./

VC知识库:http://www./     

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多