go13h

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2018 License: MIT Imports: 10 Imported by: 0

README

go-13h

go13h is a wrapper for go-sdl2 that provides simple 2d graphics, as a spiritual call back to DOS mode 13h.

Installation

As go-13h fronts go-sdl2, you will need to install go-sdl2.

I may be putting a tutorial on install for both Linux and Windows```

Once you have go-sdl2 working you can go get bitbucket.org/hsoolien/go-13h.

```Note: I have had issues using go-get to work on linux machines. It seems go wants to want to use mercurial, I've yet to determine a fix short of moving the repo, so git clone git@bitbucket.org:hsoolien/go-13h to the appropritate directory in your go path works```

##Usage

Once everything is installed, it's pretty easy to setup

#!go

package main

import ( "log"

"bitbucket.org/hsoolien/go-13h"
"github.com/veandco/go-sdl2/sdl"

)

func main() { //Create a screen that has a drawing resolution of 200100 and a display resolution of 400200 (effectively doubling the size of the pixels) //Do not full screen, vsync or filter the scaling scr, l, err := go13h.NewScreen(200, 100, 400, 200, "Test", false, false, false) if err != nil { log.Println("Can't start go13h, error:", err) return } //Frees up cgo assets defer scr.QuitScreen()

//Clear layer to Blue
l.Clear(go13h.Blue)
//Of course
go13h.DrawString(l, "Hello World!", 0, 1, 1, go13h.Gray, scr.CS)

//Update screen
scr.Update()

//Use go-sdl to capture quit event
quit := false
for !quit {
	l.Clear(go13h.Black)
	for test := sdl.PollEvent(); test != nil; test = sdl.PollEvent() {
		switch test.(type) {
		case *sdl.QuitEvent:
			quit = true
		}
	}
}

}


Will display a window similar to so:

