Blog 2020/4/14
<- previous | index | next ->
Install the SDL2 libraries:
$ brew install sdl2{,_image,_mixer,_ttf,_gfx} pkg-config
Install the SDL2 Golang bindings:
$ go get -v github.com/veandco/go-sdl2/{sdl,img,mix,ttf}
Check that SDL2 and Golang are working together by trying this demo:
sdl2-demo.go
:
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
}
}
}
}
$ go build sdl2-demo.go
$ ./sdl2-demo
You should see a window with a black background with a red square in it:
Install the Cairo libraries:
$ brew install cairo
Install the Cairo Golang bindings:
$ go get github.com/ungerik/go-cairo
Build and run a slightly modified version of this demo:
cairo-sdl2-demo.go
:
package main
import (
"github.com/ungerik/go-cairo"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
sdl.Init(sdl.INIT_EVERYTHING)
defer sdl.Quit()
window, _ := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED, 800, 600,
sdl.WINDOW_SHOWN)
sdlSurface, _ := window.GetSurface()
sdlSurface.FillRect(nil, 0)
cairoSurface := cairo.NewSurfaceFromData(sdlSurface.Data(),
cairo.FORMAT_ARGB32, int(sdlSurface.W), int(sdlSurface.H),
int(sdlSurface.Pitch))
cairoSurface.SelectFontFace("serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
cairoSurface.SetFontSize(32.0)
cairoSurface.SetSourceRGB(0.0, 0.0, 1.0)
cairoSurface.MoveTo(10.0, 50.0)
cairoSurface.ShowText("Hello World")
cairoSurface.Finish()
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
}
}
}
}
$ go build cairo-sdl2-demo.go
$ ./cairo-sdl2-demo
You should see a window with a black background with the words "Hello World" in blue:
I was able to get this working under Windows 7 via MSYS2.
$ pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-pkg-config git
$ pacman -S mingw-w64-x86_64-go
$ pacman -S mingw-w64-x86_64-SDL2{,_image,_mixer,_ttf,_gfx}
$ go get -v github.com/veandco/go-sdl2/{sdl,img,mix,ttf,gfx}
$ pacman -S mingw-w64-x86_64-cairo
$ go get -v github.com/ungerik/go-cairo
and for 32-bit Windows 7:
$ pacman -S mingw-w64-i686-gcc mingw-w64-x86_64-pkg-config git
$ pacman -S mingw-w64-i686-go
$ pacman -S mingw-w64-i686-SDL2{,_image,_mixer,_ttf,_gfx}
$ go get -v github.com/veandco/go-sdl2/{sdl,img,mix,ttf,gfx}
$ pacman -S mingw-w64-i686-cairo
$ go get -v github.com/ungerik/go-cairo
note that for some reason, I had to export GOROOT=/mingw32/lib/go
to get my 32-bit golang setup working.