A Simple Golang SDL example

First install golang, SDL and the golang SDL package. The following is for Ubuntu 12.04:

sudo apt-get install golang-go

Install SDL libraries:

sudo apt-get install libsdl1.2-dev
sudo apt-get install libsdl-mixer*
sudo apt-get install libsdl-image*
sudo apt-get install libsdl-ttf*
sudo go get -v github.com/0xe2-0x9a-0x9b/Go-SDL/...

Then create the following go program. This write random pixel data to the screen directly. If you save it to main.go you can execute it with:

go run main.go

Here’s the code (please note there’s probably a lot wrong with it, it’s my first go program):

package main

import (
"github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
"log"
"unsafe"
"math/rand"
)

func draw_point(x int32,y int32,value uint32,screen* sdl.Surface) {
  var pix = uintptr(screen.Pixels);
  pix += (uintptr)((y*screen.W)+x)*unsafe.Sizeof(value);
  var pu = unsafe.Pointer(pix);
  var pp *uint32;
  pp = (*uint32)(pu);
  *pp = value;
}

func main() {

  var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)

  if screen == nil {
    log.Fatal(sdl.GetError())
  }

  var n int32;
  for n=0;n<1000000;n++ {

    var y int32 =rand.Int31()%480;
    var x int32 =rand.Int31()%640;
    var value uint32 = rand.Uint32();
    draw_point(x,y,value,screen);

    screen.Flip();
  }
}
  • Zach Aysan

    Your URL to Go-SDL link no longer works, that user has intentionally disappeared from the internet. This might be a replacement URL:

    https://github.com/banthar/Go-SDL

  • Madis

    There http://go-lang.cat-v.org/library-bindings is claim that 0xe2-0x9a-0x9b’s version was fork of Banthar’s and much more worked on. Banthar’s version looks like quite abandoned, mostly changed years ago. Some changes half year ago also.

  • Eric

    I found a fork of the improved version that is still maintained: https://github.com/neagix/Go-SDL