dango

package module
v0.0.0-...-77e5ff9 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 20 Imported by: 0

README

Dango

Dango is a collection of functions that can be used with Ebitengine

data.go - FS

embedded file system

Commonly used function to load files from embedded file system Example:

import "github.com/iatearock/dango"
//go:embed assets/*
var data embed.FS
vfs = dango.NewFS(data)

// example usage
img, err := vfs.GetImage("assets/images/red.png") // *ebiten.Image
ff, err := vfs.GetFontFace("assets/font/red.ttf") // font.Face
str, err := vfs.GetCSV("assets/csv/red.csv") // [][]string
byte, err := vfs.ReadFile("assets/csv/red.txt") // []byte

camera

Adopted from ebiten camera example: https://github.com/hajimehoshi/ebiten/tree/main/examples/camera

cam := &dango.Camera{}  // setup camera 
cam.SetViewPort(w, h)
cam.Update() // update when position/rotation/zoom/viewport change

spriteOp := &ebiten.DrawImageOptions{}  // init options, and then apply sprite's transformation to spriteOp
spriteOP.GeoM.Concat(cam.GeoM()) // multiply sprite's matrix to camera matrix
screen.Draw(sprite, spriteOp)

screenX, screenY := cam.WorldToScreen(worldX, worldY) // transform coordinates

scene

Scene manager adopted from ebiten Block example Handle transition between scenes that implement Update() and Draw(*ebiten.Image)

id

Simple unique id generator, concurrency safe, I think.

bitmasks

bits.go provide bitmask functions with mutex lock Example:

const {
	s1 = 1 << iota
	s2
	s3
    s4
}

b = NewBits()
b.Set(s2|s3)
b.Has(s1|s2)  // true, matched any bit
b.HasAll(s1|s2) // false, need to match all bits

Neon light

Add neon light effect to an image

Neon adds color c within light pixels away from any pixels with 255 for alpha channel. Gaussian blur is applied with sqaure of width blur, and sigma as Gaussian standard deviation If origin, original image is draw on top of new image If resize, new image will be larger due to Gaussian effect on the edge

img := LoadPNG("input.png") // import a png file to *image.RGBA

d := 2 // add colour up to 2 pixels aways, from existing pixel with 255 in alpha channel
k := 3 // 3x3 kernal size for Gaussian blurring
s := 1 // standard deviation for Gaussian kernal
yellow := color.RGBA{255,255,0,255}
origin := true // draw original image on top of the result
resize := false // remove edge pixel produced by the Gaussin kernal, i.e. false to return same image size as the input

dango.Neon(img, d, k, s, yellow, origin, resize)

Input image:input Result image:result with neon light effect

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddWidth

func AddWidth(img *image.RGBA, dist int, c color.RGBA) *image.RGBA

AddWidth light effect to any pixel with 255 in alpha channle within `dist` from the pixels

func Blur

func Blur(img *image.RGBA, size int, kernal []float64) *image.RGBA

Return a new image applying Gaussian Blur Note, for my purpose, the reutrn image size is larger by the size of the kernal, to capture the entire bluring effect

func ConsolePrompt

func ConsolePrompt(text string) string

ConsolePrompt takes a text prompt, wait for and return user input

func Convolution

func Convolution(img *image.RGBA, x, y, size int) []color.RGBA

Return the RGBA values of kernal with `size` x `size` in a list

func DistanceTolerance

func DistanceTolerance(a, b f64.Vec2, epsilon float64) bool

DistanceTolerance check if two 2D positions are close enough

func DrawRect

func DrawRect(w, h, t, edge int, c color.RGBA) *image.RGBA

DrawRect with width, height, thickness, tansparent edge

func G

func G(x, y, sigma float64) float64

G calculates kernal value with Gaussian distribution at point x, y

func Kernal

func Kernal(size int, sigma float64) []float64

Create a gaussian kernal, each row is appended to previous row

func LoadPNG

func LoadPNG(name string) *image.RGBA

Read img from path

func Neon

func Neon(img *image.RGBA, light, blur int, sigma float64,
	c color.RGBA, origin, resize bool) *image.RGBA

Neon add neon light effect of an image, Neon adds color `c` within `light` pixels away from any pixels with 255 for alpha channel. Gaussian blur is applied with sqaure of width `blur`, and `sigma` as Gaussian standard deviation If `origin`, original image is draw on top of new image If `resize`, new image will be larger due to Gaussian effect on the edge

func Paint

func Paint(img *image.RGBA, x, y, radius int, c color.RGBA)

Paint add colour to pixel radius away from x, y

func PosToUint

func PosToUint(p uint) uint

Convert bit position to uint, right most bit is bit 1,

func SegmentsIntersect

func SegmentsIntersect(x1, y1, x2, y2, x3, y3, x4, y4 float64) (float64, float64, error)

SegmentsIntersect find intersection point between line pt1 to pt2 and pt3 and pt4 return error if no intersection

func SimpleAlphaComposite

func SimpleAlphaComposite(s, b color.RGBA) color.RGBA

Simple Alpha Composite source with background pixel https://www.w3.org/TR/compositing-1/#simplealphacompositing

func Tolerance

func Tolerance(a, b, epsilon float64) bool

Tolerance check if a and b is close and within epsilon

