Wednesday, 2 July 2014

Building MOAI for Android and OUYA - all in one.

Building MOAI for Android.

Some things you’ll need before you start.

Patience and Attention to Detail - There are a lot of steps, thankfully once you’ve completed the earlier steps, you won’t need to repeat them. The steps have to be carried out accurately and while you can repeat them, they delete and recreate directories in some cases, so be sure you’re following the correct steps..

An existing runnable MOAI project. There are example projects to use, but as you’re at the point of building for Android, it is probably safe to assume that you have a project ready to at least test on Android.

It’s quite simple once you’ve got the hang of it.

Installing the Environment

Install CYGWIN (Install everything, MOAI uses a bunch of modules, be safe and get them all)
Install adt (Eclipse IDE with built in Android SDK and plugins)
(install to c:\adt-bundle-windows-x86_64)
Install android NDK (copy into subdirectory of adt-bundle-windows-x86_64)
Install Apache ANT (install to c:\apache-ant )

Add ant directory to your path
Add ndk directory to your path

You can use the [Start]->[Control Panel]->[System]->[Admin System Settings]->[Advanced]->[Environment Variables] and edit the value of the path variable.
(Make a copy of the previous path first before you edit as it’s hard to repair if you break it)

or you can use the command line

It’s important to get these 2 directories added to your path otherwise all sorts of things will go wrong as the build process won’t be able to find things it depends on.

Start cygwin

The Cygwin terminal places you in your user’s home directory on your the virtual linux.
To get access to your filesystem you need to:

cd /cygdrive/c

This places you at the root of your hard drive (C:\ in DOS speak)

Optional: If you already have a MOAI source codebase you want to work from,
you can use that instead.
Get the most stable MOAI from the git repo into c:\moai-dev

git clone git://

Once the source code for MOAI has been retrieved to moai-dev

cd /cygdrive/c/moai-dev/ant   
(remember in cygwin / takes you to the root of cygwin - not the root of C: )

Run the script to compile MOAI and generate the project structure.
(this will trash any previous projects - be warned)
Pay attention to any errors, this part will fail if anything is wrong with your environment settings.

./ -p com.yourdomain.moai  

