go for mobile games

81
The Go gopher was designed by Renée French . The gopher stickers was made by Takuya Ueda. Licensed under the Creative Commons 3.0 Attributions license. Go for Mobile Games GopherCon 2016, Denver 11th July 2016 Takuya Ueda @tenntenn KLab Inc. Slide URL: https://goo.gl/cIvTb5

Upload: takuya-ueda

Post on 23-Jan-2018

2.446 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Go for Mobile Games

The Go gopher was designed by Renée French.The gopher stickers was made by Takuya Ueda.Licensed under the Creative Commons 3.0 Attributions license.

Go for Mobile GamesGopherCon 2016, Denver11th July 2016Takuya Ueda @tenntennKLab Inc.

Slide URL: https://goo.gl/cIvTb5

Page 2: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

What is this talk about?● The Basics of Go Mobile

○ Cross-compile and Android device○ SDK Apps and Native Apps

● Go for Mobile Game○ Event handling○ 2D scene graph

● Advanced Topics○ Distribute apps on Google Play○ How to use Android API from Go

2

Page 3: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

The Basics of Go Mobile

3

● Run on Android Devices● Cross-compile and cgo● SDK Apps and Native Apps

Page 4: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Cross-compile● GOOS and GOARCH

○ Go can cross-compile○ GOOS indicates target OS○ GOARCH indicates target architecture

4

# Build for 32bit Windows$ GOOS=windows GOARCH=386 go build# Build for arm Linux$ GOOS=linux GOARCH=arm go build

A linux/arm binary also works on android devices.

The Basics of Go Mobile / Cross-compile and cgo

Page 5: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Webserver on Android Devices

Watch at Youtube Source Code

Android

Shell on Mac

adb shell

The Basics of Go Mobile / Webserver on Androind Devices 5

Page 6: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

cgo● C codes into Go codesimport "unsafe"/*#include <stdio.h>#include <stdlib.h>void hello(char *s) { printf("Hello, %s\n", s); }*/import "C"func main() {

str := C.CString("GopherCon")C.hello(str)C.free(unsafe.Pointer(str))

}

Comments before import "C" would be built as C codes

Call C’s function from Go code

The Basics of Go Mobile / Cross Compile and cgo 6

Page 7: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

cgo for Android● cgo codes also can be cross-compiled

7

$ CGO_ENABLED=1\ CC=arm-linux-androideabi-gcc\ GOOS=android\ GOARCH=arm\ GOARM=7\ go build -buildmode=pie hellocgo.go$ adb push hellocgo /data/local/tmp

$ chmod 755 /data/local/tmp/hellocgo$ /data/local/tmp/hellocgoHello, GopherCon

GOOS should be androidwhen CGO_ENABLED is 1.

Enable cgo at cross-compiling

adb shell

PC

The Basics of Go Mobile / Cross Compile and cgo

Page 8: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

buildmode● Change output formats

○ archive, c-archive■ build to C archive (.a file)

○ shared, c-shared■ build to shared library (.so file)

○ exe■ build to executable file

○ pie■ build to PIE style executable file

archive and shared ignoremain package

Go can build to .so files for Android

8The Basics of Go Mobile / Cross Compile and cgo

Page 9: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go Mobile● What is Go Mobile?

○ Go Mobile is a toolkit for Mobile Platform (Android and iOS) with Go.

● How Go Mobile works?○ Go Mobile provides bindings of Android and iOS

through cgo.

9

Go CJavaObj-C

JNIcgoAndroid

iOS

The Basics of Go Mobile / Go Mobile

Page 10: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go MobileThe Basics of Go Mobile / Go Mobile 10

https://github.com/golang/mobile

Page 11: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Installation● Install gomobile comand

● Initialize the build tool chain○ gomobile init initializes the build tool chain for

mobile apps. Android NDK is also installed.

11

