go-sdl2

repository·master·Indexed 25 days ago

https://github.com/veandco/go-sdl2

A Go wrapper for the SDL2 (Simple DirectMedia Layer) C library, providing bindings to create cross-platform multimedia applications and games. It includes core bindings (sdl) and optional extension bindings for images (img), audio (mix), fonts (ttf), and graphics (gfx).

Tokens
3.5K
Snippets
8
Records
18
Agent score
81%

What's inside go-sdl2

  1. Install go-sdl2 bindings

    master

    Install the core SDL2 bindings and optional extension bindings using go get. If you are using the Go Module system and need the latest features, refer to the master branch.

    Core bindings:

    • github.com/veandco/go-sdl2/sdl

    Optional extension bindings:

    • github.com/veandco/go-sdl2/img (SDL2_image)
    • github.com/veandco/go-sdl2/mix (SDL2_mixer)
    • github.com/veandco/go-sdl2/ttf (SDL2_ttf)
    • github.com/veandco/go-sdl2/gfx (SDL2_gfx)
    go get -v github.com/veandco/go-sdl2/{sdl,img,mix,ttf}
  2. Build statically with go-sdl2

    master

    Since v0.3.0, you can build statically against included libraries in .go-sdl2-libs. This is useful for creating portable binaries.

    Linux Static Build: CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"

    Windows Static Build (from Linux/macOS): CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -tags static -ldflags "-s -w"

    Note: To hide the Command Prompt window on Windows, append -H windowsgui to the -ldflags value.

    CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"
  3. Add new SDL2 bindings with fail-safe measures

    master

    To maintain compatibility with older versions of SDL2, go-sdl2 uses CGO with #if guards to provide dummy implementations for functions introduced in newer SDL2 versions. This prevents compilation errors when building against an older library.

    Implementation Pattern

    1. Use the SDL_VERSION_ATLEAST(major, minor, patch) macro to check the installed version.
    2. Use #if defined(WARN_OUTDATED) to provide a compile-time warning (enabled via CGO_CPPFLAGS=-DWARN_OUTDATED).
    3. Define a static dummy function in the C block that does nothing.

    Example for SDL_LockSensors (introduced in 2.0.14):

    package sdl
    
    /*
    #include "sdl_wrapper.h"
    
    #if !(SDL_VERSION_ATLEAST(2,0,14))
    
    #if defined(WARN_OUTDATED)
    #pragma message("SDL_LockSensors is not supported before SDL 2.0.14")
    #endif
    
    static void SDL_LockSensors(void)
    {
        // do nothing
    }
    
    #endif
    */
    import "C"
  4. Install SDL2 system requirements

    master

    The go-sdl2 package is a wrapper around the C library, so the original SDL2 development libraries must be installed on your system.

    OSCommand
    Ubuntu 22.04+apt install libsdl2{,-image,-mixer,-ttf,-gfx}-dev
    Fedora 36+dnf install SDL2{,_image,_mixer,_ttf,_gfx}-devel
    Arch Linuxpacman -S sdl2{,_image,_mixer,_ttf,_gfx}
    Gentooemerge -av libsdl2 sdl2-{image,mixer,ttf,gfx}
    macOSbrew install sdl2{,_image,_mixer,_ttf,_gfx} pkg-config
  5. Install go-sdl2 on macOS

    master

    To set up go-sdl2 on macOS, you must install the Go toolchain, pkg-config, and the SDL2 C libraries. It is recommended to build SDL2 and its related packages from source and install them to a local directory (e.g., $HOME/.local) to avoid cluttering system directories.

    1. Install Go

    Download the Go toolchain for your architecture (Intel or M1) and extract it. Add the following to your $HOME/.zshrc:

    export GOROOT="$HOME/Downloads/CLI/go"
    export GOPATH="$HOME/.go"
    export PATH="$GOPATH/bin:$GOROOT/bin:$PATH"

    Note: On macOS, you may need to manually allow the Go linker tool to execute by double-clicking the link binary in $GOROOT/pkg/tool/darwin_amd64 (or darwin_arm64) via Finder and allowing it in System Preferences -> Security and Privacy.

    2. Install pkg-config

    Install via Homebrew and update your path in $HOME/.zshrc:

    brew install pkgconfig
    export PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH"

    Download the source for SDL2 and required extensions (e.g., SDL2_mixer, SDL2_image, SDL2_ttf, SDL2_gfx). Ensure dependencies like mpg123 (for mixer) and freetype (for ttf) are built first.

    Set these environment variables in $HOME/.zshrc so the build process can find your local installations:

    export LDFLAGS="-L$HOME/.local/lib $LDFLAGS"
    export C_INCLUDE_PATH="$HOME/.local/include:$C_INCLUDE_PATH"

    For each package, use the following build template:

    tar xf [PACKAGE].[EXTENSION]
    mkdir -p [PACKAGE]/build
    cd [PACKAGE]/build
    ../configure --prefix=$HOME/.local
    make
    make install
    cd -

    4. Clone and Test

    Clone the repository with submodules included:

    git clone --recursive git@github.com:veandco/go-sdl2
    cd go-sdl2
    cd .go-sdl2-examples/examples/render
    go run render.go
  6. Cross-compile Linux to Windows

    master

    To cross-compile from Linux to Windows, you need the MinGW toolchain and the SDL2 development package for MinGW.

    1. Install MinGW: On Arch Linux, use pacman -S mingw-w64.
    2. Setup SDL2 for MinGW: Download the SDL2 development package for MinGW. Extract it and copy the x86_64-w64-mingw32 folder recursively into your system's MinGW x86_64-w64-mingw32 folder (e.g., cp -r x86_64-w64-mingw32 /usr on Arch).
    3. Build: Run the following command: env CGO_ENABLED="1" CC="/usr/bin/x86_64-w64-mingw32-gcc" GOOS="windows" CGO_LDFLAGS="-lmingw32 -lSDL2" CGO_CFLAGS="-D_REENTRANT" go build -x main.go
    4. Deployment: Place SDL2.dll from the SDL2 runtime package in the same folder as your generated .exe.
    env CGO_ENABLED="1" CC="/usr/bin/x86_64-w64-mingw32-gcc" GOOS="windows" CGO_LDFLAGS="-lmingw32 -lSDL2" CGO_CFLAGS="-D_REENTRANT" go build -x main.go
  7. Install SDL2 on Windows

    master

    To use go-sdl2 on Windows, follow these steps:

    1. Install MinGW-w64: Download from Mingw-builds. Extract the content (e.g., mingw64) to C:\.
    2. Install SDL2 Development Files: Download SDL2-devel-[version]-mingw.zip from SDL Releases. Extract it and copy the i686-w64-mingw32 and/or x86_64-w64-mingw32 folders into your C:\mingw64 directory.
    3. Update PATH: Add the MinGW binaries location (e.g., C:\mingw64\bin) to your system Path environment variable. Restart your terminal.
    4. Runtime DLLs: Download the SDL2 runtime libraries from SDL Releases. Extract and copy the .dll file into your project directory alongside your executable.
  8. Get Started with go-sdl2

    master

    To use go-sdl2 in a new project, initialize a Go module and import the sdl package. You must run go mod tidy to fetch the dependencies before building.

    // main.go
    package main
    
    import (
    	"github.com/veandco/go-sdl2/sdl"
    )
    
    func main() {
    	sdl.Init(sdl.INIT_EVERYTHING)
    }
    go mod init app
    go mod tidy
    go build
    ./app
  9. Resolve SDL_mixer MP3 playback issues on other OSs

    master
    On non-macOS platforms, you must compile smpeg and SDL_mixer from source with the MP3 option enabled. The smpeg source can be found in the external directory of the SDL_mixer repository.
  10. Breaking changes in sdl package (v0.3 to master)

    master

    Recent updates to the sdl package have introduced several breaking changes, primarily involving the renaming of RWops methods and changes to Window.VulkanCreateSurface return types.

    RWops Method Renames

    Most RWops methods have been renamed to remove the RW prefix:

    • RWops.FreeRW() $\rightarrow$ RWops.Free()
    • RWops.RWsize() $\rightarrow$ RWops.Size()
    • RWops.RWseek() $\rightarrow$ RWops.Seek()
    • RWops.RWread() $\rightarrow$ RWops.Read() and RWops.Read2()
    • RWops.RWtell() $\rightarrow$ RWops.Tell()
    • RWops.RWwrite() $\rightarrow$ RWops.Write() and RWops.Write2()
    • RWops.RWclose() $\rightarrow$ RWops.Close()

    Other sdl Changes

    • RWFromMem() has changed.
    • Window.VulkanCreateSurface now returns unsafe.Pointer instead of uintptr.