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

2 thoughts on “A Simple Golang SDL example

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>