Skip to content

krux02/go-sdl2

 
 

Repository files navigation

SDL2 binding for Go

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, install SDL2 via Msys2 like so: pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2{,_mixer,_image,_ttf}

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 crash randomly?
Putting runtime.LockOSThread() at the start of your main() usually solves the problem. We will reimplement parts of go-sdl2 in different way so this doesn't happen in the future.

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?
Not yet

Will there be Go port of SDL2?
There's some work on it but no plan to open-source it yet simply because it's a mess right now :)

How do I contribute?
Generally 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.

Contributors

if anyone is missing, let me know!

License

Go-SDL2 is BSD 3-clause licensed.

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.8%
  • Other 1.2%