![test.png](https://bitbucket.org/repo/akeLXAb/images/1220526842-test.png)

If you get  exit status 3221225781 check to see that the SDL2 libraries are installed (on windows, is sdl2.ll in your PATH)

Documentation

Index

Constants

View Source
const (
	//AlphaNone is no alpha blend for DrawPoint
	AlphaNone = iota
	//AlphaAdd is a basic add alpha for DrawPoint (transparency)
	AlphaAdd
	//AlphaMultiply converts both src and dst to 1.0f then multiplies by each
	AlphaMultiply
	//AlphaSub returns the highest value pixel minus the lowest value pixel
	AlphaSub
	//AlphaXor returns the xor of src and dst
	AlphaXor
	//AlphaAnd returns the and of src and dst
	AlphaAnd
	//AlphaOr returns the or of src and dst
	AlphaOr
	//AlphaAndNot returns the or of src and dst
	AlphaAndNot
)
View Source
const (
	//DrawModNone applies no transformation with CopyDrawerMod
	DrawModNone = iota
	//DrawModFlipY applies CopyDrawerMod with a Y flip
	DrawModFlipY
	//DrawModFlipX applies CopyDrawerMod with a X flip
	DrawModFlipX
	//DrawModFlipBoth applies CopyDrawerMod with both axis flipped
	DrawModFlipBoth
)
View Source
const (
	//Depth8Bit is one byte of colour data per pixel, offset by 1 so number of bytes matches bit depth
	Depth8Bit = iota + 1
	//Depth16Bit is two bytes of colour data per pixel
	Depth16Bit
	//Depth24Bit is three bytes of colour data per pixel
	Depth24Bit
	//Depth32Bit is three bytes of colour data per pixel
	Depth32Bit
)

Variables

View Source
var (
	//White color preset
	White color.RGBA
	//Silver color preset
	Silver color.RGBA
	//Gray color preset
	Gray color.RGBA
	//Black color preset
	Black color.RGBA
	//Red color preset
	Red color.RGBA
	//Maroon color preset
	Maroon color.RGBA
	//Orange color preset
	Orange color.RGBA
	//Yellow color preset
	Yellow color.RGBA
	//Olive color preset
	Olive color.RGBA
	//Lime color preset
	Lime color.RGBA
	//Green color preset
	Green color.RGBA
	//Aqua color preset
	Aqua color.RGBA
	//Teal color preset
	Teal color.RGBA
	//Blue color preset
	Blue color.RGBA
	//Navy color preset
	Navy color.RGBA
	//Fuscia color preset
	Fuscia color.RGBA
	//Purple color preset
	Purple color.RGBA
	//NullCol color preset
	//Used for zero value returns
	NullCol color.RGBA
)

Functions

func AlphaAddColor added in v0.0.5

func AlphaAddColor(src, dst, alp uint8) byte

AlphaAddColor blends dst and src using alp as a additive alpha blend

func AlphaAndColor added in v0.0.5

func AlphaAndColor(src, dst uint8) byte

AlphaAndColor returns the and of src and dst

func AlphaAndNotColor added in v0.0.5

func AlphaAndNotColor(src, dst uint8) byte

AlphaAndNotColor returns the AndNot of src and dst

func AlphaChoose added in v0.0.5

func AlphaChoose(src, dst, alp, ab uint8) byte

AlphaChoose returns color based on current AlphaBlend setting

func AlphaMultiplyColor added in v0.0.5

func AlphaMultiplyColor(src, dst uint8) byte

AlphaMultiplyColor converts src and dst to 1.f = 255 float then multiplies them by each other and returns a uint8 clamped value

func AlphaOrColor added in v0.0.5

func AlphaOrColor(src, dst uint8) byte

AlphaOrColor returns the or of src and dst

func AlphaSubColor added in v0.0.5

func AlphaSubColor(src, dst uint8) byte

AlphaSubColor returns the highest of src/dst minus the lowest in a subtractive blend

func AlphaXorColor added in v0.0.5

func AlphaXorColor(src, dst uint8) byte

AlphaXorColor returns the xor of src and dst

func CopyDrawer added in v0.0.4

func CopyDrawer(dd, ds Drawer, x, y, xs, ys, w, h int32)

CopyDrawer copies ds at xs, ys, for w, h; to dd at x, y (for w, h)

func CopyDrawerFlipBoth added in v0.0.5

func CopyDrawerFlipBoth(dd, ds Drawer, x, y, xs, ys, w, h int32)

CopyDrawerFlipBoth copies ds at xs, ys, for w, h; to dd at x + w, y + h (for x, y)

func CopyDrawerFlipX added in v0.0.5

func CopyDrawerFlipX(dd, ds Drawer, x, y, xs, ys, w, h int32)

CopyDrawerFlipX copies ds at xs, ys, for w, h; to dd at x + w, y (for x, h)

func CopyDrawerFlipY added in v0.0.5

func CopyDrawerFlipY(dd, ds Drawer, x, y, xs, ys, w, h int32)

CopyDrawerFlipY copies ds at xs, ys, for w, h; to dd at x, y + h (for w, y)

func CopyDrawerMod added in v0.0.5

func CopyDrawerMod(dd, ds Drawer, x, y, xs, ys, w, h int32, dm uint8)

CopyDrawerMod copies ds at xs, ys, for w, h; to dd at x,y(for w,h) with dm

func DrawChar added in v0.0.4

func DrawChar(dr Drawer, s string, x, y, sc int32, c color.RGBA, cs *CharacterSet) error

DrawChar renders charcater s at x, y scaled sc times in c color to dr using cs

func DrawCharFlip added in v0.0.4

func DrawCharFlip(dr Drawer, s string, x, y, sc int32, c color.RGBA, cs *CharacterSet) error

DrawCharFlip renders charcater s at x, y scaled sc times in c color to dr using cs, upside down

func DrawLine added in v0.0.4

func DrawLine(dr Drawer, x, y, x2, y2 int32, c color.RGBA)

DrawLine draws a line from x, y, to x2, y2 using c color Code derived from examples given here: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

func DrawRectBorder added in v0.0.4

func DrawRectBorder(dr Drawer, x, y, w, h int32, c color.RGBA)

DrawRectBorder makes a w wide and h high rectangle, 1 px borders only at x, y using c color

func DrawRectFill added in v0.0.4

func DrawRectFill(dr Drawer, x, y, w, h int32, c color.RGBA)

DrawRectFill makes a w wide and h high rectangle at x, y using c color

func DrawString added in v0.0.4

func DrawString(dr Drawer, s string, x, y, sc int32, c color.RGBA, cs *CharacterSet) []error

DrawString renders string s at x, y scaled sc times with c color to dr, using cs CharacterSet

func DrawStringFlip added in v0.0.4

func DrawStringFlip(dr Drawer, s string, x, y, sc int32, c color.RGBA, cs *CharacterSet) []error

DrawStringFlip renders string s to x,y with y scaled sc times with y 0 at bottom of screen

func NewImageFromDrawer added in v0.0.4

func NewImageFromDrawer(dr Drawer) draw.Image

NewImageFromDrawer creates a image interface from a drawer interface

func NewScreen

func NewScreen(w, h, rw, rh int32, n string, fs, vs, lf bool) (*Screen, *Layer, error)

NewScreen sets up package and returns a *Screen to manage the global state as well as a first *Layer to draw to. Currently only supports a ARGB8888 colour space w, h are the dimensions of the layer you draw to rw, rh are the real dimensions of the window/screen (will scale screen) n is name of Window fs is fullscreen, vs is vSync and lf is linear filtering for screen scaling When quiting, call QuitScreen to close off all SDL assets.

func RandColor added in v0.0.6

func RandColor(s int) (c color.RGBA)

RandColor returns a color with random r, g, b

Types

type CharacterSet added in v0.0.4

type CharacterSet struct {
	CharHeight, CharWidth int32
	CharSpace,
	CharExclaim,
	CharDblQuote,
	CharHash,
	CharString,
	CharPercent,
	CharAmp,
	CharApostrophe,
	CharOpenBracketCurved,
	CharCloseBracketCurved,
	CharAsterisks,
	CharPlus,
	CharComma,
	CharMinus,
	CharPeriod,
	CharForwardSlash,
	Char0,
	Char1,
	Char2,
	Char3,
	Char4,
	Char5,
	Char6,
	Char7,
	Char8,
	Char9,
	CharColon,
	CharSemiColon,
	CharLessThan,
	CharEqual,
	CharGreaterThan,
	CharQuestion,
	CharAt,
	CharA,
	CharB,
	CharC,
	CharD,
	CharE,
	CharF,
	CharG,
	CharH,
	CharI,
	CharJ,
	CharK,
	CharL,
	CharM,
	CharN,
	CharO,
	CharP,
	CharQ,
	CharR,
	CharS,
	CharT,
	CharU,
	CharV,
	CharW,
	CharX,
	CharY,
	CharZ,
	CharOpenBracketSquare,
	CharBackSlash,
	CharCloseBracketSquare,
	CharCarrot,
	CharUnderScore,
	CharBackTick,
	Chara,
	Charb,
	Charc,
	Chard,
	Chare,
	Charf,
	Charg,
	Charh,
	Chari,
	Charj,
	Chark,
	Charl,
	Charm,
	Charn,
	Charo,
	Charp,
	Charq,
	Charr,
	Chars,
	Chart,
	Charu,
	Charv,
	Charw,
	Charx,
	Chary,
	Charz,
	CharOpenCurleyBrace,
	CharPipe,
	CharCloseCurleyBrace,
	CharTilde,
	CharNoChar string
}

CharacterSet contains characters for drawing text

func (*CharacterSet) InitCharMap added in v0.0.4

func (CS *CharacterSet) InitCharMap()

InitCharMap sets loads a CharacterSet up

type ColorCount added in v0.0.6

type ColorCount struct {
	// contains filtered or unexported fields
}

ColorCount holds a list of colors/quantities that can be sorted

func CountColors added in v0.0.6

func CountColors(dr Drawer) ColorCount

CountColors returns a ColorCount of an image sorted by occurrence

func (ColorCount) Len added in v0.0.6

func (cc ColorCount) Len() int

Len implemented to satisfy sort.Interface

func (ColorCount) Less added in v0.0.6

func (cc ColorCount) Less(i, j int) bool

Less implemented to satisfy sort.Interface

func (ColorCount) Sort added in v0.0.6

func (cc ColorCount) Sort()

Sort is a helper method

func (ColorCount) Swap added in v0.0.6

func (cc ColorCount) Swap(i, j int)

Swap implemented to satisfy sort.Interface

type Drawer added in v0.0.4

type Drawer interface {
	//ReadPoint returns the colour value of location x, y in Drawer memory
	ReadPoint(x, y int32) (color.RGBA, error)
	//DrawPoint sets the colour value of location x, y in Drawer memory
	DrawPoint(x, y int32, c color.RGBA)
	//Clear sets all of Drawer to colour
	Clear(c color.RGBA)
	//GetWidth returns width of Drawer
	GetWidth() int32
	//GetHeight returns Height of Drawer
	GetHeight() int32
	//GetMemory returns the raw memory as a []byte (used in testing, returns []byte{0} for ImageWrap)
	GetMemory() []byte
}

Drawer is the interface for go13h drawing functions.

type ImageWrap added in v0.0.4

type ImageWrap struct {
	M  draw.Image
	A  uint8 //AlphaMode for DrawPoint
	OD bool  //Over draw
}

ImageWrap is used to satisfy the Drawer interface with a draw.Image interface

func WrapImage added in v0.0.5

func WrapImage(m image.Image) (*ImageWrap, error)

WrapImage returns a *ImageWrap for any image.Image that is capable of being cast to draw.Image

func (ImageWrap) Clear added in v0.0.4

func (iw ImageWrap) Clear(c color.RGBA)

Clear clears the Image to c color

func (ImageWrap) DrawPoint added in v0.0.4

func (iw ImageWrap) DrawPoint(x, y int32, c color.RGBA)

DrawPoint sets a single point on the Image to c color

func (ImageWrap) GetHeight added in v0.0.4

func (iw ImageWrap) GetHeight() int32

GetHeight reports Image height

func (ImageWrap) GetMemory added in v0.0.5

func (iw ImageWrap) GetMemory() []byte

GetMemory returns the raw []byte of Memory for now *ImageWrap.GetMemory() returns an empty slice

func (ImageWrap) GetWidth added in v0.0.4

func (iw ImageWrap) GetWidth() int32

GetWidth reports Image height

func (ImageWrap) ReadPoint added in v0.0.4

func (iw ImageWrap) ReadPoint(x, y int32) (color.RGBA, error)

ReadPoint returns the colour value of location x, y in Image

type Img added in v0.0.4

type Img struct {
	Memory []byte //Raw Img data in BGRA layout
	W, H   int32  //Img width and height
	A      uint8  //Alpha mode for DrawPoint
	OD     bool   //Overdraw

}

Img is raw image data with resolution and color context

func NewImgFromImage added in v0.0.4

func NewImgFromImage(m image.Image) *Img

NewImgFromImage returns a *Img from any go image interface

func (*Img) Clear added in v0.0.4

func (im *Img) Clear(c color.RGBA)

Clear clears the memory to color

func (*Img) DrawPoint added in v0.0.4

func (im *Img) DrawPoint(x, y int32, c color.RGBA)

DrawPoint sets a single point on the Img memory to c color

func (*Img) GetHeight added in v0.0.4

func (im *Img) GetHeight() int32

GetHeight reports Img height

func (*Img) GetMemory added in v0.0.5

func (im *Img) GetMemory() []byte

GetMemory returns the raw []byte of Memory

func (*Img) GetWidth added in v0.0.4

func (im *Img) GetWidth() int32

GetWidth reports Img width

func (*Img) ReadPoint added in v0.0.4

func (im *Img) ReadPoint(x, y int32) (color.RGBA, error)

ReadPoint returns the colour value of location x, y in img memory

type Layer

type Layer struct {
	Memory []byte //Raw Layer data in BGRA layout

	W, H int32 //Layer resolution
	A    uint8 //AlphaMode for DrawPoint
	Scr  *Screen

	Hide bool

	OD bool //Over draw
	// contains filtered or unexported fields
}

Layer contains a screen layer of info, a basic screen always has a 0 layer

func (*Layer) Clear

func (l *Layer) Clear(c color.RGBA)

Clear clears the memory to color

func (*Layer) DrawChar

func (l *Layer) DrawChar(s string, x, y, sc int32, c color.RGBA) error

DrawChar renders character s to x, y scaled sc times Replaced by DrawChar as a function that accepts a Drawer interface

func (*Layer) DrawCharFlip added in v0.0.3

func (l *Layer) DrawCharFlip(s string, x, y, sc int32, c color.RGBA) error

DrawCharFlip renders character s to x, y scaled sc times with y 0 at bottom of screen Replaced by DrawCharFlip as a function that accepts a Drawer interface

func (*Layer) DrawCircleBorder

func (l *Layer) DrawCircleBorder(x, y int32, r float64, c color.RGBA)

DrawCircleBorder makes circle of r radius at x, y using c color

func (*Layer) DrawCircleFill

func (l *Layer) DrawCircleFill(x, y int32, r1, r2 float64, c color.RGBA)

DrawCircleFill fills a circle from r1 to r2 at x, y using c color

func (*Layer) DrawLine

func (l *Layer) DrawLine(x, y, x2, y2 int32, c color.RGBA)

DrawLine draws a line from x, y, to x2, y2 using c color Replaced by DrawLine as a function that accepts a Drawer interface

func (*Layer) DrawPoint

func (l *Layer) DrawPoint(x, y int32, c color.RGBA)

DrawPoint sets a single point on the screen memory to current working color If l.OD is true pixels wrap once around NullCol will never draw

func (*Layer) DrawRectBorder

func (l *Layer) DrawRectBorder(x, y, w, h int32, c color.RGBA)

DrawRectBorder makes a w wide and h high rectangle, borders ony2 at x, y using c color Replaced by DrawRectBorder as a function that accepts a Drawer interface

func (*Layer) DrawRectFill

func (l *Layer) DrawRectFill(x, y, w, h int32, c color.RGBA)

DrawRectFill makes a w wide and h high rectangle at x, y using c color Replaced by DrawRectFill as a function that accepts a Drawer interface

func (*Layer) DrawString

func (l *Layer) DrawString(s string, x, y, sc int32, c color.RGBA) []error

DrawString renders string s to x, y scaled sc times Replaced by DrawString as a function that accepts a Drawer interface

func (*Layer) DrawStringFlip added in v0.0.3

func (l *Layer) DrawStringFlip(s string, x, y, sc int32, c color.RGBA) []error

DrawStringFlip renders string s to x,y with y scaled sc times with y 0 at bottom of screen Replaced by DrawStringFlip as a function that accepts a Drawer interface

func (*Layer) FlipX added in v0.0.3

func (l *Layer) FlipX()

FlipX flips layer x axis (0 is now right hand of screen)

func (*Layer) FlipY added in v0.0.3

func (l *Layer) FlipY()

FlipY flips layer y axis (0 is now bottom of screen)

func (*Layer) GetHeight added in v0.0.4

func (l *Layer) GetHeight() int32

GetHeight reports Layer height

func (*Layer) GetMemory added in v0.0.5

func (l *Layer) GetMemory() []byte

GetMemory returns the raw []byte of Memory

func (*Layer) GetWidth added in v0.0.4

func (l *Layer) GetWidth() int32

GetWidth reports Layer height

func (*Layer) Offset

func (l *Layer) Offset(x, y float64)

Offset moves the layer in relation to the 0,0 of the final screen resolution

func (*Layer) ReadPoint

func (l *Layer) ReadPoint(x, y int32) (color.RGBA, error)

ReadPoint returns the colour value of location x, y in layer memory

func (*Layer) Rotate

func (l *Layer) Rotate(r float64, p *sdl.Point)

Rotate rotates layer around point by r

func (*Layer) Scale

func (l *Layer) Scale(sx, sy float64)

Scale scales layer by s

type PImg added in v0.0.5

type PImg struct {
	Memory []byte   //Raw PImg data in a 8 bit or 16 paletted layout
	W, H   int32    //Img width and height
	D      uint8    //Palette mode
	P      *Palette //PImg palette
	OD     bool     //Over draw
}

PImg is raw paletted image data with resolution and color context PImg does not support Alpha draws to itself.

func NewPImg added in v0.0.6

func NewPImg(w, h int32, d uint8, p *Palette) (*PImg, error)

NewPImg returns a initialized PImp If no p is passed it will create an empty p

func NewPImgFromDrawer added in v0.0.6

func NewPImgFromDrawer(dr Drawer, d uint8) (*PImg, error)

NewPImgFromDrawer returns an initialized PImg Derived from any Drawer clamped to d

func (*PImg) Clear added in v0.0.5

func (pim *PImg) Clear(c color.RGBA)

Clear clears the Memory to color A bad Pim.D(epth) setting will result in a memory of []byte{0}

func (*PImg) DrawPoint added in v0.0.5

func (pim *PImg) DrawPoint(x, y int32, c color.RGBA)

DrawPoint sets a single point on the PImg memory to c color or closest c in pim.P A bad Pimg.D(epth) setting will result in no point being drawn If pim.OD is true pixels wrap once around NullCol will never draw

func (*PImg) DrawPointByIndex added in v0.0.5

func (pim *PImg) DrawPointByIndex(x, y int32, ci uint64)

DrawPointByIndex sets a single point of the PImg memory to the ci colour index DrawPointByIndex guards against ci's that overflow the pim.D(epth), but will draw indexes outside of the pim.P(alette) If pim.OD is true pixels wrap once around

func (*PImg) GetHeight added in v0.0.5

func (pim *PImg) GetHeight() int32

GetHeight reports PImg height

func (*PImg) GetMemory added in v0.0.5

func (pim *PImg) GetMemory() []byte

GetMemory returns the raw []byte of Memory

func (*PImg) GetWidth added in v0.0.5

func (pim *PImg) GetWidth() int32

GetWidth reports PImg width

func (*PImg) ReadPoint added in v0.0.5

func (pim *PImg) ReadPoint(x, y int32) (color.RGBA, error)

ReadPoint returns the colour of the palette value of location x, y in PImg memory ReadPoint will return a NullCol on bad points (out of bounds, invalid PImg.P(alette) entry, or bad PImg.D(epth))

type Palette added in v0.0.5

type Palette []color.RGBA

Palette is a slice of color.RGBA

func (*Palette) Add added in v0.0.6

func (p *Palette) Add(c color.RGBA)

Add adds a color.RGBA to the palette

func (*Palette) ChangeByColor added in v0.0.6

func (p *Palette) ChangeByColor(c, nc color.RGBA) error

ChangeByColor changes the first occurrence of c in a palette to nc, if c does not exist will return a error.

func (*Palette) ChangeByIndex added in v0.0.6

func (p *Palette) ChangeByIndex(i int, c color.RGBA) error

ChangeByIndex changes the specified i to c, if i is too large or negative, will return an error

func (*Palette) Clamp added in v0.0.6

func (p *Palette) Clamp(d uint8)

Clamp limits palette to d(epth)

func (Palette) Convert added in v0.0.5

func (p Palette) Convert(c color.RGBA) color.RGBA

Convert returns the palette color closest to c in Euclidean R,G,B space.

func (*Palette) Delete added in v0.0.6

func (p *Palette) Delete(i int)

Delete replaces the color with NullCol unless it's the last color then the index is removed

func (Palette) HasColor added in v0.0.6

func (p Palette) HasColor(c color.RGBA) (bool, int)

HasColor returns true and index of color if it exists in palette

func (Palette) Index added in v0.0.5

func (p Palette) Index(c color.Color) int

Index returns the index of the palette color closest to c in Euclidean R,G,B,A space.

func (*Palette) ShiftBack added in v0.0.6

func (p *Palette) ShiftBack(i int)

ShiftBack moves palette back by i and wraps front to end

func (*Palette) ShiftForward added in v0.0.6

func (p *Palette) ShiftForward(i int)

ShiftForward moves palette ahead by i and wraps end to front

type Screen

type Screen struct {
	Layers []*Layer //Slice of screen layers
	W, H   int32    //Screen resolution
	CS     *CharacterSet
	// contains filtered or unexported fields
}

Screen manages the global state of go13h, and holds some useful info (ex character set) Screen also manages layer ordering and final rendering

func (*Screen) NewLayer

func (scr *Screen) NewLayer(w, h int32, lf bool) (*Layer, error)

NewLayer sets up and registers a new *Layer, note layers do not have to be the same width, height or texture filtering as other layers Layers can take up a lot of memory very easily, they take up their current working Memory, as well as the memory required for the backing texture ex: a 1920x1080 layer takes up 16Megabytes of RAM at minimum

func (*Screen) QuitScreen

func (scr *Screen) QuitScreen()

QuitScreen closes off SDL assets

func (*Screen) Update

func (scr *Screen) Update() error

Update refreshes display

Jump to

Keyboard shortcuts

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