raylib-go
repository·master·Indexed 25 days ago
https://github.com/gen2brain/raylib-goGolang bindings for raylib, a library for videogame programming. It supports both cgo and purego modes and includes the raylib C source code. The project provides additional packages such as 'easings' for value animation (a port of Robert Penner's equations), 'physics' for 2D physics (a port of Victor Fisac's physac engine), and 'raygui' for immediate mode GUI development.
What's inside raylib-go
- raygui is a simple and easy-to-use IMGUI (immediate mode GUI) library for raylib-go. Because it is an immediate mode API, you call the GUI functions every frame within your main game loop to define and render UI elements like buttons, sliders, and panels.
Use the physics package for 2D physics in videogames
masterThephysicspackage provides a 2D physics engine for videogames. It is a Go port of Victor Fisac'sphysacengine. Use this package to implement physical behaviors like collisions and movement in your raylib-go projects.Use the easings package for value animation
masterTheeasingspackage provides a collection of easing functions used to animate values smoothly over time. It is a Go port of Robert Penner's easing equations. These functions are typically used to calculate an interpolated value based on a progress factor (usually between 0.0 and 1.0) to create effects like acceleration, deceleration, or bouncing.Use raylib-go without cgo (purego)
masterYou can use
raylib-goonLinux,macOS,Windows, andFreeBSDwithoutcgoby settingCGO_ENABLED=0.Limitations:
- 32-bit architectures are not supported.
Embedded Libraries: Shared libraries (
.dll,.so,.dylib) for raylib are embedded by default for:- Linux (arm64/amd64)
- macOS (arm64/amd64)
- Windows (arm64/amd64)
You can disable this embedding using the build tag
raylib_no_embedor the environment variableRAYLIB_NO_EMBED=1.Compile a shared library for Android (aarch64/arm64)
masterTo target 64-bit ARM (
arm64), modify the standard Android build process with the following changes:- Replace
arm-linux-androideabiwithaarch64-linux-androidin the path exports andCCvariable. - Set
GOARCHtoarm64. - Use a minimum
ANDROID_APIof21.
- Replace
Cross-compile for macOS from Linux
masterTo cross-compile for macOS using
cgo, install the OSXCross toolchain. Use the following commands for different architectures:For x86_64 (Intel):
$ CGO_ENABLED=1 CC=x86_64-apple-darwin21.1-clang GOOS=darwin GOARCH=amd64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=10.15'"For arm64 (Apple Silicon):
$ CGO_ENABLED=1 CC=aarch64-apple-darwin21.1-clang GOOS=darwin GOARCH=arm64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=12.0.0'"$ CGO_ENABLED=1 CC=x86_64-apple-darwin21.1-clang GOOS=darwin GOARCH=amd64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=10.15'" $ CGO_ENABLED=1 CC=aarch64-apple-darwin21.1-clang GOOS=darwin GOARCH=arm64 go build -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=12.0.0'"Compile a shared library for Android (armeabi-v7a)
masterTo compile the example into a shared library for Android (32-bit ARM), you must have the Android NDK installed (Note: NDK 27 is not supported). You need to set up environment variables for the NDK path, toolchain, sysroot, and API version, then run the
go buildcommand with specificCGOflags.Prerequisites:
- Android NDK (unpacked to a local directory)
ANDROID_NDK_HOMEset to your NDK locationANDROID_APIset to16(or your target version)
Steps:
- Export NDK paths and toolchain binaries to your
PATH. - Set
ANDROID_SYSROOTandANDROID_TOOLCHAIN. - Execute the
go buildcommand withGOOS=androidandGOARCH=armusing-buildmode=c-shared.
export ANDROID_NDK_HOME=/opt/android-ndk export PATH=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH} export ANDROID_SYSROOT=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/sysroot export ANDROID_TOOLCHAIN=${ANDROID_NDK_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 export ANDROID_API=16 CC="armv7a-linux-androideabi${ANDROID_API}-clang" \ CGO_CFLAGS="-I${ANDROID_SYSROOT}/usr/include -I${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi --sysroot=${ANDROID_SYSROOT} -D__ANDROID_API__=${ANDROID_API}" \ CGO_LDFLAGS="-L${ANDROID_SYSROOT}/usr/lib/arm-linux-androideabi/${ANDROID_API} -L${ANDROID_TOOLCHAIN}/arm-linux-androideabi/lib --sysroot=${ANDROID_SYSROOT}" \ CGO_ENABLED=1 GOOS=android GOARCH=arm \ go build -buildmode=c-shared -ldflags="-s -w -extldflags=-Wl,-soname,libexample.so" \ -o=android/libs/armeabi-v7a/libexample.soInstall raylib-go
masterInstall the raylib bindings using
go get. The package includes the raylib C source code and compiles it together with the bindings. Note that the initial build may take several minutes.go get -v -u github.com/gen2brain/raylib-go/raylibSystem requirements for cgo
masterIf you are using
cgo(the default), you must install the following system dependencies based on your OS:Ubuntu
apt-get install libgl1-mesa-dev libxi-dev libxcursor-dev libxrandr-dev libxinerama-dev libwayland-dev libxkbcommon-devFedora
dnf install mesa-libGL-devel libXi-devel libXcursor-devel libXrandr-devel libXinerama-devel wayland-devel libxkbcommon-develmacOS
Requires Xcode or Command Line Tools for Xcode.
Windows
Requires a C compiler such as Mingw-w64 or TDM-GCC. You can also use the MSYS2 shell.
Note: To build a Windows application without a console window, use the flag
-ldflags "-H=windowsgui"during build.Use rGuiIcons to manage icon files
masterTheraylib-goGUI examples utilize the.rgi-fileformat for icon sets. You can use the rGuiIcons tool to view, create, or manage these custom icon files, which are then loaded into your application to be drawn as individual icons.Cross-compile for Windows from Linux
masterTo cross-compile for Windows using
cgo, install the MinGW toolchain. Use the following commands for different architectures:For x86_64 (64-bit):
$ CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -ldflags "-s -w"For Intel 80386 (32-bit):
$ CGO_ENABLED=1 CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 go build -ldflags "-s -w"$ CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" $ CGO_ENABLED=1 CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 go build -ldflags "-s -w"Build Android shared libraries on Windows
masterTo build shared libraries on Windows, use the providedandroidcompile.batscript. You should open this file in a text editor to configure the necessary paths and settings before executing it to generate the libraries.