pixelpusher

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: BSD-3-Clause Imports: 13 Imported by: 2

README

pixelpusher

Build GoDoc License Go Report Card

Concurrent software rendering, triangle rasterization and pixel buffer manipulation.

screencap triangles

butterflies glitch effect

software rendered duck with glitch effect software rendered beveled cube

Features and limitations

  • Can draw software-rendered triangles concurrently, using goroutines. The work of drawing the triangles is divided on the available CPU cores.
  • Provides flat-shaded triangles.
  • Everything is drawn to a []uint32 pixel buffer (containing "red", "green", "blue" and "alpha").
  • Tested together with SDL2, but can be used with any graphics library that can output pixels from a pixel buffer.
  • The software rendering of 3D graphics in the screenshot above is provided by fauxgl. The outputs from this can be combined with effects from pixelpusher.

Getting started

This program imports pixelpusher, sets up a callback function for drawing pixels to the gfx.Pixels slice, creates a pixelpusher.Config struct and then calls the .Run function. The window title is Red Pixel:

package main

import (
    pp "github.com/xyproto/pixelpusher"
)

func onDraw(canvas *pp.Canvas) error {
    // x=0, y=0, red=255, green=0, blue=0
    return pp.Plot(canvas, 0, 0, 255, 0, 0)
}

func main() {
    // The window title is "Red Pixel"
    canvas := pp.New("Red Pixel")
    // onDraw will be called whenever it is time to draw a frame
    canvas.Run(onDraw, nil, nil, nil)
}

This program allows the user to press the arrow keys or the WASD keys to move a red pixel around and draw something. Press ctrl-s to save the image and press Esc or q to quit.

package main

import (
    "errors"

    pp "github.com/xyproto/pixelpusher"
)

var x, y = 160, 100

func onDraw(canvas *pp.Canvas) error {
    return pp.Plot(canvas, x, y, 255, 0, 0)
}

func onPress(left, right, up, down, space, enter, esc bool) error {
    if up {
        y--
    } else if down {
        y++
    }
    if left {
        x--
    } else if right {
        x++
    }
    if esc {
        return errors.New("quit")
    }
    return nil
}

func main() {
    pp.New("Simple Draw").Run(onDraw, onPress, nil, nil)
}

General information

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int32) int32

func Add

func Add(c1, c2 uint32) uint32

Add adds one color value on top of another. Only considers the alpha value of the second color value. Returns an opaque color.

func Alpha

func Alpha(cv uint32) uint8

Extract the alpha component from a ARGB uint32 color value

func Blend

func Blend(c1, c2 uint32) uint32

Blend blends two color values, based on the alpha value

func BlitImage

func BlitImage(pixels []uint32, pitch int32, img *image.RGBA) error

BlitImage blits an image on top of a pixel buffer, while blending

func BlitImageOnTop

func BlitImageOnTop(pixels []uint32, pitch int32, img *image.RGBA) error

BlitImageOnTop blits an image on top of a pixel buffer, disregarding any previous pixels. The resulting pixels are opaque.

func Blue

func Blue(cv uint32) uint8

Extract the blue component from a ARGB uint32 color value

func Clamp

func Clamp(x, a, b int32) int32

Clamp makes sure x is between the two given numbers by simply cutting it off

func ClampByte

func ClampByte(x, a, b uint8) uint8

Clamp makes sure x is between the two given numbers by simply cutting it off

func Clear

func Clear(pixels []uint32, c color.RGBA)

Clear changes all pixels to the given color

func ColorToColorValue

func ColorToColorValue(c color.RGBA) uint32

ColorToColorValue converts from color.RGBA to an ARGB uint32 color value

func ColorValueToRGBA

func ColorValueToRGBA(cv uint32) (uint8, uint8, uint8, uint8)

ColorValueToRGBA converts from an ARGB uint32 color value to four bytes

func FastClear

func FastClear(pixels []uint32, colorValue uint32)

FastClear changes all pixels to the given uint32 color value, like 0xff0000ff for: 0xff red, 0x00 green, 0x00 blue and 0xff alpha.

func FastPixel

func FastPixel(pixels []uint32, x, y int32, colorValue uint32, pitch int32)

FastPixel draws a pixel to the pixel buffer, given a uint32 ARGB value

func GetWrap

func GetWrap(pixels []uint32, pos, size int32) uint32

Return a pixel, with position wraparound instead of overflow

func GetXYWrap

func GetXYWrap(pixels []uint32, x, y, w, h, pitch int32) uint32

Return a pixel, with position wraparound instead of overflow

func GlitchyStretchContrast

func GlitchyStretchContrast(cores int, pixels []uint32, pitch int32, discardRatio float32)

GlitchyStretchContrast stretches the contrast of the pixels in the given "pixels" slice (of width "pitch"), discarding the discardRatio ratio of the most unpopular pixel values, then scaling the remaining pixels to cover the full 0..255 range.

func Green

func Green(cv uint32) uint8

Extract the green component from a ARGB uint32 color value

func HideCursor added in v1.1.0

func HideCursor()

HideCursor hides the mouse cursor

func HorizontalLine

func HorizontalLine(pixels []uint32, y, x1, x2 int32, c color.RGBA, pitch int32)

HorizontalLine draws a line from (x1, y) to (x2, y)

func HorizontalLineFast

func HorizontalLineFast(pixels []uint32, y, x1, x2 int32, c color.RGBA, pitch int32)

HorizontalLineFast draws a line from (x1, y) to (x2, y), but x1 must be smaller than x2!

func IsFullscreen added in v1.1.0

func IsFullscreen(window *sdl.Window) bool

Fullscreen checks if the current window has the WINDOW_FULLSCREEN or WINDOW_FULLSCREEN_DESKTOP flag set.

func Lengths

func Lengths(p1, p2 *Pos) (int32, int32)

Lengths returns the x direction and y direction distance between the two points

func Line

func Line(pixels []uint32, x1, y1, x2, y2 int32, c color.RGBA, pitch int32)

Line draws a line in a completely wrong way to the pixel buffer. pixels are the pixels, pitch is the width of the pixel buffer.

func Max2

func Max2(a, b int32) int32

Max2 returns the largest of two numbers

func Max3

func Max3(a, b, c int32) int32

Max3 finds the largest of three numbers

func Min2

func Min2(a, b int32) int32

Min2 returns the smallest of two numbers

func Min3

func Min3(a, b, c int32) int32

Min3 finds the smallest of three numbers

func MinMax

func MinMax(a, b int32) (int32, int32)

MinMax find the smallest and greatest of two given numbers

func MinMax3

func MinMax3(a, b, c int32) (int32, int32)

MinMax3 finds the smallest and largest of three numbers

func MinMax3Byte

func MinMax3Byte(a, b, c uint8) (uint8, uint8)

MinMax3Byte finds the smallest and largest of three bytes

func OrAlpha

func OrAlpha(cores int, pixels []uint32)

Turn on all the alpha bits

func Pixel

func Pixel(pixels []uint32, x, y int32, c color.RGBA, pitch int32)

Pixel draws a pixel to the pixel buffer

func PixelRGB

func PixelRGB(pixels []uint32, x, y int32, r, g, b uint8, pitch int32)

PixelRGB draws an opaque pixel to the pixel buffer, given red, green and blue

func PixelsToImage

func PixelsToImage(pixels []uint32, pitch int32) *image.RGBA

PixelsToImage converts a pixel buffer to an image.RGBA image

func Plot added in v1.1.0

func Plot(c *Canvas, x, y, r, g, b int) error

Plot can plot a pixel to a canvas. It is a bit slow because it contains additional checks. Modify canvas.Pixels directly for better performance.

func RGBAToColorValue

func RGBAToColorValue(r, g, b, a uint8) uint32

RGBAToColorValue converts from four bytes to an ARGB uint32 color value

func Red

func Red(cv uint32) uint8

Extract the red component from a ARGB uint32 color value

func RemoveBlue

func RemoveBlue(cores int, pixels []uint32)

RemoveBlue removes all blue color.

func RemoveGreen

func RemoveGreen(cores int, pixels []uint32)

RemoveGreen removes all green color.

