A Simple Golang SDL example

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

1
2
3
4
5
6
7
8
9
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:

1
go run main.go

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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();
  }
}

3 Comments

  1. Zach Aysan says:

    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

  2. Madis says:

    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.

  3. Eric says:

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

Leave a Reply