Bind HGE to Lua

Source: Internet
Author: User

 

I have been resting since GF came back in May middle August. Angelscript is followed at most. By the way, bind the HGE 2D engine to the script language. You can see the running results without compiling.

Recently, I started to contact Lua again. I checked the programming language on http://www.tiobe.com/tpci.htmand found that Lua is rising fast. I have been familiar with Python a little before, and it seems very huge. It basically belongs to an independent language, rather than integrating it into the game.

After connecting to Lua, we intend to bind HGE to it. (I seem to be very keen on this recently) bind C ++ to Lua. the convenient and quick way is to use a third-party library. Here I am using tolua ++, it's a cool thing.

About tolua ++:

Tolua ++ manual translation

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 is a tool that greatly simplifies the integration of C/C ++ to Lua. Relying on a pure header file (or even a header file ),
Tolua can automatically generate binding code so that Lua can access some of C/C ++. Use Lua APIs and
Tag Method facilities, tolua puts constants, global variables, functions, classes, and class methods in C/C ++
Ing to Lua.

How tolua works
How does tolua work?

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
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.

To use tolua, create a package file, which is a pure C/C ++ header file (that is, remove unnecessary
Header file of information. The redundant information is the information that you do not want to register in Lua.
We want to register various constants, variables, functions, classes, and class methods in Lua. Then tolua analyzes the file
Create a C/C ++ code file. The code file is automatically bound to C/C ++ to Lua. When we put this code file
In our project, Lua can access the previously specified C/C ++ elements.

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

A package file can also contain normal header files (header files for C/C ++) and other package files,
Or a Lua file.

Some examples are listed below.

Basic Concepts
Basic Concepts

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

The first step to use tolua is to create a package file. We can start with the actual header file corresponding to the package,
You can create a package by removing information that we do not want Lua to know and following the file format identified by tolua.
File. The file format recognized by tolua is described as follows:

Including files
Include files

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

A package file can contain another package file. The approximate format is:
$ Pfile "include_file"

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

A package file can also contain the Normal C/C ++ header file, which requires the hfile or cfile flag:

$ Cfile "example. H"

The following is not clear about tolua_beign.

Finally, Lua files can be encoded on a package file, using $ lfile:

$ Lfile "example. Lua"

The package file can also contain the Lua file, using the @ lfile flag:
@ 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.

The new data types defined by typedef can also be mapped to Lua, but will be processed as user-defined data types.

Similar
$ # Include "header1.h" // include first Header
To contain real header files.

 

Basically, you need to write a PKG file to meet the requirements for the large-scale conversion, and then use the tolua00000000.exe program to process the file to generate the binding code. Finally, add the bound code to your project for compilation. Now you can use the bound content in Lua.

This bound library is very powerful, and even the C ++ template is supported. It is multi-state and inherits many characters.

After learning about the general steps, it is basically no technical difficulty to bind HGE to Lua. I basically finished it with a playful attitude. Currently, only HGE, hgesprite, hgefont, hgeresourcemanager, and hgerect classes are bound. One or two interfaces have problems.

If you want to write a subprogram or something later, you can send me an email or code.

Update:

Example: http://blog.blogwhy.com/azjex/upload/luahge_sample.rar this example uses Lua to override HGE's tutorial 7.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.