v0.0.0-...-5f3c121 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2020 License: Apache-2.0 Imports: 12 Imported by: 0



Go package for simple 2D video games.

See doc.go for details.



nimble is a Go package for simple 2D video games.

It's a Go incarnation of a lightweight graphics interface that I use for video games. The games typically involve unusual rendering not handled by hardware accelerators. Hence the interface focuses on providing simple efficient access to bitmaps in memory.

The other purpose of the package is be a thin wrapper that can be wrapped around a variety of host platforms. The initial port is against go-sdl2 (

When used for production, it should be built with `-tags=release`.



View Source
const (
	Black = Pixel(0xFF000000)
	White = Pixel(0xFFFFFFFF)

Predefined pixel constants.

View Source
const (
	KeyBackspace = Key(8)
	KeyTab       = Key(9)
	KeyReturn    = Key(0xD)
	KeyEscape    = Key(0x1B)
	KeyDelete    = Key(0x7F)
	KeyLeft      = Key(0x11 + iota) // Borrow "device control" codes

Key values for non-printing keys.

View Source
const (
	MouseMove = MouseEvent(iota) // Mouse moved with button up
	MouseDown                    // Button pressed
	MouseUp                      // Button released
	MouseDrag                    // Mouse moved with button down

Kinds of mouse events.

View Source
const SampleRate = 44100.0

Nominal sample rate for waveforms.


This section is empty.


func AddKeyObserver

func AddKeyObserver(k KeyObserver)

AddKeyObserver causes KeyObserver k to be notified of key events.

func AddMouseObserver

func AddMouseObserver(m MouseObserver)

AddMouseObserver causes m to be notified of mouse events.

func AddRenderClient

func AddRenderClient(r renderClient)

func CreateRecordFile

func CreateRecordFile(filename string) (file *os.File, err error)

func MouseState

func MouseState() (x, y int32, isDown bool)

Get state of mouse.

func Now

func Now() float64

Get time in seconds. Time zero is platform specific.

func OpenRecordFile

func OpenRecordFile(filename string) (*os.File, error)

func PlaySound

func PlaySound(waveform []float32, relativeAmplitude, relativePitch float32)

Play a sound using the given waveform samples.

func Quit

func Quit()

Quit causes Run() to return after processing any pending events.

func Run

func Run(win WindowSpec) int

win==nil requests a full-screen window

func ShowCursor

func ShowCursor(show bool)

ShowCursor causes the cursor to be shown or hidden.


type Font

type Font ttf.Font

A Font

func OpenFont

func OpenFont(filename string, size int) (*Font, error)

OpenFont opens creates a font object and returns a pointer to it.

func (Font) Close

func (f Font) Close()

Close frees resources used by the given Font object.

func (*Font) Height

func (f *Font) Height() int32

Height returns the nominal height of the font.

func (*Font) Size

func (f *Font) Size(text string) (width, height int32)

Size returns the width and height of a bounding box for the text.

type Key

type Key uint8

A Key represents a key on the keyoard. Printable ASCII values represent themselves.

type KeyObserver

type KeyObserver interface {

A KeyObserver is notified of key events.

type MouseEvent

type MouseEvent uint8

type MouseObserver

type MouseObserver interface {
	ObserveMouse(event MouseEvent, x, y int32)

A MouseObserver observes mousse events.

type PixMap

type PixMap struct {
	// contains filtered or unexported fields

A PixMap is a reference to a 2D array or subarray of pixels.

func MakePixMap

func MakePixMap(width, height int32, pixels []Pixel, vstride int32) (pm PixMap)

MakePixMap makes a PixMap referring to pixels in the given slice. The following identities describe the mapping:

pixels[0] maps to (0,0).
pixels[1] maps to (1,0).
pixels[vstride] to (0,1).

func ReadPixMap

func ReadPixMap(filename string) (pm PixMap, err error)

ReadPixMap reads a PixMap from a file with the given name.

func (*PixMap) Contains

func (pm *PixMap) Contains(x, y int32) bool

Contains returns true iff the PixMap contains pointer (x,y).

func (*PixMap) Copy

func (dst *PixMap) Copy(x0, y0 int32, src *PixMap)

Copy copies PixMap src to dst, mapping (0,0) of src onto (x0,y0) of dst.

func (*PixMap) DrawRect

func (pm *PixMap) DrawRect(r Rect, color Pixel)

DrawRect draws a rectangle with the given color.

func (*PixMap) DrawText

func (pm *PixMap) DrawText(x, y int32, text string, color Pixel, font *Font) (width, height int32)

DrawText draws the given text at (x,y) on pm, in the given color and font. The return value indicates the width and height of a bounding box for the text.

func (*PixMap) Empty

func (pm *PixMap) Empty() bool

Empty is true if the PixMap has zero pixels.

func (*PixMap) Fill

func (pm *PixMap) Fill(color Pixel)

Fill fills the PixMap with the given color.

func (*PixMap) Height

func (pm *PixMap) Height() int32

Height returns the height of the PixMap.

func (*PixMap) Intersect

func (pm *PixMap) Intersect(r Rect) (result PixMap)

Intersect returns a PixMap referencing the pixels in the intersection of a PixMap and a Rect.

func (*PixMap) Pixel

func (pm *PixMap) Pixel(x int32, y int32) Pixel

Pixel returns value of pixel at (x,y)

func (*PixMap) Row

func (pm *PixMap) Row(y int32) []Pixel

Row returns a slice referring to the pixels with the given y coordinate. For example:


refers to the same pixel as:


func (*PixMap) SetPixel

func (pm *PixMap) SetPixel(x, y int32, color Pixel)

Set pixel at (x,y) to given color.

func (*PixMap) Size

func (pm *PixMap) Size() (w, h int32)

Size returns the width and height of the PixMap

func (*PixMap) Width

func (pm *PixMap) Width() int32

Width returns the width of the PixMap.

type Pixel

type Pixel uint32

A pixel value. Currently the format is ARGB, but this might change in the future. Use the Pixel constructing functions RGB and Gray to construct pixels.

func Gray

func Gray(frac float32) Pixel

Gray constructs a Pixel with equal red, green, and blue components. frac should be in the interval [0,1]

func PixelSliceFromByteSlice

func PixelSliceFromByteSlice(data []byte, length int32) (pixels []Pixel)

Creates a slice of Pixel from a raw pointer

func RGB

func RGB(red float32, green float32, blue float32) Pixel

RGB constructs a Pixel from its red, green, and blue components. Each component should be in the interval [0,1]

type Rect

type Rect struct {
	Left, Top, Right, Bottom int32

A rectangle or bounding box. The bounds form the product of half-open intervals [Left,Right) x [Top,Bottom)

func MakeRect

func MakeRect(x, y, width, height int32) (r Rect)

MakeRect makes a Rect given upper left corner (x,y) and its width and height

func (*Rect) Contains

func (r *Rect) Contains(x, y int32) bool

Contains returns truee iff the Rect contains (x,y)

func (*Rect) Empty

func (r *Rect) Empty() bool

Empty returns true iff rectangle is empty

func (*Rect) Height

func (r *Rect) Height() int32

Height returns the height the Rect.

func (*Rect) RelativeToLeftTop

func (r *Rect) RelativeToLeftTop(x, y int32) (xLocal, yLocal int32)

func (*Rect) Size

func (r *Rect) Size() (w, h int32)

Size returns the width and height of the Rect.

func (*Rect) Width

func (r *Rect) Width() int32

Width returns the width the Rect.

type WindowSpec

type WindowSpec interface {
	Size() (width, height int32)
	Title() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL