go for mobile games
TRANSCRIPT
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
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
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
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
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
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
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
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
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
Slide URL: https://goo.gl/cIvTb5
Go MobileThe Basics of Go Mobile / Go Mobile 10
https://github.com/golang/mobile
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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})
}
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
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:
}}
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
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
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
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
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
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
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
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
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
}
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
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
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
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
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
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)
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
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
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
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
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
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
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
Slide URL: https://goo.gl/cIvTb5
Advanced Topics
55
● Distribute apps on Google Play● Use Platform APIs from Go
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
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
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
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
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"
...
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"
...
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
Slide URL: https://goo.gl/cIvTb5
Distribute on Google Play63Advanced Topics / Distribute on Google Play
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!
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
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
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
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!
Slide URL: https://goo.gl/cIvTb5
Toast on Go MobileAdvanced Topics / Toast on Go Mobile 69
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
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
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!
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#)
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
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
Takuya Ueda@tenntennKLab Inc.
76
Takuya Ueda@tenntennKLab Inc.
77
Takuya Ueda@tenntennKLab Inc.
78
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
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
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