From 696db895ff72ffd17aac80dc7d7f328d01813019 Mon Sep 17 00:00:00 2001 From: Daniel Ortiz Pereira da Silva Date: Fri, 19 Dec 2014 15:17:48 -0200 Subject: [PATCH] example/sprite: make example lives Add necessary files to build and run example/sprite. Change main.go to get image from assets. Change-Id: Ic3065aab97f59c34f2c0446ab4b46840305b5864 Reviewed-on: https://go-review.googlesource.com/1882 Reviewed-by: David Crawshaw --- example/sprite/AndroidManifest.xml | 25 ++++++++++ example/sprite/all.bash | 15 ++++++ example/sprite/all.bat | 16 +++++++ .../sprite/assets}/waza-gophers.jpeg | Bin example/sprite/build.xml | 15 ++++++ example/sprite/jni/Android.mk | 11 +++++ example/sprite/main.go | 8 ++-- example/sprite/make.bash | 17 +++++++ example/sprite/make.bat | 44 ++++++++++++++++++ 9 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 example/sprite/AndroidManifest.xml create mode 100644 example/sprite/all.bash create mode 100644 example/sprite/all.bat rename {testdata => example/sprite/assets}/waza-gophers.jpeg (100%) create mode 100644 example/sprite/build.xml create mode 100644 example/sprite/jni/Android.mk create mode 100644 example/sprite/make.bash create mode 100644 example/sprite/make.bat diff --git a/example/sprite/AndroidManifest.xml b/example/sprite/AndroidManifest.xml new file mode 100644 index 0000000..ac1c005 --- /dev/null +++ b/example/sprite/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + diff --git a/example/sprite/all.bash b/example/sprite/all.bash new file mode 100644 index 0000000..9ee4a54 --- /dev/null +++ b/example/sprite/all.bash @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Copyright 2014 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +# Script to build and launch the app on an android device. + +set -e + +./make.bash + +adb install -r bin/nativeactivity-debug.apk + +adb shell am start -a android.intent.action.MAIN \ + -n com.example.sprite/android.app.NativeActivity diff --git a/example/sprite/all.bat b/example/sprite/all.bat new file mode 100644 index 0000000..18d9974 --- /dev/null +++ b/example/sprite/all.bat @@ -0,0 +1,16 @@ +:: Copyright 2014 The Go Authors. All rights reserved. +:: Use of this source code is governed by a BSD-style +:: license that can be found in the LICENSE file. + +@echo off + +setlocal + +echo # building sprite +call make.bat + +echo # installing bin/nativeactivity-debug.apk +adb install -r bin/nativeactivity-debug.apk >nul + +echo # starting android.app.NativeActivity +adb shell am start -a android.intent.action.MAIN -n com.example.sprite/android.app.NativeActivity >nul \ No newline at end of file diff --git a/testdata/waza-gophers.jpeg b/example/sprite/assets/waza-gophers.jpeg similarity index 100% rename from testdata/waza-gophers.jpeg rename to example/sprite/assets/waza-gophers.jpeg diff --git a/example/sprite/build.xml b/example/sprite/build.xml new file mode 100644 index 0000000..6bfc783 --- /dev/null +++ b/example/sprite/build.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/example/sprite/jni/Android.mk b/example/sprite/jni/Android.mk new file mode 100644 index 0000000..b0bf948 --- /dev/null +++ b/example/sprite/jni/Android.mk @@ -0,0 +1,11 @@ +# Copyright 2014 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := sprite +LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libsprite.so + +include $(PREBUILT_SHARED_LIBRARY) diff --git a/example/sprite/main.go b/example/sprite/main.go index efe78da..8cd2857 100644 --- a/example/sprite/main.go +++ b/example/sprite/main.go @@ -8,7 +8,6 @@ import ( "image" "log" "math" - "os" "time" _ "image/jpeg" @@ -125,12 +124,13 @@ const ( ) func loadTextures() []sprite.SubTex { - f, err := os.Open("../../testdata/waza-gophers.jpeg") + a, err := app.Open("waza-gophers.jpeg") if err != nil { log.Fatal(err) } - defer f.Close() - img, _, err := image.Decode(f) + defer a.Close() + + img, _, err := image.Decode(a) if err != nil { log.Fatal(err) } diff --git a/example/sprite/make.bash b/example/sprite/make.bash new file mode 100644 index 0000000..f45ae8d --- /dev/null +++ b/example/sprite/make.bash @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Copyright 2014 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +set -e + +if [ ! -f make.bash ]; then + echo 'make.bash must be run from $GOPATH/src/golang.org/x/mobile/example/sprite' + exit 1 +fi + +mkdir -p jni/armeabi +CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \ + go build -ldflags="-shared" -o jni/armeabi/libsprite.so . +ndk-build NDK_DEBUG=1 +ant debug diff --git a/example/sprite/make.bat b/example/sprite/make.bat new file mode 100644 index 0000000..5dab3f9 --- /dev/null +++ b/example/sprite/make.bat @@ -0,0 +1,44 @@ +:: Copyright 2014 The Go Authors. All rights reserved. +:: Use of this source code is governed by a BSD-style +:: license that can be found in the LICENSE file. + +@echo off + +setlocal + +if not exist make.bat goto error-invalid-path + +if not exist jni\armeabi mkdir jni\armeabi + +set CGO_ENABLED=1 +set GOOS=android +set GOARCH=arm +set GOARM=7 + +go build -ldflags="-shared" -o jni/armeabi/libsprite.so . +if errorlevel 1 goto error-go-build + +if defined NDK_ROOT goto ndk-build +echo NDK_ROOT path not defined +goto end + +:ndk-build +call %NDK_ROOT%\ndk-build.cmd NDK_DEBUG=1 >nul + +if defined ANT_HOME goto ant-build +echo ANT_HOME path not defined +goto end + +:ant-build +call %ANT_HOME%\bin\ant.bat debug >nul +goto end + +:error-invalid-path +echo make.bat must be run from %%GOPATH%%\src\golang.org\x\mobile\example\sprite +goto end + +:error-go-build +echo Error building go lib +goto end + +:end \ No newline at end of file