/go-sdl2

SDL2 binding for Go

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

SDL2 binding for Go Build Status

go-sdl2 is SDL2 wrapped for Go users. It enables interoperability between Go and the SDL2 library which is written in C. That means the original SDL2 installation is required for this to work.

Requirements

Below is some commands that can be used to install the required packages in some Linux distributions. Some older versions of the distributions such as Ubuntu 13.10 may also be used but it may miss an optional package such as libsdl2-ttf-dev on Ubuntu 13.10's case which is available in Ubuntu 14.04.

On Ubuntu 14.04 and above, type:
apt-get install libsdl2{,-mixer,-image,-ttf}-dev
Note: Ubuntu 14.04 currently has broken header file in the SDL2 package that disables people from compiling against it. It will be needed to either patch the header file or install SDL2 from source.

On Fedora 20 and above, type:
yum install SDL2{,_mixer,_image,_ttf}-devel

On Arch Linux, type:
pacman -S sdl2{,_mixer,_image,_ttf}

On Mac OS X, install SDL2 via Homebrew like so: brew install sdl2{,_image,_ttf,_mixer}

On Windows, you can either install SDL2 via Msys2 like so: pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2{,_mixer,_image,_ttf}

or use MinGW and download pre-compiled development libraries from http://libsdl.org/download-2.0.php. In this case, you need to put files inside i686-w64-mingw32 or x86_64-w64-mingw32 into MinGW's mingw32 directory e.g. C:\MinGW\mingw32. Assuming you have setup MinGW correctly (install base system so it has gcc and friends, set PATH environment variables), you should be able to build go-sdl2. If you have 64-bit system, you can still build by modifying certain environment flags before building such as CGO_ENABLED=1 GOARCH=386 go build.

Installation

To get the bindings, type:
go get -v github.com/veandco/go-sdl2/sdl
go get -v github.com/veandco/go-sdl2/sdl_mixer
go get -v github.com/veandco/go-sdl2/sdl_image
go get -v github.com/veandco/go-sdl2/sdl_ttf

or type this if you use Bash terminal:
go get -v github.com/veandco/go-sdl2/sdl{,_mixer,_image,_ttf}

Note: If you didn't use the previous commands or use 'go install', you will experience long compilation time because Go doesn't keep the built binaries unless you install them.

Example

package main

import "github.com/veandco/go-sdl2/sdl"

func main() {
	sdl.Init(sdl.INIT_EVERYTHING)

	window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		800, 600, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()

	surface, err := window.GetSurface()
	if err != nil {
		panic(err)
	}

	rect := sdl.Rect{0, 0, 200, 200}
	surface.FillRect(&rect, 0xffff0000)
	window.UpdateSurface()

	sdl.Delay(1000)
	sdl.Quit()
}

For more complete examples, see inside the examples folder. Run any of the .go files with go run.

FAQ

Why does my program exits with code 3221225781 on Windows?
You need to put the SDL2.dll in the same folder as your program.

Why does my program crash randomly or hang?
Putting runtime.LockOSThread() at the start of your main() usually solves the problem (see SDL2 FAQ about multi-threading).

UPDATE: Recent update added a call queue system where you can put thread-sensitive code and have it called synchronously on the same OS thread. See the render_queue or render_goroutines examples to see how it works.

Why can't SDL_mixer seem to play MP3 audio file?
Your installed SDL_mixer probably doesn't support MP3 file. You will need to compile smpeg and SDL_mixer from source with the MP3 option enabled. You can find smpeg in the external directory of SDL_mixer. Refer to issue #148 for instructions.

Does go-sdl2 support compiling on mobile platforms like Android and iOS?
For Android, see https://github.com/gen2brain/go-sdl2-android-example.

There is currently no support for iOS yet.

How do I contribute?
You can contribute by a lot of ways from improving README, fixing typos, coding style, specific bugs, performance optimizations. However, it is preferred that you break up your commits to single logical change using git add -p so it is easier to review the patch. The larger the change, the more necessary it is for the commit to be broken up to tiny little pieces. If your change is large but consistent throughout (e.g. fixing a specific coding style that happens on almost every file), that can be counted as single logical change.

You can generally start by forking the repository, and then sending pull requests. But unfortunately this is a Go project, and the absolute import statements make forking a bit more complicated. Here are some instructions, how you can work with that. Generally pull requests are very welcome.

Last but not least, we're starting to use commit messages that looks like this: sdl: fixed some typos in render.go or examples: render_goroutine: fixed a dereferenced nil pointer where it starts with folder hierarchy. It's not something strictly required but we would prefer it to be followed.

Will there be Go port of SDL2?
Due to the way Go is going with C interopability, this might not be possible or even make sense. Another way that we're currently thinking is to create a Rust port instead as it has nicer compatibility with C.

License

Go-SDL2 is BSD 3-clause licensed.

Contributors

Here's the list of contributors with their respective Github URLs.

if anyone is missing, let me know!.. or you can add yourself in :)