cocos2d-x 3.0 + C A super-introduction to game development starting with ++ 11 strong> div>
Lua Binding up to cocos2d-x 2.x
Script Binding requires a code (Glue Code) to bridge the two languages.
In the past, a special definition file called * .pkg
was written and automatically generated by using a utility calledtolua ++
. This is convenient.
↑ Such code is automatically generated.
For details, please refer to the following article.
How to use tolua ++ (mainly Mac) --Programming monster
However, it took time to write all the classes and methods in * pkg, and it became difficult when the number was large, and there were bugs caused by it.
The story of merging after fixing a bug in cocos2d-x --5.1 Sara Udon
Lua Binding from cocos2d-x 3.0
Starting with cocos2d-x 3.0, you can use a utility called binding-generator
to automatically search for classes and methods from the header and automatically generate a Glue Code.
In addition, the settings can be described quite flexibly, which simplifies the definition file creation that was conventionally done manually.
cocos2d/bindings-generator
Setup on Mac
I will explain it because it is a little troublesome to set up
See page below. Currently not compatible with Windows.
bindings-generator/README.md at master · cocos2d/bindings-generator
Install cocos2d-x 3.0
Download cocos2d-x 3.0 alpha1 (as of 12/15) from here and unzip it. Place it in $ HOME / cocos2d-x-3.0
.
http://www.cocos2d-x.org/download
Python package installation
Install the required Python packages. If you're using standard Python, use ʻeasy_install`.
sudo easy_install pyyaml, cheetah
or
pip install pyyaml, cheetah
Introduction of llvm-clang-3.3
Download Clang Binaries for Mac OS X
from the page below. It seems that Homebrew will also be included, but it has not been verified.
It was 3.1 in the docs, but it didn't work without 3.3, so please download it.
This time it is installed at $ HOME / llvm + clang-3.3
.
http://llvm.org/releases/download.html#3.3
Introducing Android NDK
From the following page.
http://developer.android.com/tools/sdk/ndk/index.html
This time it is installed in / Applications / Android / android-ndk
.
It seems that Homebrew will also be included.
brew install android-ndk
Setting environment variables
Set various environment settings. Please specify the path you have installed so far.
Since I installed Python with pyenv, the PATH is as follows, but usually / usr / bin / python
is OK.
export PYTHON_BIN=$HOME/.pyenv/shims/python
export COCOS2DX_ROOT=$HOME/cocos2d-x-3.0
export NDK_ROOT=/Applications/Android/android-ndk
export CLANG_ROOT=$HOME/clang+llvm-3.3
Generate Glue Code for cocos2d-x
Let's try to generate the code for Lua Binding of cocos2d-x.
cd $COCOS2DX_ROOT/tools/tolua
./genbindings.sh
If all goes well, 93000 lines and 2.6MB of huge source code should be automatically generated in $ COCOS2DX_ROOT / scripting / auto-generated / lua-bindings
. You did it!
At the same time, a configuration file called ʻuserconf.iniis generated. This is a file that sets environment variables, and the attached
genbindings.sh` will automatically generate it for you.
Try to generate it in your own project
The setup up to this point was troublesome and I was already quite tired, but this time I will generate it from my own project of the main subject.
Write your_game.ini
By describing the Glue Code settings to be generated in your_game.ini
, you can specify the class methods to be generated or ignored as shown below.
Basically, copy cocos2dx.ini
and rewrite it if necessary.
#Specify the PATH of the header to parse
headers = %(cocosdir)s/cocos2dx/include/cocos2d.h %(cocosdir)s/CocosDenshion/include/SimpleAudioEngine.h ../Classes/LuaBindSample.h
#Describe the list of generated classes (regular expressions are possible)
classes = ^GameObject$
#Describe the class and method pair to be ignored (regular expression is possible)
skip = IgnoredClass::[ignoredMethod{0-9} anotherMethod],
LuaBindSample/binding-generator/bind_sample.ini at master · giginet/LuaBindSample
The description method is written in the comment, so I will omit the detailed explanation.
Please refer to cocos2dx.ini for details.
It seems that it runs on a template engine called CHEETAH made by Python, so the following may be helpful for grammar etc.
Cheetah - The Python-Powered Template Engine
Write userconf.ini
By describing the PATH specified in the environment variable earlier in a file called ʻuserconf.ini`, it seems that it will read and execute without permission.
Environment variables cannot be used, so I think it's best to create them according to each environment.
For example, it looks like the following.
[DEFAULT]
androidndkdir=/Applications/Android/android-ndk-r8e
clangllvmdir=/Users/giginet/clang+llvm-3.3
cocosdir=/Users/giginet/cocos2d-x-3.0
cxxgeneratordir=/Users/giginet/cocos2d-x-3.0/tools/bindings-generator
extra_flags=
Run the script
All you have to do is pass the appropriate arguments to generator.py in binding-generator
.
Since there are many arguments, it is convenient to write a wrapper like this
#!/bin/bash
GENERATOR_ROOT=${COCOS2DX_ROOT}/tools/bindings-generator
INI_FILE=bind_sample.ini
SECTION_NAME=your_game
OUTPUT_PATH=../Classes/LuaGlue
OUTPUT_NAME=lua_your_game_auto
LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${GENERATOR_ROOT}/generator.py $INI_FILE -s $SECTION_NAME -t lua -o ${OUTPUT_PATH} -n $OUTPUT_NAME
By doing this, lua_your_game_auto.h/cpp
will be generated under ../Class/LuaGlue
. great!
Sample project
I created a repository for verification on Github, so I will put it there.
LuaBindSample/binding-generator at master · giginet/LuaBindSample
It works when cloned under $ COCOS2DX_ROOT / projects
.
What about JavaScript Binding?
I haven't tried it, but I feel like I can go as well. Please write someone.
Summary
Building the environment was a lot of work, but it's great to be able to easily generate a Glue Code. It is also good to be able to describe flexibly with regular expressions.
I will make effective use of it the next time I make a game.
Lua related articles
This is useful information related to Lua that I wrote earlier.
I tried to automate Lua Syntax check with Jenkins --5.1 Sara Udon
Let's explain the Lua binding of cocos2d-x --5.1 Sara Udon
Promotion
We have released a game called "VOXCHRONICLE", a back-scrolling RPG made by cocos2d-x that plays sounds.
It's completely free to play, so if you find this article useful, please try it. Lua Binding is mostly used in this game.
VOXCHRONICLE --on the App Store on iTunes
The back scroll RPG "VOXCHRONICLE" that plays the sound has been released --5.1 Sara Udon
A story about trying interactive music with an iPhone game --5.1 Sara Udon
giginet/VOXCHRONICLE