$ gomobile init -v$ ls $GOPATH/pkg/gomobileandroid-ndk-r12 pkg_android_386 pkg_android_armpkg_darwin_amd64 pkg_darwin_arm64 dlpkg_android_amd64 pkg_android_arm64 pkg_darwin_arm version

$ go get golang.org/x/mobile/cmd/gomobile

The Basics of Go Mobile / Installation

Page 12: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

gomobile commandgomobile command provides sub-commands.● Sub-commands

12

bind build a library for Android and iOS

build compile Android APK and iOS app

clean remove object files and cached gomobile files

init install android compiler toolchain

install compile android APK and install on device

version print version

The Basics of Go Mobile / gomobile command

Page 13: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

SDK Apps and Native AppsGo Mobile provides two ways to develop mobile apps.

■ SDK Apps● Write common funcations in Go as a library● Write UI and platform dependent functions in Java for

Android and Objective-C/Swift for iOS

■ Native Apps● Write UI and all codes in Go

13The Basics of Go Mobile / SDK Apps and Native Apps

Page 14: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go

aar file

SDK Apps and Native Apps

Binding Classes (Java)

Shared library (.so)

Java

● SDK Apps for Android

● Native Apps for Android

apk file

GoGoNativeActivity

Shared library (.so)

UI, IAB, ...

As a library

UI, audio, ...

gomobile bind

gomobile build

The Basics of Go Mobile / SDK Apps and Native Apps 14

Page 15: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go

aar file

SDK Apps and Native Apps

Binding Classes (Java)

Shared library (.so)

Java

● SDK Apps for Android

● Native Apps for Android

apk file

GoGoNativeActivity

Shared library (.so)

UI, IAB, ...

As a library

UI, audio, ...

gomobile bind

gomobile build

The Basics of Go Mobile / SDK Apps and Native Apps 15

Page 16: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

SDK App example: Ivy● Ivy big number calculator (source code)

○ Interpriter for APL-like language○ Android App and iOS App use a same engine○ The engine is written in Go by Rob Pike

16

Google Play App Store

The Basics of Go Mobile / SDK App example: Ivy

Page 17: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

gomobile bind

● Generate an Android Archive (.aar)○ Including a shared library (.so) written in Go○ Including a JAR file which is bult Java bindings

● Develop with Android Studio Plugin○ Runs gomobile bind○ Links to a generated .aar file

$ gomobile bind [-target ios|android] mypkg

17The Basics of Go Mobile / gomobile bind

Page 18: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Contents of AAR18The Basics of Go Mobile / Contents of AAR

$ gomobile bind sample$ unzip -Z1 sample.aarAndroidManifest.xmlproguard.txtclasses.jarjni/armeabi-v7a/libgojni.sojni/arm64-v8a/libgojni.sojni/x86/libgojni.sojni/x86_64/libgojni.soR.txtres/

Compiled Java code

Compiled Go/C code

Page 19: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Binding

Use SDK from Application19The Basics of Go Mobile / Use SDK from Application

Java code

Application Code(Java)

C codeGo/cgo

JNI Generated by gomobile bind

SDK Code(Go)

cgo

Page 20: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Binding Go and Java20The Basics of Go Mobile / Binding Go and Java

Package Abstrct Class

Struct Inner Class

Struct Field Getter/Setter(Native)

Method Method(Native)

Pacakge Function Static Method

Go Java

Page 21: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Binding Go and Java21The Basics of Go Mobile / Binding Go and Java

package samplefunc Hello() string { return "Hello" }type MyStruct struct { Str string }func (s MyStruct) MyMethod() string { return s.Str }

public abstract class Sample {// ...private Sample() {} // uninstantiablepublic static final class MyStruct extends Seq.Proxy {

public final native String getStr();public final native void setStr(String v);public native String MyMethod();// ...

}public static native String Hello();

}

Java

Go

Struct

Field

Method

Package Function

Page 22: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Type restrictionsThe Basics of Go Mobile / 22

● Signed integer and floating point type● String and boolean type● Byte slice type● Any functions

