现在的位置: 首页 > 综合 > 正文

绑定HGE到Lua中

2013年09月12日 ⁄ 综合 ⁄ 共 2937字 ⁄ 字号 评论关闭

 

    从8月中旬GF回来后我就一直在休息,期间基本没做什么事。最多也就关注了下AngelScript。顺便把HGE这个2D引擎绑定到这个脚本语言里。不需要编译就可以看运行结果这种感觉太不错了。

    最近又开始接触Lua,我考察了http://www.tiobe.com/tpci.htm上的编程语言排行,发现Lua上升的挺快。以前稍微接触过Python,感觉而言就是很庞大,基本属于一门独立的语言,而不是拿给我们集成到游戏中去的。

    接触了Lua,又打算把HGE绑定进去。(最近我似乎很热衷这种事)绑定C++的东西到Lua中,方便快捷的方法是使用第三方库,这里我使用的是tolua++,很酷的一个东西。

   关于tolua++:

tolua++ manual 翻译

Kevin Lynx

2007/9/3

tolua is a tool that greatly simplifies the integration of C/C++ code with Lua.
Based on a cleaned header file (or extracts from real header files), tolua
automatically generates the binding code to access C/C++ features from Lua.
Using Lua API and tag method facilities, tolua maps C/C++ constants, external
variables, functions, classes, and methods to Lua.

tolua是一种大大简化集成C/C++到LUA中的工具。依靠一种纯头文件(甚至直接是头文件),
tolua可以自动地产生绑定代码,使LUA可以访问到C/C++的一些东西。使用lua api以及
tag method facilities,tolua把C/C++中的常量,全局变量,函数,类,以及类方法
映射进LUA中。

How tolua works
tolua如何工作?

To use tolua, we create a package file, a C/C++ cleaned header file, listing
the constants, variables, functions, classes, and methods we want to export to
the Lua environment. Then tolua parses this file and creates a C/C++ file that
automatically binds the C/C++ code to Lua. If we link the created file with our
application, the specified C/C++ code can be accessed from Lua.

要使用tolua,我们创建一个package文件,这是一种纯C/C++头文件(即一种去除了多余
信息的头文件,多余信息就是那些不想注册进LUA的信息,个人理解),该文件罗列了
我们想注册进LUA中的各种常量,变量,函数,类,以及类方法。然后tolua分析该文件
并创建一个C/C++代码文件,该代码文件会自动绑定C/C++到LUA中。当我们把该代码文件
加进我们的工程中时,LUA就可以访问到之前指定的那些C/C++元素。

A package file can also include regular header files, other package files, or
lua files.

一个package文件也可以包含正常的头文件(即用于C/C++的头文件),其他package文件,
或者lua文件。

以下罗列了部分例子,略。

Basic Concepts
基本概念

The first step in using tolua is to create the package file. Starting with
the real header files, we clean them by declaring the features we want to
access from Lua in a format that tolua can understand. The format tolua
understands is simple C/C++ declarations as described below.

使用tolua的第一步是创建package文件。我们可以从package对应的真正的头文件开始,
去掉一些我们并不想让LUA知道的信息,以及遵循tolua识别的文件格式,即可创建package
文件。tolua识别的文件格式如下所描述:

Including files
包含文件

A package file may include other package file. The general format to do that is:
$pfile "include_file"

一个package文件可以包含另一个package文件。大致的格式为:
$pfile "include_file"

A package file may also include regular C/C++ header files, using the hfile or
cfile directive:

一个package文件也可以包含正常的C/C++头文件,这需要使用hfile或者cfile标志:

$cfile "example.h"

以下关于tolua_beign之类的没看明白。

Finally, lua files can be included on a package file, using $lfile:

$lfile "example.lua"

package文件也可以包含lua文件,使用 @lfile 标志:
@lfile "example.lua"

Typedefs
tolua also accepts simple typedef's inside the package files. Any occurrence
of a type after its definition is mapped by tolua to the base type.

typedef定义的新数据类型也可以映射到LUA中,不过将被当作用户自定义类型处理。

可以使用类似的
    $#include "header1.h"                 // include first header
来包含真实的头文件。

 

    基本上,大致转换步骤就是根据绑定需求写pkg文件,然后用tolua++.exe程序处理该文件,从而生成绑定代码。最后将绑定代码添加到自己的工程中一起编译即可。这个时候即可在lua中使用绑定的内容了。

    这个绑定库很强大,连C++模板都支持,多态,多继承不在话下。

    了解了大致步骤后,绑定HGE到lua中基本没技术难度。我基本上是抱着玩一玩的态度做完这件事的。目前只绑定了HGE,hgeSprite,hgeFont,hgeResourceManager,hgeRect等类。有一两个接口有问题。

    迟些时候写个例子程序什么的放出来,当然有兴趣的可以发邮件给我,我给代码。

 update :

    例子:http://blog.blogwhy.com/azjex/upload/luahge_sample.rar 该例子用LUA重写了HGE的教程7。

 

抱歉!评论已关闭.