dango

package module
v0.0.0-...-4aa67a7 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 21 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 Clamp

func Clamp(f, min, max float64) float64

func Clamp01

func Clamp01(f float64) float64

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 EqualFloat

func EqualFloat(a, b, tolerance float64) bool

func G

func G(x, y, sigma float64) float64

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

func InvertMatrix

func InvertMatrix(m []float64) ([]float64, bool)

InvertMatrix computes the inverse of a 4x4 matrix.

func Kernal

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

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

func Lerp

func Lerp(f1, f2, t float64) float64

func LerpConst

func LerpConst(f1, f2, d float64) float64

func LoadPNG

func LoadPNG(name string) *image.RGBA

Read img from path

func MatrixMultiplication

func MatrixMultiplication(a, b []float64) []float64

func MatrixVectorMultiplication

func MatrixVectorMultiplication(a, b []float64) []float64

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)

Render draw `world` image on screen

func (*Camera) Reset

func (c *Camera) Reset()

func (*Camera) Rotate

func (c *Camera) Rotate(a float64)

func (*Camera) Scale

func (c *Camera) Scale() 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 Camera3D

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

func NewCamera3D

func NewCamera3D(pos, lookAt Vector3, fov, w, h float64) *Camera3D

func (*Camera3D) FOV

func (cam *Camera3D) FOV(delta float64)

change fov, need to call Update() the camear manually

func (*Camera3D) LineToScreen

func (cam *Camera3D) LineToScreen(a Vector3, b Vector3) ([]float32, bool)

give screen coordinates of 2 points {x1 y1 z1 w1 x2 y2 z2 w2}, i.e. point1 [0] v[1], and point2 v[4] v[5] if one point is behind the Camera3D, it will return move that point to where the line intersect with the clip plan if both points are behind, it will return both points as { 0, 0, 0, -1} return boolean indicates if the line is in front of the Camera3D

func (*Camera3D) Move

func (cam *Camera3D) Move(dir Vector3)

move the Camera3D

func (*Camera3D) Pitch

func (cam *Camera3D) Pitch(rad float64)

pitch the Camera3D, need to call Update() the Camera3D manually

func (*Camera3D) PosToScreen

func (cam *Camera3D) PosToScreen(p Vector3) []float32

convert world position to screen coordinate

func (*Camera3D) PrintCombineMatrix

func (cam *Camera3D) PrintCombineMatrix() string

func (*Camera3D) PrintMatrix

func (cam *Camera3D) PrintMatrix(m []float64) string

print a 4 x 4 matrix

func (*Camera3D) PrintProjectionMatrix

func (cam *Camera3D) PrintProjectionMatrix() string

func (*Camera3D) PrintVector

func (cam *Camera3D) PrintVector(m []float64) string

print a 4 x 1 vector

func (*Camera3D) PrintViewMatrix

func (cam *Camera3D) PrintViewMatrix() string

func (*Camera3D) PrintViewportMatrix

func (cam *Camera3D) PrintViewportMatrix() string

func (*Camera3D) Update

func (cam *Camera3D) Update()

call after changeing Camera3D parameters

func (*Camera3D) UpdateCamera3D

func (cam *Camera3D) UpdateCamera3D(pos, lookAt Vector3, fov, w, h float64)

func (*Camera3D) WorldToScreen

func (cam *Camera3D) WorldToScreen(p []float64) []float64

convert world position to screen coordinate

func (*Camera3D) Yaw

func (cam *Camera3D) Yaw(rad float64)

rotate the Camera3D, need to call Update() the Camera3D manually

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) GetGoTextFaceSource

func (f *FS) GetGoTextFaceSource(path string) *text.GoTextFaceSource

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) Current

func (i *ID) Current() int

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

type Vector

type Vector struct {
	X, Y float64
}

func ForAngle

func ForAngle(a float64) Vector

ForAngle returns the unit length vector for the given angle (in radians).

func (Vector) Add

func (v Vector) Add(other Vector) Vector

func (Vector) CheckAxis

func (v Vector) CheckAxis(v1, p, n Vector) bool

func (Vector) Clamp

func (v Vector) Clamp(length float64) Vector

func (Vector) Clone

func (v Vector) Clone() Vector

func (Vector) ClosestDist

func (v Vector) ClosestDist(v1 Vector) float64

func (Vector) ClosestPointOnSegment

func (v Vector) ClosestPointOnSegment(a, b Vector) Vector

func (Vector) ClosestT

func (v Vector) ClosestT(b Vector) float64

func (Vector) Cross

func (v Vector) Cross(other Vector) float64

Cross calculates the 2D vector cross product analog. The cross product of 2D vectors results in a 3D vector with only a z component. This function returns the magnitude of the z value.

func (Vector) Distance

func (v Vector) Distance(other Vector) float64

func (Vector) DistanceSq

func (v Vector) DistanceSq(other Vector) float64

func (Vector) Dot

func (v Vector) Dot(other Vector) float64

func (Vector) Equal

func (v Vector) Equal(other Vector) bool

func (Vector) Length

func (v Vector) Length() float64

func (Vector) LengthSq

func (v Vector) LengthSq() float64

func (Vector) Lerp

func (v Vector) Lerp(other Vector, t float64) Vector

func (Vector) LerpConst

func (v Vector) LerpConst(other Vector, d float64) Vector

func (Vector) LerpT

func (v Vector) LerpT(b Vector, t float64) Vector

func (Vector) Mult

func (v Vector) Mult(s float64) Vector

func (Vector) Near

func (v Vector) Near(other Vector, d float64) bool

func (Vector) Neg

func (v Vector) Neg() Vector

func (Vector) Normalize

func (v Vector) Normalize() Vector

func (Vector) Perp

func (v Vector) Perp() Vector

func (Vector) PointGreater

func (v Vector) PointGreater(b, c Vector) bool

func (Vector) Project

func (v Vector) Project(other Vector) Vector

func (Vector) ReversePerp

func (v Vector) ReversePerp() Vector

func (Vector) Rotate

func (v Vector) Rotate(other Vector) Vector

func (Vector) SLerp

func (v Vector) SLerp(other Vector, t float64) Vector

func (Vector) SlerpConst

func (v Vector) SlerpConst(other Vector, a float64) Vector

func (Vector) String

func (v Vector) String() string

func (Vector) Sub

func (v Vector) Sub(other Vector) Vector

func (Vector) ToAngle

func (v Vector) ToAngle() float64

func (Vector) Unrotate

func (v Vector) Unrotate(other Vector) Vector

type Vector3

type Vector3 struct {
	X, Y, Z float64
}

func SliceToVector

func SliceToVector(a []float64) Vector3

take first 3 element as x, y, z

func (Vector3) Add

func (a Vector3) Add(b Vector3) Vector3

a add b

func (Vector3) Angle

func (a Vector3) Angle(b Vector3) float64

Angle returns the angle in radians between two vectors.

func (Vector3) Cross

func (a Vector3) Cross(b Vector3) Vector3

a cross b

func (Vector3) DistanceSq

func (a Vector3) DistanceSq(b Vector3) float64

func (Vector3) Dot

func (a Vector3) Dot(b Vector3) float64

func (Vector3) Length

func (v Vector3) Length() float64

func (Vector3) LengthSq

func (v Vector3) LengthSq() float64

func (Vector3) Lerp

func (a Vector3) Lerp(b Vector3, t float64) Vector3

func (Vector3) Mult

func (a Vector3) Mult(t float64) Vector3

a * float64

func (Vector3) Negate

func (v Vector3) Negate() Vector3

func (Vector3) Normalize

func (v Vector3) Normalize() Vector3

func (Vector3) Slerp

func (a Vector3) Slerp(b Vector3, t float64) Vector3

Slerp performs Spherical Linear Interpolation between two vectors. It's ideal for smoothly rotating a direction vector. Both vectors should be normalized for the best results.

func (Vector3) Sub

func (a Vector3) Sub(b Vector3) Vector3

a subtract b

func (Vector3) ToSlice

func (a Vector3) ToSlice() []float64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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