○ parameter and result types must be supported types○ results are 0, 1 or 2 (2nd result must be an error type)

● Any struct type○ all fields and methods must be supported types

● Any interface○ all methods must be supported types

Page 23: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go

aar file

SDK Apps and Native Apps

Binding Classes (Java)

Shared library (.so)

Java

● SDK Apps for Android

● Native Apps for Android

apk file

GoGoNativeActivity

Shared library (.so)

UI, IAB, ...

As a library

UI, audio, ...

gomobile bind

gomobile build

The Basics of Go Mobile / SDK Apps and Native Apps 23

Page 24: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

gomobile build and gomobile install

● Generate an .apk file○ Including a shared library (.so) written in Go○ Including a dex file which is bult GoNativeActivity

● Build and Install ○ Run gomobile build and adb install○ Android Only

$ gomobile build [-target ios|android] mainpkg

24The Basics of Go Mobile / gomobile build and gomobile install

$ gomobile install [-target ios|android] mainpkg

Page 25: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Contents of APK25The Basics of Go Mobile / Contents of APK

$ gomobile build golang.org/x/mobile/example/flappy$ unzip -Z1 flappy.apkAndroidManifest.xmlclasses.dexlib/armeabi-v7a/libflappy.solib/arm64-v8a/libflappy.solib/x86/libflappy.solib/x86_64/libflappy.soassets/READMEassets/sprite.pngMETA-INF/MANIFEST.MFMETA-INF/CERT.SFMETA-INF/CERT.RSA

Compiled GoNativeActivity.java

Compiled Go/C code

Page 26: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

x/mobile/app

How Native App works?26The Basics of Go Mobile / How Native App works?

GoNativeActivity

C code

Go/cgo

JNI

Application Code(Go)

cgo

exntends NativeActivity

Page 27: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go for Mobile Game

27

● Basic event loop and paint events● Size Event and Lifecycle Event● 2D Scene Graph● Touch Event

Page 28: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go for Mobile GameGo for Mobile Game / Go for Mobile Game 28

● Flappy Gopher○ by Andrew Gerrand○ for Go Conference 2015 Winter○ Source Code

● How it works?○ gomobile build○ render images and animate○ handle touch events

$ go get golang.org/x/mobile/cmd/gomobile && gomobile init$ gomobile install golang.org/x/mobile/example/flappy

Page 29: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Packages● Rendering

○ OpenGL ES2 : gl, exp/gl/glutil○ 2D Scene Graph : exp/sprite

● Event○ Touch Event : event/touch○ Lifecycle Event : event/lifecycle

● Sensors : exp/sensor○ Accelerometer, Gyroscope, Magnetometer

● Audio : exp/audio

Go for Mobile Game / Packages 29

Package Name

Page 30: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

app.Main● app.Main receives an entry point function

Go for Mobile Game / app.Main 30

