go-sdl2

module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2017 License: BSD-3-Clause

README

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} pkg-config

On Windows,

  1. Install mingw-w64 from Mingw-builds
    - Version: latest (at time of writing 6.3.0)
    - Architecture: x86_64
    - Threads: win32
    - Exception: seh
    - Build revision: 1
    - Destination Folder: Select a folder that your Windows user owns
  2. Install SDL2 http://libsdl.org/download-2.0.php
    - Extract the SDL2 folder from the archive using a tool like 7zip
    - Inside the folder, copy the i686-w64-mingw32 and/or x86_64-w64-mingw32 depending on the architecture you chose into your mingw-w64 folder e.g. C:\Program Files\mingw-w64\x86_64-6.3.0-win32-seh-rt_v5-rev1\mingw64
  3. Setup Path environment variable
    - Put your mingw-w64 binaries location into your system Path environment variable. e.g. C:\Program Files\mingw-w64\x86_64-6.3.0-win32-seh-rt_v5-rev1\mingw64\bin and C:\Program Files\mingw-w64\x86_64-6.3.0-win32-seh-rt_v5-rev1\mingw64\x86_64-w64-mingw32\bin
  4. Open up a terminal such as Git Bash and run go get -v github.com/veandco/go-sdl2/sdl. To prove that it's working correctly, you can change directory by running cd go/src/github.com/veandco/go-sdl2/examples/events and run go run events.go. A window should pop up and you can see event logs printed when moving your mouse over it or typing on your keyboard.
  5. (Optional) You can repeat Step 2 for SDL_image, SDL_mixer, SDL_ttf
    - NOTE: pre-build the libraries for faster compilation by running go install github.com/veandco/go-sdl2/{sdl,img,mix,ttf}

or you can 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/mix
go get -v github.com/veandco/go-sdl2/img
go get -v github.com/veandco/go-sdl2/ttf

or type this if you use Bash terminal:
go get -v github.com/veandco/go-sdl2/{sdl,mix,img,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() {
	if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()

	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(2500)
}

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.

On Mac OS X, this is easy to correct. First remove the faulty mixer: brew remove sdl2_mixer, then reinstall it with the MP3 option: brew install sdl2_mixer --with-flac --with-fluid-synth --with-libmikmod --with-libmodplug --with-smpeg2. If necessary, check which options you can enable with brew info sdl2_mixer.

On Other Operating Systems, 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.

License

Go-SDL2 is BSD 3-clause licensed.

Directories

Path Synopsis
examples
events
This program creates an SDL window and renderer and pushes UserEvents using PeepEvents This program creates an SDL window and renderer and takes in events using sdl.PeepEvents This program creates an SDL window and renderer and pushes events with PushEvent to be read by PollEvents This program creates an SDL window and renderer and takes in events using sdl.WaitEvent This program creates an SDL window and renderer and takes in events using sdl.WaitEventTimeout If there is a timeout (1000 ms), the program prints that there was and tries again
This program creates an SDL window and renderer and pushes UserEvents using PeepEvents This program creates an SDL window and renderer and takes in events using sdl.PeepEvents This program creates an SDL window and renderer and pushes events with PushEvent to be read by PollEvents This program creates an SDL window and renderer and takes in events using sdl.WaitEvent This program creates an SDL window and renderer and takes in events using sdl.WaitEventTimeout If there is a timeout (1000 ms), the program prints that there was and tries again
gfx

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL