Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active April 19, 2020 23:33
Show Gist options
  • Save cellularmitosis/0c4f109be15845e9151292c5668ef26c to your computer and use it in GitHub Desktop.
Save cellularmitosis/0c4f109be15845e9151292c5668ef26c to your computer and use it in GitHub Desktop.
Getting started with Cairo in SDL2 in Golang on OSX

Blog 2020/4/14

<- previous | index | next ->

Getting started with Cairo in SDL2 in Golang on OSX

SDL2

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:

Screen Shot 2020-04-14 at 3 26 36 PM

Cairo

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:

Screen Shot 2020-04-14 at 3 25 25 PM

Windows

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment