raylib-go

repository·master·Indexed 25 days ago

https://github.com/gen2brain/raylib-go

Golang 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.

Tokens
21.3K
Snippets
18
Records
143
Agent score
82%

What's inside raylib-go

  1. Use raygui for immediate mode GUI development

    master
    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.
  2. Use the physics package for 2D physics in videogames

    master
    The physics package provides a 2D physics engine for videogames. It is a Go port of Victor Fisac's physac engine. Use this package to implement physical behaviors like collisions and movement in your raylib-go projects.
  3. Use the easings package for value animation

    master
    The easings package 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.
  4. Use raylib-go without cgo (purego)

    master

    You can use raylib-go on Linux, macOS, Windows, and FreeBSD without cgo by setting CGO_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_embed or the environment variable RAYLIB_NO_EMBED=1.

  5. Cross-compile for macOS from Linux

    master

    To 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'"
  6. Compile a shared library for Android (armeabi-v7a)

    master

    To 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 build command with specific CGO flags.

    Prerequisites:

    • Android NDK (unpacked to a local directory)
    • ANDROID_NDK_HOME set to your NDK location
    • ANDROID_API set to 16 (or your target version)

    Steps:

    1. Export NDK paths and toolchain binaries to your PATH.
    2. Set ANDROID_SYSROOT and ANDROID_TOOLCHAIN.
    3. Execute the go build command with GOOS=android and GOARCH=arm using -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.so
  7. Install raylib-go

    master

    Install 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/raylib
  8. System requirements for cgo

    master

    If 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-dev

    Fedora

    dnf install mesa-libGL-devel libXi-devel libXcursor-devel libXrandr-devel libXinerama-devel wayland-devel libxkbcommon-devel

    macOS

    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.

  9. Cross-compile for Windows from Linux

    master

    To 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"