All Projects → veandco → Go Sdl2

veandco / Go Sdl2

Licence: bsd-3-clause
SDL2 binding for Go

Programming Languages

c
50402 projects - #5 most used programming language
go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Sdl2

Soldat
Soldat is a unique 2D (side-view) multiplayer action game
Stars: ✭ 1,199 (-28.07%)
Mutual labels:  sdl2
Engge
Open source remake of Thimbleweed Park's engine
Stars: ✭ 94 (-94.36%)
Mutual labels:  sdl2
Samples
Community driven repository for Dapr samples
Stars: ✭ 104 (-93.76%)
Mutual labels:  binding
Expostal
Elixir binding for Libpostal - a library for parsing/normalizing street addresses around the world. Powered by statistical NLP and open geo data.
Stars: ✭ 80 (-95.2%)
Mutual labels:  binding
Sled
Satanic/Sexy/Stupid/Silly/Shiny LED matrix controller
Stars: ✭ 88 (-94.72%)
Mutual labels:  sdl2
Openrct2
An open source re-implementation of RollerCoaster Tycoon 2 🎢
Stars: ✭ 10,115 (+506.78%)
Mutual labels:  sdl2
Kys Cpp
《金庸群侠传》c++复刻版,已完工
Stars: ✭ 1,182 (-29.09%)
Mutual labels:  sdl2
Rawgl
Another World/Out of This World engine reimplementation (SDL, OpenGL)
Stars: ✭ 111 (-93.34%)
Mutual labels:  sdl2
Query State
Application state in query string
Stars: ✭ 88 (-94.72%)
Mutual labels:  binding
Sdl2 nim
Wrapper of the SDL 2 library for the Nim language.
Stars: ✭ 108 (-93.52%)
Mutual labels:  sdl2
Raylib Lua
A simple and easy-to-use Lua library to enjoy videogames programming
Stars: ✭ 80 (-95.2%)
Mutual labels:  binding
Etherterm
EtherTerm (SDL2) Telnet/SSH Terminal
Stars: ✭ 86 (-94.84%)
Mutual labels:  sdl2
Scrcpy
Display and control your Android device
Stars: ✭ 58,880 (+3432.09%)
Mutual labels:  sdl2
Doomfire
DOOM fire implementation written in rust
Stars: ✭ 80 (-95.2%)
Mutual labels:  sdl2
Vkbind
Single file Vulkan API loader.
Stars: ✭ 110 (-93.4%)
Mutual labels:  binding
Dungeonrush
👾🐍 A opensource game inspired by Snake, written in pure C with SDL
Stars: ✭ 1,192 (-28.49%)
Mutual labels:  sdl2
Nginx Haskell Module
Nginx module for binding Haskell code in configuration files for great good!
Stars: ✭ 99 (-94.06%)
Mutual labels:  binding
Xray 16
Improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World. Join OpenXRay! ;)
Stars: ✭ 1,806 (+8.34%)
Mutual labels:  sdl2
Prototracker
Prototracker
Stars: ✭ 110 (-93.4%)
Mutual labels:  sdl2
Sdl2 Cmake Modules
Modern CMake modules for finding and using the SDL2 library as well as other related libraries: SDL2_image, SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx. (Targets: SDL2::Core, SDL2::Main, SDL2::Image, SDL2::TTF, SDL2::Net, SDL2::Mixer and SDL2::GFX). Mirror of https://gitlab.com/aminosbh/sdl2-cmake-modules
Stars: ✭ 108 (-93.52%)
Mutual labels:  sdl2

SDL2 binding for Go Build Status Go Report Card Reviewed by Hound Financial Contributors on Open Collective

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.

Table of Contents

Documentation

Examples

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)
	}
	surface.FillRect(nil, 0)

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

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			}
		}
	}
}

For more complete examples, see https://github.com/veandco/go-sdl2-examples. You can run any of the .go files with go run.

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 install libsdl2{,-image,-mixer,-ttf,-gfx}-dev

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

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

On Gentoo, type:
emerge -av libsdl2 sdl2-{image,mixer,ttf,gfx}

On macOS, install SDL2 via Homebrew like so:
brew install sdl2{,_image,_mixer,_ttf,_gfx} 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.
  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{,_image,_mixer,_ttf,_gfx}

Installation

To get the bindings, type:
go get -v github.com/veandco/go-sdl2/sdl
go get -v github.com/veandco/go-sdl2/img
go get -v github.com/veandco/go-sdl2/mix
go get -v github.com/veandco/go-sdl2/ttf
go get -v github.com/veandco/go-sdl2/gfx

