Games | Tutorial | Stratosphere | Centura

Ouya Game Programming

Chapter 1 - Project Setup

New Project

File -> New -> Android Application Project

Name Project

Application Name: Here is where you name your game "Demo Game"
Project Name: is your game name without spaces "DemoGame"
Package Name: the first part should be your website backwords unfoil.com = com.unfoil followed by your project name "com.unfoil.demogame"
Minimum SDK: for ouya this would be "Android 4.1" however since I am also targeting other android devices I set this to "Android 2.2"
Target SDK: set this to "Android 4.1"
Compile With: set this to "Android 4.1"

Click Next

Configure Project

Uncheck - create custom launcher icon (this will just skip a screen in the project creation process we can change the icon later)
Uncehck - create activity (while we will have an activity in our project creating it here will create extra stuff that we will just end up deleting)

Click Finish

Creating a Thread

Under Project explorer on the left side select your project and expand it. Then you will right click src New -> Class

Package: You will input "com.unfoil.demogame"
Name: You will name it "GameThread"

GameThread.java

package com.unfoil.demogame;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;


public class GameThread extends Thread {
	
	private Paint paint = new Paint();
	private boolean running;
	private SurfaceHolder surfaceHolder;
	
	public GameThread(SurfaceHolder surfaceHolder) {
		super();
		this.surfaceHolder = surfaceHolder;
		running = false;
	}
			 
	public void setRunning(boolean running) {
		this.running = running;
	}
	
	public void run() {
		Canvas canvas;
		while(running) {
			canvas = null;
			try {
				canvas = this.surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) {
					paint.setColor(0xFFCCCCCC);
					paint.setTextSize(50);
					canvas.drawText("Hello, World!", 350, 220, paint);
				}
			}
			finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}  
		}
	}

}

Creating a Panel

Under Project explorer on the left side select your project and expand it. Then you will right click src New -> Class

Package: You will input "com.unfoil.demogame"
Name: You will name it "GamePanel"

GamePanel.java

package com.unfoil.demogame;

import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
	
	public GameThread thread;
	
	public GamePanel(Context context) {
		super(context);
		getHolder().addCallback(this);
		thread = new GameThread(getHolder());
		setFocusable(true);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}
	 
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}
	 
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		 boolean retry = true;
		 while (retry) {
			 try {
				 thread.join();
				 retry = false;
			 }
			 catch (InterruptedException e) {
			 }
		 }
	}
	
}

Creating an Activity

Under Project explorer on the left side select your project and expand it. Then you will right click src New -> Class

Package: You will input "com.unfoil.demogame"
Name: You will name it "GameActivity"

GameActivity.java

package com.unfoil.demogame;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;


public class GameActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(new GamePanel(this));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
		 
	@Override
	protected void onStop() {
		super.onStop();
	}

}

Manifest

In the manifest we add an activity under application

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unfoil.demogame"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.unfoil.demogame.GameActivity"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="tv.ouya.intent.category.GAME"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

ouya_icon.png

The ouya icon is a 732x412 png file. This is placed under res/drawable-xhdpi

ouya-sdk.jar

In the libs directory under Project Exploror to the left delete the android-support jar and copy in the ouya-sdk.jar

Conclusion

With the Ouya console plugged into your computer with the usb cable do the following

Run -> Run Configurations
Project: Click Browes and then Select DemoGame
Click - Run

You will see a device in the top list called Ouya with some other characters after it. Double click this line.

At this point you should have Hello, World! showing on your Ouya screen. To exit hold down / double tap the ouya button or press A on the controller.