From 17941ec954f9343f382c38cbf170a6e5e2577c6f Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Wed, 22 Feb 2017 12:11:18 +0100 Subject: [PATCH] RPi example --- README.md | 4 ++ examples/README.md | 3 ++ examples/android/example/bootstrap.sh | 18 ++++---- examples/rpi/basic_window/README.md | 20 +++++++++ examples/rpi/basic_window/bootstrap.sh | 61 ++++++++++++++++++++++++++ examples/rpi/basic_window/main.go | 21 +++++++++ 6 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/rpi/basic_window/README.md create mode 100755 examples/rpi/basic_window/bootstrap.sh create mode 100644 examples/rpi/basic_window/main.go diff --git a/README.md b/README.md index a249bef..2552f2c 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ On OS X system OpenAL framework is used. [Android example](https://github.com/gen2brain/raylib-go/tree/master/examples/android/example). +##### Raspberry Pi + +[RPi example](https://github.com/gen2brain/raylib-go/tree/master/examples/rpi/basic_window). + ### Installation go get -v github.com/gen2brain/raylib-go/raylib diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..b5fcf3a --- /dev/null +++ b/examples/README.md @@ -0,0 +1,3 @@ +## Examples + +Live (web/emscripten) examples are available on [raylib website](http://www.raylib.com/examples.html). diff --git a/examples/android/example/bootstrap.sh b/examples/android/example/bootstrap.sh index 60287e7..a3ef780 100755 --- a/examples/android/example/bootstrap.sh +++ b/examples/android/example/bootstrap.sh @@ -22,28 +22,28 @@ export PATH=${INSTALL_PREFIX}/android-arm7/bin:${INSTALL_PREFIX}/android-arm64/b BUILD_DIR=`mktemp -d` mkdir -p ${BUILD_DIR}/bootstrap -echo "Make standalone android toolchains" +echo "##### Make standalone android toolchains" ${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=${INSTALL_PREFIX}/android-arm7 --toolchain=arm-linux-androideabi-4.9 ${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=${INSTALL_PREFIX}/android-arm64 --toolchain=aarch64-linux-android-4.9 -echo "Download Go binaries" +echo "##### Download Go binaries" cd ${BUILD_DIR}/bootstrap && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.${GO_OS}-${GO_ARCH}.tar.gz | tar xz -echo "Download Go source" +echo "##### Download Go source" cd ${BUILD_DIR} && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.src.tar.gz | tar xz && cd ${BUILD_DIR}/go/src -echo "Compile Go for host" +echo "##### Compile Go for host" GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go ./make.bash || exit 1 -echo "Compile Go for arm-linux-androideabi" +echo "##### Compile Go for arm-linux-androideabi" GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=arm-linux-androideabi-gcc GOOS=android GOARCH=arm CGO_ENABLED=1 ./make.bash --no-clean || exit 1 -echo "Compile Go for aarch64-linux-android" +echo "##### Compile Go for aarch64-linux-android" GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=aarch64-linux-android-gcc GOOS=android GOARCH=arm64 CGO_ENABLED=1 ./make.bash --no-clean || exit 1 cp -r -f ${BUILD_DIR}/go ${INSTALL_PREFIX} -echo "Compile OpenAL" +echo "##### Compile OpenAL" cd ${BUILD_DIR} && curl -s -L http://kcat.strangesoft.net/openal-releases/openal-soft-${OPENAL_VERSION}.tar.bz2 | tar -xj @@ -75,7 +75,7 @@ cd ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-arm64 cmake -DLIBTYPE=STATIC -DCMAKE_TOOLCHAIN_FILE=../android-arm64.cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}/android-arm64 .. make -j $(nproc) && make install -echo "Compile android_native_app_glue" +echo "##### Compile android_native_app_glue" mkdir -p ${BUILD_DIR}/native_app_glue/jni cp -r ${ANDROID_NDK_HOME}/sources/android/native_app_glue/* ${BUILD_DIR}/native_app_glue/jni/ @@ -89,5 +89,5 @@ cp ${BUILD_DIR}/native_app_glue/obj/local/arm64-v8a/libandroid_native_app_glue.a cp ${ANDROID_NDK_HOME}/sources/android/native_app_glue/android_native_app_glue.h ${INSTALL_PREFIX}/android-arm7/include/ cp ${ANDROID_NDK_HOME}/sources/android/native_app_glue/android_native_app_glue.h ${INSTALL_PREFIX}/android-arm64/include/ -echo "Remove build directory" +echo "##### Remove build directory" rm -rf ${BUILD_DIR} diff --git a/examples/rpi/basic_window/README.md b/examples/rpi/basic_window/README.md new file mode 100644 index 0000000..5ce4f6a --- /dev/null +++ b/examples/rpi/basic_window/README.md @@ -0,0 +1,20 @@ +### Raspberry Pi example + +To compile example for Raspberry Pi you will need RPi toolchain and userspace libraries, and Go must be cross compiled for linux/arm with that toolchain. + +There is a bootstrap.sh script that you can use to cross compile Go and OpenAL. + +Compile Go and OpenAL, /usr/local is prefix where Go and RPi toolchain and libraries will be installed: + + ./bootstrap.sh /usr/local + +After build is complete point GOROOT to new Go installation in /usr/local, and add toolchain bin directory to PATH: + + export GOROOT=/usr/local/go + export PATH=/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:${PATH} + +And compile example: + + CGO_CFLAGS="-I/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/include -I/usr/local/vc/include -I/usr/local/vc/include/interface/vcos -I/usr/local/vc/include/interface/vmcs_host/linux -I/usr/local/vc/include/interface/vcos/pthreads" \ + CGO_LDFLAGS="-L/usr/local/vc/lib -L/usr/local/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/lib -ldl" \ + CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=6 ${GOROOT}/bin/go build -v -x diff --git a/examples/rpi/basic_window/bootstrap.sh b/examples/rpi/basic_window/bootstrap.sh new file mode 100755 index 0000000..024c5c1 --- /dev/null +++ b/examples/rpi/basic_window/bootstrap.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +if [ -z "$1" ]; then + echo "Usage: bootstrap.sh " + exit 1 +fi + +GO_OS="linux" +GO_ARCH="amd64" +GO_VERSION=`curl -s https://golang.org/dl/ | grep 'id="go' | head -n1 | awk -F'"' '{print $4}'` + +OPENAL_VERSION="1.17.2" + +INSTALL_PREFIX="$1" +export PATH=${INSTALL_PREFIX}/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:${PATH} + +BUILD_DIR=`mktemp -d` +mkdir -p ${BUILD_DIR}/bootstrap + +echo "##### Download toolchain" +cd ${BUILD_DIR} && git clone --depth=1 --branch=master https://github.com/raspberrypi/tools.git +cp -r -f ${BUILD_DIR}/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64 ${INSTALL_PREFIX} + +echo "##### Download userspace libraries" +cd ${BUILD_DIR} && git clone --depth=1 --branch=master https://github.com/raspberrypi/firmware.git +cp -r -f ${BUILD_DIR}/firmware/hardfp/opt/vc ${INSTALL_PREFIX} + +echo "##### Download Go binaries" +cd ${BUILD_DIR}/bootstrap && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.${GO_OS}-${GO_ARCH}.tar.gz | tar xz + +echo "##### Download Go source" +cd ${BUILD_DIR} && curl -s -L http://storage.googleapis.com/golang/${GO_VERSION}.src.tar.gz | tar xz && cd ${BUILD_DIR}/go/src + +echo "##### Compile Go for host" +GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go ./make.bash || exit 1 + +echo "##### Compile Go for arm-linux-gnueabihf" +GOROOT_BOOTSTRAP=${BUILD_DIR}/bootstrap/go CC_FOR_TARGET=arm-linux-gnueabihf-gcc GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 ./make.bash --no-clean || exit 1 + +cp -r -f ${BUILD_DIR}/go ${INSTALL_PREFIX} + +echo "##### Compile OpenAL" + +cd ${BUILD_DIR} && curl -s -L http://kcat.strangesoft.net/openal-releases/openal-soft-${OPENAL_VERSION}.tar.bz2 | tar -xj + +cat << EOF > ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/linux-rpi.cmake +set(TOOLCHAIN_PREFIX arm-linux-gnueabihf) +set(CMAKE_C_COMPILER \${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_FIND_ROOT_PATH \${INSTALL_PREFIX}/gcc-linaro-arm-linux-gnueabihf-raspbian-x64) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +EOF + +mkdir -p ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-rpi +cd ${BUILD_DIR}/openal-soft-${OPENAL_VERSION}/build-rpi +cmake -DLIBTYPE=STATIC -DCMAKE_TOOLCHAIN_FILE=../linux-rpi.cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}/gcc-linaro-arm-linux-gnueabihf-raspbian-x64 .. +make -j $(nproc) && make install + +echo "##### Remove build directory" +rm -rf ${BUILD_DIR} diff --git a/examples/rpi/basic_window/main.go b/examples/rpi/basic_window/main.go new file mode 100644 index 0000000..7785d1a --- /dev/null +++ b/examples/rpi/basic_window/main.go @@ -0,0 +1,21 @@ +package main + +import "github.com/gen2brain/raylib-go/raylib" + +func main() { + raylib.InitWindow(800, 450, "raylib [rpi] example - basic window") + + raylib.SetTargetFPS(60) + + for !raylib.WindowShouldClose() { + raylib.BeginDrawing() + + raylib.ClearBackground(raylib.RayWhite) + + raylib.DrawText("Congrats! You created your first window!", 190, 200, 20, raylib.LightGray) + + raylib.EndDrawing() + } + + raylib.CloseWindow() +}