(make sure yourdomain does not contain [-] characters, it breaks java when compiled as it can’t be used in a namespace..

Changing Settings files.

modify file.

Set the android_sdk_root to point to your android SDK.
modify the src_dirs to point to your game code.

(note that the path is to be visible to cygwin e.g. “cygdrive/c/gamedev/awesomegame/” )

Modify file

change the requires=( … ) line to read just

requires=( “miscellaneous” )

Save the changes to the files.

Run the script to build the project file.


If this works properly, you’ll have the files you need to start a project.

Bear in mind that cygwin may not have created the directories with the correct permissions. So to save yourself a lot of heartache, set the permissions on the untitled-host folder to give yourself permissions to add to it modify etc. You can also rename it to something more suitable.

Once you’ve completed this section without any issues, you shouldn’t have to repeat this unless you’re re-compiling the MOAI source.

Compiling the Android Project

Start ADT and click the file menu, choose Import.
Then navigate to the directory c:\moai-dev\ant\untitled-host/Host/build/project
If you moved your host to somewhere else, then use that directory.

At this point, your game files should be in the Host/build/project/assets folder.

Once you have the project imported into ADT, you should be able to run it as an Android application.

Note: I’ve noticed a permissions issue when I try to build  the APK, if you get messages saying that you can’t add to the apk, then you need to set the permissions on the folders again as the permissions didn’t inherit when the new build folders were created.

Building Moai for OUYA

To build for the OUYA console, you follow the exact procedure for a standard Android build but with source code (for now until MOAI includes OUYA code in it’s standard host) .

Bear in mind that this is an unofficial Fork of the MOAI engine and may not have newer fixes or features.
The git repo is at:


Once you’ve grabbed the repo either with git or by downloading the ZIP and saving it to c:\moai-dev
(or anything you need, personally mine is called c:\moai-dev-ouya so I can have both Android and OUYA builds )

You can build the project using the steps above. The only change being that instead setting
requires=( “miscellaneous” )
instead you need
requires=( “miscellaneous” “ouya” )

OUYA Specific development

Deploying to an OUYA

Take a micro-USB cable and connect the OUYA to your PC.
Windows will not be able to install the driver initially - so you’ll have to follow instructions on the OUYA web site on how to connect it.

You’ll need to make sure your Android SDK has the Google USB driver installed.
In the directory ( atdbundle/sdk/extras/google/usb_driver/android_winusb.inf), make sure you add

;OUYA Console  
%SingleAdbInterface% = USB_Install, USB\VID_2836&PID_0010  
%CompositeAdbInterface% = USB_Install, USB\VID_2836&PID_0010&MI_01

to the [Google.NTx86] and [Google.NTamd64] sections and save it.

This video explains the steps

Handling Controllers.

Here’s a handy table of controls and how they’re mapped along with a brief description.
The icons are from the OUYA Dev kit.

There are listeners which have to be defined to see the OUYA events.

MOAIOuyaAndroid.setListener ( MOAIOuyaAndroid.OUYA_MOTION_EVENT, onGenericMotionEvent )
MOAIOuyaAndroid.setListener ( MOAIOuyaAndroid.OUYA_MOTION_EVENT_TOUCHPAD, onGenericMotionEventTouchpad )
MOAIOuyaAndroid.setListener ( MOAIOuyaAndroid.OUYA_BUTTON_DOWN, onOuyaButtonDown )
MOAIOuyaAndroid.setListener ( MOAIOuyaAndroid.OUYA_BUTTON_UP, onOuyaButtonUp )

The event handlers are defined as:

function onGenericMotionEventTouchpad(touchpadX, touchpadY, player)

function onGenericMotionEvent(leftAxisX, leftAxisY, rightAxisX, rightAxisY, l2Axis, r2Axis , player)

function onOuyaButtonDown (keyCode, player)

function onOuyaButtonUp (keyCode, player)

You’ll note that the player number is passed as the last parameter. This is 1 to 4.
Also, there is no way to directly query the state of the sticks or buttons, instead you get the events when something happens.

Also note that the Java code handling the sticks takes the deadzone into account. So you need only check for values in the -1 to +1 range in either axis.

Typical Use
Maps to MOAI
The OK/Select button
Button code 96
The Secondary OK button
Button code 99
The Tertiary OK button
Button code 14
A - The Cancel / Back button
Button code 97
Used for accurate directional control.

Button code 21

Button code 22

Button code 19

Button code 20
Left Shoulder Top
Button code 102
Left Shoulder Bottom
General motion l2axis
Left Analog Pressed.
button code 107
Left Analog Stick
General motion leftAxisX leftAcisY
Right Shoulder Top
Button code 103
Right Shoulder Bottom
General motion r2axis
Right Analog Pressed
Button code 107
Right Analog Stick
general motion rightAxisX rightAxisY
The Touch Pad. Acts as a touch screen.
motion event touchpad touchpadX touchpadY
The System button - Used for pause menus, be aware that pressing this button for more than a couple of seconds quits the game
Button code 82

Here’s a handy list of constants:

AXIS_L2 = 17
AXIS_R2 = 18
AXIS_RS_X = 11
AXIS_RS_Y = 14
BUTTON_L1 = 102
BUTTON_L2 = 104
BUTTON_L3 = 106
BUTTON_R1 = 103
BUTTON_R2 = 105
BUTTON_R3 = 107
BUTTON_Y = 100

In App Purchases

The business model for OUYA has always been Free to Play. This doesn’t mean Free games however, it is perfectly fine to offer a Free to play Demo of a game which requires payment in order to continue or even a Free-To-Play game which offers in app purchases for in-game-items.

Purchases and card details are handled through OUYA’s market place. All the game has to do is talk to it.

Checking purchases

When the game runs, after a purchase has been made - it’s important to check to see if the game has been purchased or not. Part of the ODK deals with this.

Sources and Thanks

Thanks to gamesfromscratch for getting me through the build process so I could create this document.

Thanks to Krusmir for putting the OUYA MOAI branch together

No comments:

Post a Comment