func main() {app.Main(func(a app.App) {

// Event Loop})

}

Page 31: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Event Loop● Receive events through a channel

Go for Mobile Game / Event Loop 31

Event LoopPaint Event

Touch Event

Life Cycle Event

Switch by Event Types

CASE

CASE

CASE

Page 32: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Event Channel● Receive events from App.Events() channel

Go for Mobile Game / Event Channel 32

// Events() <-chan interface{}for e := range a.Events() { // a is app.App

switch e := a.Filter(e).(type) {case paint.Event:case touch.Event:case lifecycle.Event:

}}

Page 33: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Event TypesGo for Mobile Game / Event Types 33

paint.Event Rendering event

touch.Event Screen touch event

lifecycle.EventApp’s lifecycle eventSuch as OnStart and OnStop on Android

size.Eventdimension, physical resolution and orientation changed event

mouse.Event Mouse event

key.Event Only hardware keyboard event

Page 34: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

How to render imagesGo for Mobile Game / How to render images 34

● Construct a scene graph○ exp/sprite package provides 2D scene graph

● Load a texture○ asset package loads a image as a texture

● Set a sub-texture to a node○ A node of scene graph can be set a sub-texture (part of

a texture)

● Render a scene graph○ sprite.Engine type has Render method

Page 35: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

2D scene graphGo for Mobile Game / 2D scene graph 35

scene

gopher

RenderingEngine

ground1

Node

TextureSub-texture

Game SceneScene Graph Render

Set Load

Page 36: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Construct a scene graph● Create a rendering engine

● Create and register a node

● Append a child node

Go for Mobile Game / Construct a scene graph 36

images = glutil.NewImages(glctx)eng = glsprite.Engine(images)

node := &sprite.Node{}eng.Register(node)

parentNode.AppendChild(childNode)

glctx is a OpenGL context

images maintains shared state used

by texutres

Page 37: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Load a texture● Open an asset

● Decode an image

● Load a texture to rendering engine

Go for Mobile Game / Load a texture 37

a, err := asset.Open("gopher.png")

img, err := image.Decode(a)

tex, err := eng.LoadTexture(img)

asset files are put into assets directory

use image package from standard packages

Page 38: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Set a sub-texture● Create a sub-texture

● Set a sub-texture to a node

Go for Mobile Game / Set a sub-texture 38

subtex := sprite.Subtex {T:tex,R:iamge.Rect(128, 0, 128, 128)

}

eng.SetSubTex(node, subtex)

Bounds on a source texture

Page 39: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Render a scene graph● Render receives a root node of a scene graph

Go for Mobile Game / Render a scene graph 39

// compute current frame in 60FPSsince := time.Since(startTime)now := clock.Time(since * 60 / time.Second)

// scene *sprite.Node// now clock.Time// sz event.Sizeeng.Render(scene, now, sz)

Root Node

Screen Size and Orientation

Page 40: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handling EventsGo for Mobile Game / Handling Events 40

Handle Size Event

Handle Lifecycle Event

Handle Paint Event

Get Screen Size and Orientation

Get OpenGL Contextand Construct a Scene Graph

Render a Scene Graph

Page 41: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Size EventGo for Mobile Game / Handle Size Event 41

app.Main(func(a app.App) {var glctx gl.Contextvar sz size.Eventfor e := range a.Events() {

switch e := a.Filter(e).(type) {case size.Event:

sz = e// ...}

}})

type Event struct {WidthPx, HeightPx intWidthPt, HeightPt geom.PtPixelsPerPt float32Orientation Orientation

}

Page 42: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Lifecycle EventGo for Mobile Game / Handle Lifecycle Event 42

case lifecycle.Event:switch e.Crosses(lifecycle.StageVisible) {case lifecycle.CrossOn:

glctx, _ = e.DrawContext.(gl.Context)onStart(glctx)a.Send(paint.Event{})

case lifecycle.CrossOff:onStop()glctx = nil

}

Get a OpenGL Context

StageDeadStageAlive

StageVisibleStageFocused

Page 43: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Lifecycle EventGo for Mobile Game / Handle Lifecycle Event 43

StageDead

StageAlive

StageVisible

StageFocused

From

To

type Event struct { From, To Stage DrawContext interface{}}

lifecycle.Event

e.Crosses(StageVisible) == CrossOn

Page 44: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Lifecycle EventGo for Mobile Game / Handle Lifecycle Event 44

StageDead

StageAlive

StageVisible

StageFocused

To

From

type Event struct { From, To Stage DrawContext interface{}}

lifecycle.Event

e.Crosses(StageAlive) == CrossOff

Page 45: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Paint EventGo for Mobile Game / Handle Paint Event 45

Arrange Nodes

Set Sub-texture

Render Scene Graph

Set an Affine Transformation Matrix Relative to Parent Node

Change images for animations

Page 46: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Arrange a Scene Graph NodeGo for Mobile Game / Arrange a Scene Graph Node 46

type Arranger interface{Arrange(e Engine, n *Node, t clock.Time)

}

// For convinience, arrangeFunc implements Arrange interfacetype arrangeFunc func(e Engine, n *Node, t clock.Time)func (f arrangeFunc) Arrange(e Engine, n *Node, t clock.Time) {

f(e, n, t) }

● sprite.Arrranger has Arrange method which is called each frame by rendering engine

Page 47: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Set Arranger to a Scene Graph NodeGo for Mobile Game / Set Arranger to a Scene Graph Node 47

// Called each framefunc arrange(e sprite.Engine,

n *sprite.Node, t clock.Time) {

// Node manupulation}

// Set a Arranger interface to a nodenode.Arranger = arrangerFunc(arrange)

Page 48: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Affine Transformation MatrixGo for Mobile Game / Affine Transformation Matrix 48

// Scale(2,2) and Translate(5,5)eng.SetTransform(node, f32.Affine{{2, 0, 5},{0, 2, 5},

})

● A scene graph node can set a affine transform matrix to compute position, scale and rotation angle

Page 49: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Affine TransformGo for Mobile Game / Affine Transform 49

● Affine transform from the root node to the child node

root

n

Translate(5,5)Scale(2,2)

Translate(10,10)Scale(100,100)

pos: (5,5)size: 2pt x 2pt

pos: (25,25)size: 200pt x 200pt

1pt = 1/72 inch

Base size is 1pt x 1ptBase position is (0,0)Base rotation angle is 0

Page 50: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Affine TransformGo for Mobile Game / Affine Transform 50

root

n

pos: (5,5)size: 2pt x 2pt

pos: (25,25)size: 200pt x 200pt

eng.SetTransform(root,f32.Affine{

{2, 0, 5},{0, 2, 5},

})

eng.SetTransform(n,f32.Affine{

{100, 0, 10},{0, 100, 10},

})

pos: (0,0)size: 1pt x 1pt

Page 51: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Touch EventGo for Mobile Game / Handle Touch Event 51

type Event struct {// Touch location in pixels.X, Y float32// Sequence number for each touch.Sequence Sequence// Type is the touch type.// TypeBegin, TypeMove, TypeEndType Type

}

touch.Event

Page 52: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Touch Types and SequenceGo for Mobile Game / Touch Types and Sequence 52

TypeBegin TypeMove

Same Sequence

TypeEnd

Sequence:1

Multi Touch

Sequence:2

Page 53: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Debug on PC● Go Mobile’s packages also works fine on PC

(Mac, Windows, Linux).

Go for Mobile Game / Debug on PC 53

$ go build mainpkg$ ./mainpkg

assets directry should be placed at working directory

Running on Mac

Page 54: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Restrictions for Native Apps (Android)● Cannot set own App’s icon● Only use a debug keystore● Cannot distribute on Google Play

○ Need to set own App’s icon○ Need a release keystore

● Cannot use Platform API○ Cannot use In-app Billing API○ Cannot use third party API

Go for Mobile Game / Debug on PC 54

Page 55: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Advanced Topics

55

● Distribute apps on Google Play● Use Platform APIs from Go

Page 56: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Contents of APK56Advanced Topics / Contents of APK

$ gomobile build golang.org/x/mobile/example/flappy$ unzip -Z1 flappy.apkAndroidManifest.xmlclasses.dexlib/armeabi-v7a/libflappy.solib/arm64-v8a/libflappy.solib/x86/libflappy.solib/x86_64/libflappy.soassets/READMEassets/sprite.pngMETA-INF/MANIFEST.MFMETA-INF/CERT.SFMETA-INF/CERT.RSA

res directory is not included into APK

Signature Related Files

Page 57: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Modify APK for Release57Advanced Topics / Modify APK for Release

gomobile build● Create an APK file for debug● Expand the APK file by apktool● Modify the AndroidManifest

○ Add icon setting and Disable debuggable setting● Add app’s icons and res directory● Rearchive an APK file by apktool● Signature with release keystore● ZIP Align

Page 58: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Expand an APK FileAdvanced Topics / Expand an APK File 58

# if you have not installed$ brew install apktool$ apktool d flappy.apk # expand

● Use apktool to expand and archive an APK file

Page 59: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Add App’s IconsAdvanced Topics / Add App’s Icons 59

$ tree flappy/resflappy/res├── drawable-hdpi│ └── ic_launcher.png...└── drawable-xxxhdpi └── ic_launcher.png

● Make a res direcotry and add app’s icons○ under the expanded APK directory

App’s Icons

Page 60: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Add icon setting● Add an icon setting to AndroidManifest● android:icon sepecify icon file name

Advanced Topics / Add icon setting 60

<?xml version="1.0" encoding="utf-8"?>...

<application android:label="Flappy"android:icon="@drawable/ic_launcher"

...

Page 61: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Disable debuggable setting● To upload apk on Google Play, apk should be

built without debuggable="true"○ We can also use own AndroidManifest.xml by

putting top level directory before building.

Advanced Topics / Disable debuggable setting 61

<?xml version="1.0" encoding="utf-8"?>...

<application android:label="Flappy" ... android:debuggable="true"

...

Page 62: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Signiture with Release Keystore62Advanced Topics / Signiture with Release Keystore

● Re-archive the APK file● Re-signiture the APK file

$ apktool b flappy$ cd flappy/dist$ jarsigner ... -keystore release.keystore \ flappy.apk flappy$ zipalign -f -v 4 flappy{,_aligned}.apk$ lsflappy.pak flappy_aligned.apk

Page 63: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Distribute on Google Play63Advanced Topics / Distribute on Google Play

Page 64: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Restrictions for Native App (Android)● Cannot set own App’s icon● Only use a debug keystore● Cannot distribute on Google Play

○ Need to set own App’s icon○ Need a release keystore

● Cannot use Platform API○ Cannot use In-app Billing API○ Cannot use third party API

Advanced Topics / Debug on PC 64

OK!

OK!

OK!

Page 65: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Requirement Mobile Games for Business● In-app Billing

○ Purchase a items in the game● SNS connection

○ Facebook, Twitter, ...● Advertisements● Analytics

○ Google Analytics, Firebase, Facebook Analytics,...

Advanced Topics / Requirement of Mobile Games for Business 65

These APIs are provided as Java SDK for Android

Page 66: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

How to call Android API from GoAdvanced Topics / How to call Android API from 66

● Call Android API through cgo by JNI○ We should get JavaVM* object in own cgo code

● JavaVM* object is defined in Go Mobile inner package as current_vm variable○ x/mobile/internal/mobileinit/ctx_android.go

● We also use current_vm in own cgo code

Page 67: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Call android.util.Log.dAdvanced Topics / Call android.util.Log.d 67

RunOnJVM(func(vm, jniEnv, ctx uintptr) error {env := (*C.JNIEnv)(unsafe.Pointer(jniEnv))C.logd(env)return nil

})

void logd(JNIEnv* env) {jclass log = (*env)->FindClass(env, "android/util/Log");jmethodID methodD = (*env)->GetStaticMethodID(

env, log, "d", "(Ljava/lang/String;Ljava/lang/String;)I");jstring tag = (*env)->NewStringUTF(env, "log");jstring message = (*env)->NewStringUTF(env, "hi! JVM");(*env)->CallStaticIntMethod(env, log, methodD, tag, message);

}

Go

C

All Source Code

RunOnJVM is copied fromctx_android.go

Page 68: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Call own Java Codes from Go codesAdvanced Topics / Call own Java Codes from Go codes 68

● Create a new Android project● Copy GoNativeActivity and AndroidManifest

from GoMobile repositry to own project● Write own Java codes● Write application code in Go

○ Call own Java codes with RunOnJVM● Build Go code to a shared library● Copy shared library to own Android project● Build the Android project Do all processes by hand!

Page 69: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Toast on Go MobileAdvanced Topics / Toast on Go Mobile 69

Page 70: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Plugin System for Game EngineAdvanced Topics / Plugin System for Game Engine 70

● Most Game Engine has a plugin system○ Unity, Cocos2d-x, ...○ Facebook Plugin, In-app Billing Plugin, ...

● Go Mobile needs a plugin system

Application Code(Go)

Own Plugin

Java/Obj-C codeC codeGo/cgo

Go MobileRegister

Use

Page 71: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Summaries● The Basics of Go Mobile

○ Cross-compile and Android device○ SDK App and Native App

● Go for Mobile Game○ Event handling○ 2D scene graph

● Advanced Topics○ Distribute apps on Google Play○ How to use Android API from Go

71

Page 72: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go for Unity Native Plugin● Unity Native Plugin can be written in Go/cgo

Advanced Topics / Go for Unity Native Plugin 72

UnityNative Plugin

.so for Android.a for iOS

Written in GoGo has good packages to create a communication module!

Page 73: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Go for Unity Native PluginAdvanced Topics / Go for Unity Native Plugin 73

package mainimport "C"//export Hogefunc Hoge() int {return 100}func main() {}

#if UNITY_IPHONE [DllImport("__Internal")]#else[DllImport("hoge")]#endifprivate static extern int Hoge();void Start() {Debug.LogFormat("{0}", Hoge());}

Go

Unity (C#)

Page 74: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Build a Native Plugin for AndroidAdvanced Topics / Build a Native Plugin for Android 74

$ CGO_ENABLED=1 \CC=arm-linux-androideabi-gcc \GOOS=android \GOARCH=arm \GOARM=7 \go build -buildmode=c-shared \ -pkgdir=$GOPATH/pkg/gomobile/pkg_android_arm \-o libhoge.so hoge.go

Page 75: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Build a Native Plugin for iOSAdvanced Topics / Build a Native Plugin for iOS 75

GOOS=darwin \GOARCH=arm \GOARM=7 \CC=`xcrun --sdk iphoneos -f clang` \CXX=`xcrun --sdk iphoneos -f clang` \CGO_CFLAGS="-isysroot `xcrun --sdk iphoneos --show-sdk-path` -arch armv7 -miphoneos-version-min=6.1" \CGO_LDFLAGS="-isysroot `xcrun --sdk iphoneos --show-sdk-path` -arch armv7 -miphoneos-version-min=6.1" \CGO_ENABLED=1 \go build -pkgdir=$GOPATH/pkg/gomobile/pkg_darwin_arm \-buildmode=c-archive -tags=ios -o hoge.a hoge.go

Page 76: Go for Mobile Games

Takuya Ueda@tenntennKLab Inc.

76

Page 77: Go for Mobile Games

Takuya Ueda@tenntennKLab Inc.

77

Page 78: Go for Mobile Games

Takuya Ueda@tenntennKLab Inc.

78

Page 79: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Run on Android Devices79

● Go can build for Android Devices

Android DevicePC

Cross-compile Run on adb shellpush binaryto Android Deivce

$ adb push cmd /sdcard

The Basics of Go Mobile / Run on Androind Devices

Page 80: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Run on Android Devices

Watch at Youtube Source Code

Android

Shell on Mac

adb shell

The Basics of Go Mobile / Run on Androind Devices 80

Page 81: Go for Mobile Games

Slide URL: https://goo.gl/cIvTb5

Handle Lifecycle EventGo for Mobile Game / Handle Lifecycle Event 81

case lifecycle.Event:switch e.Crosses(lifecycle.StageVisible) {case lifecycle.CrossOn:

glctx, _ = e.DrawContext.(gl.Context)onStart(glctx)a.Send(paint.Event{})

case lifecycle.CrossOff:onStop()glctx = nil

}

StageDead

StageAlive

StageVisible

StageFocused