func WritePNG

func WritePNG(path string, img image.Image) error

Write img to path

Types

type Bits

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

func NewBits

func NewBits() *Bits

func (*Bits) Clear

func (b *Bits) Clear(n uint)

func (*Bits) Has

func (b *Bits) Has(n uint) bool

Has matches any bit position

func (*Bits) HasAll

func (b *Bits) HasAll(n uint) bool

HasAll matches all bit positions

func (*Bits) Set

func (b *Bits) Set(n uint)

func (*Bits) String

func (b *Bits) String() string

func (*Bits) Toggle

func (b *Bits) Toggle(n uint)

func (*Bits) Value

func (b *Bits) Value() uint

type Camera

type Camera struct {
	ViewPort   f64.Vec2 // viewport should be the same as the window size/resolution
	Position   f64.Vec2 // points camera to `Position` in the world
	ZoomFactor int
	Rotation   float64
	// contains filtered or unexported fields
}

Camera projects world to Screen

func (*Camera) GeoM

func (c *Camera) GeoM() ebiten.GeoM

Ebiten GeoM, usage sprite.GeoM.Concat(camera.GeoM)

func (*Camera) GetPosition

func (c *Camera) GetPosition() (float64, float64)

GetPosition return x, y

func (*Camera) IsPointInViewport

func (c *Camera) IsPointInViewport(wx, wy float64) bool

IsPointInViewport check is a point in on screen

func (*Camera) Matrix

func (c *Camera) Matrix() ebiten.GeoM

Matrix return current camera matrix

func (*Camera) Pan

func (c *Camera) Pan(x, y float64)

func (*Camera) Render

func (c *Camera) Render(screen, world *ebiten.Image)

func (*Camera) Reset

func (c *Camera) Reset()

func (*Camera) Rotate

func (c *Camera) Rotate(a float64)

func (*Camera) ScreenToWorld

func (c *Camera) ScreenToWorld(posX, posY int) (float64, float64)

func (*Camera) SetPosition

func (c *Camera) SetPosition(x, y float64)

SetPosition moves camera to location x, y

func (*Camera) SetViewPort

func (c *Camera) SetViewPort(w, h int)

SetViewPort set the size of the window

func (*Camera) SpriteGeoMConcat

func (c *Camera) SpriteGeoMConcat(sprite ebiten.GeoM) ebiten.GeoM

DEPRECATED, Concat camera's matrix with m

func (*Camera) String

func (c *Camera) String() string

func (*Camera) Update

func (c *Camera) Update()

UpdateMatrix when position/rotation/zoom changes

func (*Camera) WorldToScreen

func (c *Camera) WorldToScreen(wx, wy float64) (float64, float64)

func (*Camera) WorldToScreen32

func (c *Camera) WorldToScreen32(wx, wy float64) (float32, float32)

WorldToScreen32 return x, y in float32, ebiten screen drawing use float32

func (*Camera) ZoomIn

func (c *Camera) ZoomIn(n int)

func (*Camera) ZoomOut

func (c *Camera) ZoomOut(n int)

type EmbedFS

type EmbedFS interface {
	fs.ReadDirFS
	fs.ReadFileFS
}

type FS

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

func NewFS

func NewFS(filesystem EmbedFS) *FS

func (*FS) GetFontFace

func (f *FS) GetFontFace(path string, size, dpi float64) (font.Face, error)

func (*FS) GetImage

func (f *FS) GetImage(path string) (*ebiten.Image, error)

func (*FS) GetRGBA

func (f *FS) GetRGBA(path string) (*image.RGBA, error)

func (*FS) MustGetFontFace

func (f *FS) MustGetFontFace(path string, size, dpi float64) font.Face

func (*FS) MustGetImage

func (f *FS) MustGetImage(path string) *ebiten.Image

func (*FS) MustReadCSV

func (f *FS) MustReadCSV(path string) [][]string

func (*FS) MustReadFile

func (f *FS) MustReadFile(path string) []byte

func (*FS) Open

func (f *FS) Open(path string) (fs.File, error)

func (*FS) ReadCSV

func (f *FS) ReadCSV(path string) ([][]string, error)

ReadCSV return list of rows, only use for small file

func (*FS) ReadDir

func (f *FS) ReadDir(path string) ([]fs.DirEntry, error)

func (*FS) ReadFile

func (f *FS) ReadFile(path string) ([]byte, error)

type ID

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

func NewIDGenerator

func NewIDGenerator() *ID

func (*ID) NewID

func (i *ID) NewID() int

func (*ID) Reset

func (i *ID) Reset()

func (*ID) SetCurrent

func (i *ID) SetCurrent(n int)

Up to the caller to ensure id is still unique

type Scene

type Scene interface {
	Update() error
	Draw(screen *ebiten.Image)
}

type SceneManager

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

func NewSceneManager

func NewSceneManager(w, h, transitionFrames int) *SceneManager

Create new scene manager, where w - screen width, h - screen height, transitionFrames - number of frames to transit between scenes

func (*SceneManager) Draw

func (s *SceneManager) Draw(r *ebiten.Image)

func (*SceneManager) GoTo

func (s *SceneManager) GoTo(scene Scene)

func (*SceneManager) Update

func (s *SceneManager) Update() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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