Mobile platform support
masterAndroid
Support for Android is available via examples in the go-sdl2-examples repository: https://github.com/veandco/go-sdl2-examples/tree/master/examples/android.
iOS
There is currently no support for iOS.
repository·master·Indexed 25 days ago
https://github.com/veandco/go-sdl2A 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).
Support for Android is available via examples in the go-sdl2-examples repository: https://github.com/veandco/go-sdl2-examples/tree/master/examples/android.
There is currently no support for iOS.
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/sdlOptional 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}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"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.
SDL_VERSION_ATLEAST(major, minor, patch) macro to check the installed version.#if defined(WARN_OUTDATED) to provide a compile-time warning (enabled via CGO_CPPFLAGS=-DWARN_OUTDATED).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"The go-sdl2 package is a wrapper around the C library, so the original SDL2 development libraries must be installed on your system.
| OS | Command |
|---|---|
| Ubuntu 22.04+ | apt install libsdl2{,-image,-mixer,-ttf,-gfx}-dev |
| Fedora 36+ | dnf install SDL2{,_image,_mixer,_ttf,_gfx}-devel |
| Arch Linux | pacman -S sdl2{,_image,_mixer,_ttf,_gfx} |
| Gentoo | emerge -av libsdl2 sdl2-{image,mixer,ttf,gfx} |
| macOS | brew install sdl2{,_image,_mixer,_ttf,_gfx} pkg-config |
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.
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.
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 -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.goTo cross-compile from Linux to Windows, you need the MinGW toolchain and the SDL2 development package for MinGW.
pacman -S mingw-w64.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).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.goSDL2.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.goTo use go-sdl2 on Windows, follow these steps:
mingw64) to C:\.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.C:\mingw64\bin) to your system Path environment variable. Restart your terminal..dll file into your project directory alongside your executable.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
./appSDL2.dll and its dependencies) are located in the same directory as your compiled executable.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.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.
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()RWFromMem() has changed.Window.VulkanCreateSurface now returns unsafe.Pointer instead of uintptr.