or type this if you use Bash terminal:
go get -v github.com/veandco/go-sdl2/{sdl,img,mix,ttf}

Due to go-sdl2 being under active development, a lot of breaking changes are going to happen during v0.x. With versioning system coming to Go soon, we'll make use of semantic versioning to ensure stability in the future.

Static compilation

Since v0.3.0, it is possible to build statically against included libraries in .go-sdl2-libs. To build statically, run:

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

You can also cross-compile to another OS. For example, to Windows:

CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -tags static -ldflags "-s -w"

On Windows, if you would like to hide the Command Prompt window when running the statically-compiled program, you could append -H windowsgui to the -ldflags value.

For the list of OS and architecture, you can see inside the .go-sdl2-libs directory.

NOTE: If you're using the new Go Module system, you will need to refer to the master branch for now by running:

go get -v github.com/veandco/go-sdl2/sdl@master

Before building the program.

Cross-compiling

Linux to Windows

  1. Install MinGW toolchain.
    • On Arch Linux, it's simply pacman -S mingw-w64.
  2. Download the SDL2 development package for MinGW here (and the others like SDL_image, SDL_mixer, etc.. here if you use them).
  3. Extract the SDL2 development package and copy the x86_64-w64-mingw32 folder inside recursively to the system's MinGW x86_64-w64-mingw32 folder. You may also do the same for the i686-w64-mingw32 folder.
    • On Arch Linux, it's cp -r x86_64-w64-mingw32 /usr.
  4. Now you can start cross-compiling your Go program by running 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. You can change some of the parameters if you'd like to. In this example, it should produce a main.exe executable file.
  5. Before running the program, you need to put SDL2.dll from the SDL2 runtime package (For others like SDL_image, SDL_mixer, etc.., look for them here) for Windows in the same folder as your executable.
  6. Now you should be able to run the program using Wine or Windows!

macOS to Windows

  1. Install Homebrew
  2. Install MinGW through Homebrew via brew install mingw-w64
  3. Download the SDL2 development package for MinGW here (and the others like SDL_image, SDL_mixer, etc.. here if you use them).
  4. Extract the SDL2 development package and copy the x86_64-w64-mingw folder inside recursively to the system's MinGW x86_64-w64-mingw32 folder. You may also do the same for the i686-w64-mingw32 folder. The path to MinGW may be slightly different but the command should look something like cp -r x86_64-w64-mingw32 /usr/local/Cellar/mingw-w64/5.0.3/toolchain-x86_64.
  5. Now you can start cross-compiling your Go program by running env CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows CGO_LDFLAGS="-L/usr/local/Cellar/mingw-w64/5.0.3/toolchain-x86_64/x86_64-w64-mingw32/lib -lSDL2" CGO_CFLAGS="-I/usr/local/Cellar/mingw-w64/5.0.3/toolchain-x86_64/x86_64-w64-mingw32/include -D_REENTRANT" go build -x main.go. You can change some of the parameters if you'd like to. In this example, it should produce a main.exe executable file.
  6. Before running the program, you need to put SDL2.dll from the SDL2 runtime package (For others like SDL_image, SDL_mixer, etc.., look for them here) for Windows in the same folder as your executable.
  7. Now you should be able to run the program using Wine or Windows!

Linux to macOS

  1. Install macOS toolchain via osxcross
  2. Run the following build command (replace the values in parentheses):
CGO_ENABLED=1 CC=[path-to-osxcross]/target/bin/[arch]-apple-darwin[version]-clang GOOS=darwin GOARCH=[arch] go build -tags static -ldflags "-s -w" -a

FAQ

Why does the program not run on Windows? Try putting the runtime libraries (e.g. SDL2.dll and friends) 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 from https://github.com/veandco/go-sdl2-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 macOS, 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. You could also try installing sdl2_mixer with mpg123 by running brew install sdl2_mixer --with-mpg123.

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.

Note that there seems to be a problem with SDL_mixer 2.0.2 so you can also try to revert back to 2.0.1 and see if it solves your problem

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

There is currently no support for iOS yet.

Why does my window not immediately render after creation? It appears the rendering subsystem needs some time to be able to present the drawn pixels. This can be workaround by adding delay using sdl.Delay() or put the rendering code inside a draw loop.

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

Go-SDL2 is BSD 3-clause licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].