func RemoveRed

func RemoveRed(cores int, pixels []uint32)

RemoveRed removes all red color.

func RendererToImage added in v1.1.0

func RendererToImage(renderer *sdl.Renderer) (*image.RGBA, error)

RendererToImage converts a *sdl.Renderer to an *image.RGBA image

func SaveImageToPNG

func SaveImageToPNG(img *image.RGBA, filename string, overwrite bool) error

SaveImageToPNG saves an image.RGBA image to a PNG file. Set overwrite to true to allow overwriting files.

func SavePixelsToPNG

func SavePixelsToPNG(pixels []uint32, pitch int32, filename string, overwrite bool) error

Save pixels in uint32 ARGB format to PNG with alpha. pitch is the width of the pixel buffer. Set overwrite to true to allow overwriting files.

func Scale

func Scale(x, fromA, toA, fromB, toB int32) int32

Scale an int on the scale from fromA to toA, to a scale from fromB to toB.

Example
fmt.Println(ScaleByte(0, 0, 255, 0, 255))
fmt.Println(ScaleByte(20, 20, 80, 0, 255))
fmt.Println(ScaleByte(80, 20, 80, 0, 255))
fmt.Println(ScaleByte(255, 255, 255, 0, 255))
fmt.Println(ScaleByte(0, 0, 0, 0, 255))
Output:

0
0
255
255
0

func ScaleByte

func ScaleByte(x, fromA, toA, fromB, toB uint8) uint8

ScaleByte scales a byte on the scale from fromA to toA, to a scale from fromB to toB.

func Screenshot added in v1.1.0

func Screenshot(renderer *sdl.Renderer, filename string, overwrite bool) error

Screenshot saves the contents of the given *sdl.Renderer to a PNG file. Set overwrite to true for overwriting any existing files.

func SetBlueBits

func SetBlueBits(cores int, pixels []uint32)

Turn on all the blue bits

func SetGreenBits

func SetGreenBits(cores int, pixels []uint32)

Turn on all the green bits

func SetRedBits

func SetRedBits(cores int, pixels []uint32)

Turn on all the red bits

func SetWrap

func SetWrap(pixels []uint32, pos, size int32, colorValue uint32)

Set a pixel, with position wraparound instead of overflow

func SetXYWrap

func SetXYWrap(pixels []uint32, x, y, w, h int32, colorValue uint32, pitch int32)

Set a pixel, with position wraparound instead of overflow

func ShowCursor added in v1.1.0

func ShowCursor()

ShowCursor shows the mouse cursor

func Sort2

func Sort2(a, b int32) (int32, int32)

Sort2 sorts two numbers

func Sort3

func Sort3(a, b, c int32) (int32, int32, int32)

Sort3 sorts three numbers

func StretchContrast

func StretchContrast(cores int, pixels []uint32, pitch int32, discardRatio float32)

StretchContrast uses "cores" CPU cores to concurrently stretch the contrast of the pixels in the given "pixels" slice (of width "pitch"), discarding the discardRatio ratio of the most unpopular pixel values, then scaling the remaining pixels to cover the full 0..255 range.

func ToggleFullscreen added in v1.1.0

func ToggleFullscreen(window *sdl.Window) bool

ToggleFullscreen switches to fullscreen, or back. Returns true if the mode has been switched to fullscreen. Also toggles the visibility of the mouse cursor.

func Triangle

func Triangle(cores int, pixels []uint32, x1, y1, x2, y2, x3, y3 int32, c color.RGBA, pitch int32)

Triangle draws a triangle, concurrently. Core is the number of goroutines that will be used. pitch is the "width" of the pixel buffer.

func Value

func Value(cv uint32) uint8

Extract the color value / intensity from a ARGB uint32 color value. Ignores alpha.

func ValueWithAlpha

func ValueWithAlpha(cv uint32) uint8

Extract the color value / intensity from a ARGB uint32 color value

func VerticalLine

func VerticalLine(pixels []uint32, x, y1, y2 int32, c color.RGBA, pitch int32)

VerticalLine draws a line from (x, y1) to (x, y2)

func VerticalLineFast

func VerticalLineFast(pixels []uint32, x, y1, y2 int32, c color.RGBA, pitch int32)

VerticalLineFast draws a line from (x, y1) to (x, y2), but y1 must be smaller than y2!

func WireTriangle

func WireTriangle(cores int, pixels []uint32, x1, y1, x2, y2, x3, y3 int32, c color.RGBA, pitch int32)

WireTriangle draws a wireframe triangle, concurrently. Core is the number of goroutines that will be used. pitch is the "width" of the pixel buffer.

Types

type ActionFunction added in v1.1.0

type ActionFunction func(bool, bool, bool, bool, bool, bool, bool) error

ActionFunction is called when keys are pressed or released. Order: left, right, up, down, space, return, esc

type Canvas added in v1.2.0

type Canvas struct {
	Title      string
	PixelScale int
	Width      int
	Height     int
	Pitch      int32
	FrameRate  int
	Opaque     uint8
	Pixels     []uint32
}

Canvas is a window title + pixels + additional info

func New added in v1.1.0

func New(title string) *Canvas

New creates a new Canvas

func (*Canvas) Run added in v1.2.0

func (c *Canvas) Run(drawFunc DrawFunction, pressFunc ActionFunction, releaseFunc ActionFunction, tickFunc TickFunction) error

Run takes an optional function draw drawing pixels, an optional function for when an action is pressed, an optional function for when an action is released and a function for each loop

type DrawFunction added in v1.1.0

type DrawFunction func(*Canvas) error

DrawFunction can be used to draw pixels to canvas.Pixels

type Pair

type Pair struct {
	Key   uint8
	Value int
}

A data structure to hold key/value pairs

type PairList

type PairList []Pair

A slice of pairs that implements sort.Interface to sort by values

func (PairList) Len

func (p PairList) Len() int

func (PairList) Less

func (p PairList) Less(i, j int) bool

func (PairList) Swap

func (p PairList) Swap(i, j int)

type Pos

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

Pos represents a position in 2D space

func Interpolate

func Interpolate(p1, p2 *Pos) []*Pos

Interpolate interpolates between two points, with the number of steps equal to the length of the longest stretch

func NewPos

func NewPos(x, y int32) *Pos

NewPos creates a new position

func (*Pos) String

func (p *Pos) String() string

type TickFunction added in v1.1.0

type TickFunction func() error

TickFunction is called at every loop

type Vec3

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

type Vertex

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

Vertex has a position and a color value

func NewVertex

func NewVertex(x, y, z float32, r, g, b, a uint8) *Vertex
Example
v := NewVertex(0, 1, 2, 3, 4, 5, 6)
fmt.Println(v)
Output:

v(0, 1, 2) color(3, 4, 5, 6)

func (*Vertex) A

func (v *Vertex) A() uint8

func (*Vertex) B

func (v *Vertex) B() uint8

func (*Vertex) G

func (v *Vertex) G() uint8

func (*Vertex) Get

func (v *Vertex) Get() (float32, float32, float32)

func (*Vertex) GetColor

func (v *Vertex) GetColor() color.RGBA

func (*Vertex) GetRGBA

func (v *Vertex) GetRGBA() (uint8, uint8, uint8, uint8)

func (*Vertex) GetVec3

func (v *Vertex) GetVec3() *Vec3

func (*Vertex) Normalize

func (v *Vertex) Normalize() error

func (*Vertex) R

func (v *Vertex) R() uint8

func (*Vertex) Set

func (v *Vertex) Set(x, y, z float32)

func (*Vertex) SetColor

func (v *Vertex) SetColor(c color.RGBA)

func (*Vertex) SetRGBA

func (v *Vertex) SetRGBA(r, g, b, a uint8)

func (*Vertex) String

func (v *Vertex) String() string

func (*Vertex) X

func (v *Vertex) X() float32

func (*Vertex) Y

func (v *Vertex) Y() float32

func (*Vertex) Z

func (v *Vertex) Z() float32

Directories

Path Synopsis
cmd
multicore
Used as an example in the README.md file
Used as an example in the README.md file

Jump to

Keyboard shortcuts

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