display

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Bits2 = &Ind2Bit{
	Threshold1_5: 0.15,
	Threshold2:   0.3,
	Threshold2_5: 0.45,
	Threshold3:   0.55,
	Threshold3_5: 0.7,
	Threshold4:   0.8,
}
View Source
var FullLight = FixedLight{1}

FullLight is a lighting model that produces a perfect lit environment.

View Source
var Palette2BitNames []string
View Source
var Palettes2Bit = map[string]Palette{
	"cga1": {
		0x0, 0x0, 0x0,
		0xff, 0x55, 0xff,
		0x55, 0xff, 0xff,
		0xff, 0xff, 0xff,
	},
	"cga2": {
		0x0, 0x0, 0x0,
		0xff, 0x55, 0x55,
		0x55, 0xff, 0x55,
		0xff, 0xff, 0x55,
	},
	"red-cyan": {
		0x0, 0x0, 0x0,
		0xff, 0x00, 0x00,
		0x00, 0xff, 0xff,
		0xff, 0xff, 0xff,
	},
	"magenta-green": {
		0x0, 0x0, 0x0,
		0xff, 0x00, 0xff,
		0x00, 0xff, 0x00,
		0xff, 0xff, 0xff,
	},
	"navy-yellow": {
		0x0, 0x0, 0x0,
		0x00, 0x00, 0xff,
		0xff, 0xff, 0x00,
		0xff, 0xff, 0xff,
	},
	"grey-red": {
		0x0, 0x0, 0x0,
		0x77, 0x77, 0x77,
		0xff, 0x00, 0x00,
		0xff, 0xff, 0xff,
	},
	"magenta-pink": {
		0x0, 0x0, 0x0,
		0x33, 0x11, 0x55,
		0xff, 0x55, 0x55,
		0xff, 0xff, 0xff,
	},
	"blue-orange": {
		0x0, 0x0, 0x0,
		0x44, 0x44, 0xff,
		0xff, 0x77, 0x00,
		0xff, 0xff, 0xff,
	},
	"desert": {
		0x0, 0x0, 0x0,
		0xaa, 0x44, 0x22,
		0xff, 0x77, 0x44,
		0xff, 0xff, 0xff,
	},
	"frozen": {
		0x0, 0x0, 0x0,
		0x22, 0x00, 0x77,
		0x00, 0x77, 0xee,
		0xff, 0xff, 0xff,
	},

	"volcanic": {
		0x0, 0x0, 0x0,
		0x33, 0x33, 0x33,
		0xff, 0x33, 0x00,
		0xff, 0xff, 0xff,
	},
	"acid1": {
		0x0, 0x0, 0x0,
		0x55, 0x00, 0x55,
		0x00, 0x77, 0x00,
		0x00, 0xff, 0xff,
	},
	"malachite": {
		0x0, 0x0, 0x0,
		0x00, 0x44, 0x22,
		0x00, 0xff, 0xaa,
		0xff, 0xff, 0xff,
	},
	"green-blood": {
		0x0, 0x0, 0x0,
		0x66, 0x00, 0x00,
		0x00, 0xff, 0x00,
		0xff, 0xff, 0xff,
	},
	"pine": {
		0x0, 0x0, 0x0,
		0x44, 0x22, 0x00,
		0x22, 0x77, 0x22,
		0xff, 0xff, 0xff,
	},
	"darkmoon": {
		0x0, 0x0, 0x0,
		0x00, 0x33, 0x33,
		0x00, 0x77, 0x00,
		0xff, 0xff, 0xff,
	},
	"painted-leather": {
		0x0, 0x0, 0x0,
		0x22, 0x55, 0x77,
		0xbb, 0x44, 0x33,
		0xff, 0xff, 0xff,
	},
	"pastel": {
		0x0, 0x0, 0x0,
		0xff, 0xb0, 0xff,
		0xb0, 0xff, 0xff,
		0xff, 0xff, 0xb0,
	},
}

Functions

func LoadImage

func LoadImage(fileName string) (image.Image, error)

func PerspTriShaderIndexed

func PerspTriShaderIndexed(iim IndexedImage, tx0, ty0, tx1, ty1, tx2, ty2 int,
	z0, z1, z2 float64) func(o *TriangleShaderOpts)

func RandomPalette2BitName

func RandomPalette2BitName() string

func RectShaderIndexed

func RectShaderIndexed(iim IndexedImage, tx0, ty0, tx1, ty1 int) func(o *RectangleShaderOpts)

func TriShaderIndexed

func TriShaderIndexed(iim IndexedImage, tx0, ty0, tx1, ty1, tx2, ty2 int,
	lightOffset, lightScale float64) func(o *TriangleShaderOpts)

Types

type CircleSource

type CircleSource struct {
	Tracked    Tracked
	Intensity  float64
	FallRadius float64
}

CircleSource is a point light source with circle area, which tracks an object position, size and alive status. At fall radius intensity is reduced by two times. The dependency is quadric.

func (*CircleSource) Alive

func (s *CircleSource) Alive() bool

func (*CircleSource) OffsetScale

func (s *CircleSource) OffsetScale(posx, posy int) (offs float64, scale float64)

type Display

type Display struct {
	Screen     IndexedImage
	RGBA       []byte
	Palette    Palette
	Rasterizer Rasterizer
	Atlases    map[string]*IndexedImage
	Sprites    map[string]*Sprite
	Lights     Lights
	Indexizer  Indexizer
	// contains filtered or unexported fields
}

func (*Display) DrawSprite

func (d *Display) DrawSprite(name string, x, y float64)

func (*Display) DrawSpriteAdvanced

func (d *Display) DrawSpriteAdvanced(o DrawSpriteOpts)

func (*Display) InitBuffers

func (d *Display) InitBuffers(w, h int)

func (*Display) LoadAtlas

func (d *Display) LoadAtlas(fileName string) error

func (*Display) ReportSprite

func (d *Display) ReportSprite(name string)

type DrawSpriteOpts

type DrawSpriteOpts struct {
	Name string
	SX   float64
	SY   float64
	SW   float64
	SH   float64
	DX   float64
	DY   float64
	DH   float64
	DW   float64
}

type FixedLight

type FixedLight struct {
	Intensity float64
}

FixedLight returns a constant intensity of light regardless of screen position. Intensity of 1 will produce the same as image intensity, while lower values will result in a dimmer color.

func (FixedLight) Light

func (l FixedLight) Light(val byte, posx, posy int) float64

type FromImageOpts

type FromImageOpts struct {
	// Number of colors to produce. Default is 255.
	Colors int

	// Alpha threshold. Alpha that is lower this is considered invisible.
	// Default is 0.5. Less than 0 produces no invisible pixels.
	AlphaThreshold float64
}

FromImageOpts is a structure with options for IndexedImageFromImage function.

type Ind2Bit

type Ind2Bit struct {
	Threshold1_5 float64
	Threshold2   float64
	Threshold2_5 float64
	Threshold3   float64
	Threshold3_5 float64
	Threshold4   float64
}

func (*Ind2Bit) Indexize

func (i *Ind2Bit) Indexize(intens float64, posx, posy int) byte

Color2Bit calculates 2-bit color index for input intensity (clamped to 0-1). To introduce lighting, add offset and/or multiply intensity by scale factor.

type IndexedImage

type IndexedImage struct {
	Width  int
	Height int

	// Pixels of the image, 1 byte per pixel.
	// Color 0 is reserved for no color.
	Pixels []byte

	// Number of colors image supports.
	Colors int
}

IndexedImage is a structure that represents image in indexed mode.

func IndexedImageFromImage

func IndexedImageFromImage(img image.Image, o FromImageOpts) IndexedImage

IndexedImageFromImage converts stdlib image.Image to IndexedImage with a given number of colors. Image is first converted to grascale, then intensity of each pixel is mapped to the color range. 0 is reserved for no color. No color is set for pixels which alpha is lower than alpha threshold.

func (IndexedImage) Fill

func (iim IndexedImage) Fill(v byte)

Fill buffer with provided byte.

func (IndexedImage) ToRGBA

func (iim IndexedImage) ToRGBA(o ToRGBAOpts) []byte

ToRGBA converts IndexedImage to RGBA mode. Resulting slice is 4 times larger than Pixels size. You can provide existing buffer in ToRGBAOpts.Pixels to avoid memory allocation, but it need to be exactly 4 times larger than Pixels of this image or function will panic.

type Indexizer

type Indexizer interface {
	Indexize(intens float64, posx, posy int) byte
}

type LightSet

type LightSet struct {
	Sources   []LightSource
	MinScale  float64
	MaxScale  float64
	MinOffset float64
	MaxOffset float64
}

LightSet is a lighting model that includes minimum and maximum scale and offset amd a number of light sources that add to scale and offset at each pixel position. Offset is added to original intensity (scaled to 0-1), then it's multiplied by scale. The more sources are used, the slower drawing functions are.

func (*LightSet) Light

func (l *LightSet) Light(val byte, posx, posy int) (intens float64)

Light calculates light intensity (0-1) at the point using current light model.

func (*LightSet) TrackCircle

func (l *LightSet) TrackCircle(t Tracked, intens, rfall float64)

TrackCircle adds a circle light source tied to an object that conforms a Tracked interface.

type LightSource

type LightSource interface {
	OffsetScale(posx, posy int) (offs float64, scale float64)
	Alive() bool
}

LightSource provides information about lighting for each point of screen. The most common implementation is a point light source that has a diminishing light the farther it is located from the light source. When light source Alive method returns false, the light source will be dropped from the current model.

type Lights

type Lights interface {
	Light(val byte, posx, posy int) (intens float64)
}

Lights calculates light intensity in a given point of the screen. It gets color value (intesity) at given point in the range of 1 (black) to 255 (white) and modifies it to produce intensity in the range (0-1). This intensity is supposed to be changed by Indexizer into indexed color.

type Palette

type Palette []byte

Triplets for each color. Must be multiplier of 3 in size.

func (Palette) Colorize

func (p Palette) Colorize(canvas []byte, index byte)

Colorize sets pixels to RGBA values from palette. Index starts from 1, 0 sets no-color (zero alpha).

func (Palette) ColorsNumber

func (p Palette) ColorsNumber() int

ColorsNumber returns number of colors in palette.

type Rasterizer

type Rasterizer struct {
	Workers int
	// contains filtered or unexported fields
}

func (*Rasterizer) DrawRectangle

func (r *Rasterizer) DrawRectangle(ri RectangleInfo)

func (*Rasterizer) DrawTriangle

func (r *Rasterizer) DrawTriangle(ti TriangleInfo)

func (*Rasterizer) Run

func (r *Rasterizer) Run()

type RectangleChunk

type RectangleChunk struct {
	RectangleRasterStatic

	// Origin x, y, pixels.
	Xo, Yo float64

	// Origin percentage coords.
	Pxo, Pyo float64

	// Offset in buffer, pixels. Equal to x + y * BufferWidth.
	BufferOffset int

	// Chunk width in pixels (can be less than chunk size).
	Width float64

	// Chunk height in pixels (can be less than chunk size).
	Height float64

	WG *sync.WaitGroup
}

RectangleChunk data for rectangle chunk to pass to rasterizer worker.

func (RectangleChunk) Process

func (c RectangleChunk) Process()

type RectangleInfo

type RectangleInfo struct {
	RectangleRasterInput

	X, Y float64
	W, H float64
}

RectangleInfo input data to DrawRectangle call.

type RectangleRasterInput

type RectangleRasterInput struct {
	// Pixel buffer. Shader needs to output color to buffer with
	// boffs offset and value calculated by shader. Buffer can use multiple
	// bytes for pixel. Multiply boffs by pixel size in this case.
	Buffer       []byte
	BufferWidth  int
	BufferHeight int

	// Function to call for each fragment.
	// Shader is responsible for saving pixel to the buffer. Use lambda function that
	// has access to the buffer to do that. See bit-shader for example.
	Shader func(o *RectangleShaderOpts)

	// Raster chunks size (in bits, e.g. 3 bits = 8 pixels).
	ChunkBits int

	// Lights model.
	Lights Lights
	// Object to convert to index color.
	Indexizer Indexizer

	// Any other data to pass to shader.
	Extra interface{}
}

RectangleRasterInput part of options that is passed to rasterizer workers.

type RectangleRasterStatic

type RectangleRasterStatic struct {
	RectangleRasterInput

	// Percentage texture coords step per pixel.
	Pxs, Pys float64
}

RectangleRasterStatic part of options including input and precalculated for the whole triangle.

type RectangleShaderOpts

type RectangleShaderOpts struct {
	RectangleRasterStatic

	// Cooridnates in buffer, pixels.
	X, Y float64

	// Percentage texture coords origin.
	Px, Py float64

	// Offset in buffer, pixels. Equal to int(x) + int(y) * BufferWidth.
	BufferOffset int
}

ShaderOpts options for shader.

type Sprite

type Sprite struct {
	Atlas            *IndexedImage
	X, Y             int
	Width, Height    int
	XOrigin, YOrigin int
}

type ToRGBAOpts

type ToRGBAOpts struct {
	// Prepared Pixels buffer to use.
	Pixels []byte

	// Palette to convert indexed color to RGBA.
	Palette Palette
}

type Tracked

type Tracked interface {
	Pos() (float64, float64)
	Alive() bool
	SizeMod() float64
}

Tracked is an interface that describes object that can be tracked by the light system. Required methods are Pos which gives current position of a light source, SizeMod that can control momentary radius of the light, and Alive property, which, when returns false, will cause the removing of the tracked object. When creating game, any objects that acts as a point light source, needs to implement this interface. When object is deleted (removed from the game), set an internal variable to return Alive equals to false, so light source could be unregistered.

type TriangleChunk

type TriangleChunk struct {
	TriangleRasterStatic

	// Origin x, y, pixels.
	Xo, Yo float64

	// Origin barycentric coords.
	W0o, W1o, W2o float64

	// Offset in buffer, pixels. Equal to x + y * BufferWidth.
	BufferOffset int

	// Chunk width in pixels (can be less than chunk size).
	Width float64

	// Chunk height in pixels (can be less than chunk size).
	Height float64

	WG *sync.WaitGroup
}

TriangleChunk data for triangle chunk to pass to rasterizer worker.

func (TriangleChunk) Process

func (c TriangleChunk) Process()

type TriangleInfo

type TriangleInfo struct {
	TriangleRasterInput

	X0, Y0 float64
	X1, Y1 float64
	X2, Y2 float64
}

TriangleInfo input data to DrawTriangle call.

type TriangleRasterInput

type TriangleRasterInput struct {
	// Pixel buffer. Shader needs to output color to buffer with
	// boffs offset and value calculated by shader. Buffer can use multiple
	// bytes for pixel. Multiply boffs by pixel size in this case.
	Buffer       []byte
	BufferWidth  int
	BufferHeight int

	// Function to call for each fragment.
	// Shader is responsible for saving pixel to the buffer. Use lambda function that
	// has access to the buffer to do that. See bit-shader for example.
	Shader func(o *TriangleShaderOpts)

	// Raster chunks size (in bits, e.g. 3 bits = 8 pixels).
	ChunkBits int

	// Lights model.
	Lights Lights
	// Object to convert to index color.
	Indexizer Indexizer

	// Any other data to pass to shader.
	Extra interface{}
}

TriangleRasterInput part of options that is passed to rasterizer workers.

type TriangleRasterStatic

type TriangleRasterStatic struct {
	TriangleRasterInput

	// Barycentric coords steps.
	A01, B01 float64
	A12, B12 float64
	A20, B20 float64

	// 1 / area.
	InvArea float64
}

TriangleRasterStatic part of options including input and precalculated for the whole triangle.

type TriangleShaderOpts

type TriangleShaderOpts struct {
	TriangleRasterStatic

	// Cooridnates in buffer, pixels.
	X, Y float64

	// Barycentric coords.
	W0, W1, W2 float64

	// Offset in buffer, pixels. Equal to int(x) + int(y) * BufferWidth.
	BufferOffset int
}

TriangleShaderOpts options for shader.

Jump to

Keyboard shortcuts

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