suslik

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT Imports: 16 Imported by: 0

README

Suslik

Cross-platform 2d engine written in Golang with goal to be simple and focus to support these platforms: Windows, Linux, Web (wasm and Gopherjs).

Name

This name comes from Russian суслик (suslik). Means gopher in English.

Credits

Thanks to these 3rd party projects:

  • The original engi game engine which engo was based off of (BSD license)
  • engo ECS game engine, initially forked to engi. (MIT License)
  • Oto, a low-level cross-platform library to play sound. The AudioSystem uses this and is based on the audio package used in Ebiten.

Documentation

Index

Constants

View Source
const (
	// Epsilon is some tiny value that determines how precisely equal we want our
	// floats to be.
	Epsilon float32 = 1e-3
	// MinNormal is the smallest normal value possible.
	MinNormal = float32(1.1754943508222875e-38) // 1 / 2**(127 - 1)

	// RadToDeg is multiplied with a radian value to get the equivalant value in degrees.
	RadToDeg = 180 / math.Pi
	// DegToRad is multiplied with a degree value to get the equivalent value in radians.
	DegToRad = math.Pi / 180
)

Variables

View Source
var (
	NONE    = Action(0)
	MOVE    = Action(1)
	PRESS   = Action(2)
	RELEASE = Action(3)
	REPEAT  = Action(4)

	SHIFT   = Modifier(0x0001)
	CONTROL = Modifier(0x0002)
	ALT     = Modifier(0x0004)
	SUPER   = Modifier(0x0008)
)
View Source
var (
	MouseLeft         = Key(-1)
	MouseRight        = Key(-2)
	MouseMiddle       = Key(-3)
	BoardDash         = Key(189)
	BoardApostrophe   = Key(222)
	BoardSemicolon    = Key(186)
	BoardEquals       = Key(187)
	BoardComma        = Key(188)
	BoardPeriod       = Key(190)
	BoardSlash        = Key(191)
	BoardBackslash    = Key(220)
	BoardBackspace    = Key(8)
	BoardTab          = Key(9)
	BoardCapsLock     = Key(20)
	BoardSpace        = Key(32)
	BoardEnter        = Key(13)
	BoardEscape       = Key(27)
	BoardInsert       = Key(45)
	BoardPrintScreen  = Key(42)
	BoardDelete       = Key(46)
	BoardPageUp       = Key(33)
	BoardPageDown     = Key(34)
	BoardHome         = Key(36)
	BoardEnd          = Key(35)
	BoardPause        = Key(19)
	BoardScrollLock   = Key(145)
	BoardArrowLeft    = Key(37)
	BoardArrowRight   = Key(39)
	BoardArrowDown    = Key(40)
	BoardArrowUp      = Key(38)
	BoardLeftBracket  = Key(219)
	BoardLeftShift    = Key(16)
	BoardLeftControl  = Key(17)
	BoardLeftSuper    = Key(73)
	BoardLeftAlt      = Key(18)
	BoardRightBracket = Key(221)
	BoardRightShift   = Key(16)
	BoardRightControl = Key(17)
	BoardRightSuper   = Key(73)
	BoardRightAlt     = Key(18)
	BoardZero         = Key(48)
	BoardOne          = Key(49)
	BoardTwo          = Key(50)
	BoardThree        = Key(51)
	BoardFour         = Key(52)
	BoardFive         = Key(53)
	BoardSix          = Key(54)
	BoardSeven        = Key(55)
	BoardEight        = Key(56)
	BoardNine         = Key(57)
	BoardF1           = Key(112)
	BoardF2           = Key(113)
	BoardF3           = Key(114)
	BoardF4           = Key(115)
	BoardF5           = Key(116)
	BoardF6           = Key(117)
	BoardF7           = Key(118)
	BoardF8           = Key(119)
	BoardF9           = Key(120)
	BoardF10          = Key(121)
	BoardF11          = Key(122)
	BoardF12          = Key(123)
	BoardA            = Key(65)
	BoardB            = Key(66)
	BoardC            = Key(67)
	BoardD            = Key(68)
	BoardE            = Key(69)
	BoardF            = Key(70)
	BoardG            = Key(71)
	BoardH            = Key(72)
	BoardI            = Key(73)
	BoardJ            = Key(74)
	BoardK            = Key(75)
	BoardL            = Key(76)
	BoardM            = Key(77)
	BoardN            = Key(78)
	BoardO            = Key(79)
	BoardP            = Key(80)
	BoardQ            = Key(81)
	BoardR            = Key(82)
	BoardS            = Key(83)
	BoardT            = Key(84)
	BoardU            = Key(85)
	BoardV            = Key(86)
	BoardW            = Key(87)
	BoardX            = Key(88)
	BoardY            = Key(89)
	BoardZ            = Key(90)
	BoardNumLock      = Key(144)
	BoardNumMultiply  = Key(106)
	BoardNumDivide    = Key(111)
	BoardNumAdd       = Key(107)
	BoardNumSubtract  = Key(109)
	BoardNumZero      = Key(96)
	BoardNumOne       = Key(97)
	BoardNumTwo       = Key(98)
	BoardNumThree     = Key(99)
	BoardNumFour      = Key(100)
	BoardNumFive      = Key(101)
	BoardNumSix       = Key(102)
	BoardNumSeven     = Key(103)
	BoardNumEight     = Key(104)
	BoardNumNine      = Key(105)
	BoardNumDecimal   = Key(110)
	BoardNumEnter     = Key(13)
)
View Source
var (
	Time  *Clock
	Files *Loader
)

Functions

func CrossProduct

func CrossProduct(this, that Point) float32

CrossProduct returns the 2 dimensional cross product of this and that, which represents the magnitude of the three dimensional cross product

func DotProduct

func DotProduct(this, that Point) float32

DotProduct returns the dot product between this and that

func Exit

func Exit()

func FloatEqual

func FloatEqual(a, b float32) bool

FloatEqual is a safe utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/

It is slightly altered to not call Abs when not needed.

func FloatEqualThreshold

func FloatEqualThreshold(a, b, epsilon float32) bool

FloatEqualThreshold is a utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/

It is slightly altered to not call Abs when not needed.

This differs from FloatEqual in that it lets you pass in your comparison threshold, so that you can adjust the comparison value to your specific needs

func Height

func Height() float32

func LineTraceFraction

func LineTraceFraction(tracer, boundary Line) float32

LineTraceFraction returns the trace fraction of tracer through boundary 1 means no intersection 0 means tracer's origin lies on the boundary line

func LoadShader

func LoadShader(vertSrc, fragSrc string) *webgl.Program

func MultiplyMatrixVector

func MultiplyMatrixVector(m *Matrix, v []float32) []float32

MultiplyMatrixVector multiplies the matrix m with the float32 vector v and returns the result. The size of vector v MUST be 2 or 3. If v is size 2, a 3rd component is automatically added with value of 1.0.

func Open

func Open(title string, width, height int, fullscreen bool, hideCursor bool, r Responder)

func SetBg

func SetBg(color uint32)

func Width

func Width() float32

Types

type AABB

type AABB struct {
	Min, Max Point
}

AABB describes two points of a rectangle: the upper-left corner and the lower-right corner. It should always hold that `Min.X <= Max.X` and `Min.Y <= Max.Y`.

type AABBer

type AABBer interface {
	// AABB returns the axis aligned bounding box.
	AABB() AABB
}

AABBer is an interface for everything that provides information about its axis aligned bounding box.

type Action

type Action int

type Animation

type Animation struct {
	TotalFrames int
	Current     int
	Delay       time.Duration
	Playing     bool
	Timer       Timer
}

func NewAnimation

func NewAnimation(frames int, delay time.Duration) *Animation

func (*Animation) GetDelay

func (anim *Animation) GetDelay() time.Duration

func (*Animation) GetFrame

func (anim *Animation) GetFrame() int

func (*Animation) GetTotalFrames

func (anim *Animation) GetTotalFrames() int

func (*Animation) NextFrame

func (anim *Animation) NextFrame() int

func (*Animation) Play

func (anim *Animation) Play(num int)

func (*Animation) ResetTime added in v0.2.4

func (anim *Animation) ResetTime()

func (*Animation) SetDelay

func (anim *Animation) SetDelay(d time.Duration)

func (*Animation) SetFrame added in v0.2.4

func (anim *Animation) SetFrame(n int)

func (*Animation) SetTotalFrames

func (anim *Animation) SetTotalFrames(total int)

func (*Animation) Stop

func (anim *Animation) Stop()

type Assets

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

func NewAssets

func NewAssets() *Assets

func (*Assets) Get

func (a *Assets) Get(path string) Image

func (*Assets) Image

func (a *Assets) Image(path string)

func (*Assets) Load

func (a *Assets) Load(onFinish func())

type Batch

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

func NewBatch

func NewBatch(width, height float32) *Batch

func (*Batch) Begin

func (b *Batch) Begin()

func (*Batch) Draw

func (b *Batch) Draw(r Drawable, x, y, originX, originY, scaleX, scaleY, rotation float32, color uint32, transparency float32)

func (*Batch) End

func (b *Batch) End()

func (*Batch) SetProjection

func (b *Batch) SetProjection(width, height float32)

type BatchLine

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

func NewBatchLine

func NewBatchLine(width, height float32) *BatchLine

func (*BatchLine) Begin

func (b *BatchLine) Begin()

func (*BatchLine) Draw

func (b *BatchLine) Draw(l Line, color uint32, transparency float32)

func (*BatchLine) End

func (b *BatchLine) End()

func (*BatchLine) SetProjection

func (b *BatchLine) SetProjection(width, height float32)

type Camera

type Camera struct {
	Position Point
	Viewport Point
	Zoom     float32
}

func NewCamera

func NewCamera(position, viewport Point) *Camera

func (*Camera) MoveTo

func (cam *Camera) MoveTo(position Point)

func (*Camera) TranslateToScreen

func (cam *Camera) TranslateToScreen(position Point) Point

func (*Camera) TranslateToWorld

func (cam *Camera) TranslateToWorld(position Point) Point

type Clock

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

func NewClock

func NewClock() *Clock

func (*Clock) Delta

func (c *Clock) Delta() float32

func (*Clock) Fps

func (c *Clock) Fps() float32

func (*Clock) Tick

func (c *Clock) Tick()

func (*Clock) Time

func (c *Clock) Time() float32

type Container

type Container interface {
	// Contains reports whether the container contains the given point.
	Contains(p Point) bool
}

A Container is a 2D closed shape which contains a set of points.

type Drawable

type Drawable interface {
	Texture() *webgl.Texture
	Width() float32
	Height() float32
	View() (float32, float32, float32, float32)
}

type Font

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

func NewGridFont

func NewGridFont(texture *Texture, cellWidth, cellHeight int) *Font

func (*Font) Print

func (f *Font) Print(batch *Batch, text string, x, y float32, color uint32)

func (*Font) Put

func (f *Font) Put(batch *Batch, r rune, x, y float32, color uint32)

func (*Font) Remap

func (f *Font) Remap(mapping string)

type Game

type Game struct{}

func (*Game) Close

func (g *Game) Close()

func (*Game) Key

func (g *Game) Key(key Key, modifier Modifier, action Action)

func (*Game) Mouse

func (g *Game) Mouse(x, y float32, button Key, action Action)

func (*Game) Preload

func (g *Game) Preload()

func (*Game) Render

func (g *Game) Render()

func (*Game) Resize

func (g *Game) Resize(w, h float32)

func (*Game) Scroll

func (g *Game) Scroll(amount float32)

func (*Game) Setup

func (g *Game) Setup()

func (*Game) Type

func (g *Game) Type(char rune)

func (*Game) Update

func (g *Game) Update(dt float32)

type Image

type Image interface {
	Data() interface{}
	Width() int
	Height() int
}

func LoadImage

func LoadImage(data interface{}) Image

type ImageObject

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

func NewImageObject

func NewImageObject(img *image.NRGBA) *ImageObject

func (*ImageObject) Data

func (i *ImageObject) Data() interface{}

func (*ImageObject) Height

func (i *ImageObject) Height() int

func (*ImageObject) Width

func (i *ImageObject) Width() int

type Key

type Key int

type Line

type Line struct {
	P1 Point
	P2 Point
}

Line describes a line segment on a 2 dimensional euclidean space it can also be thought of as a 2 dimensional vector with an offset

func (*Line) Angle

func (l *Line) Angle() float32

Angle returns the euclidean angle of l in radians relative to a vertical line, going positive as you head towards the positive x-axis (clockwise) and negative as you head towards the negative x-axis. Values returned are [-pi, pi].

func (*Line) AngleDeg

func (l *Line) AngleDeg() float32

AngleDeg returns the euclidean angle of l in degrees relative to a vertical line, going positive as you head towards the positive x-axis (clockwise) and negative as you head towards the negative x-axis. Values returned are [-180, 180].

func (*Line) Magnitude

func (l *Line) Magnitude() float32

Magnitude returns the length of the line

func (*Line) Normal

func (l *Line) Normal() Point

Normal returns the left hand normal of the line segment l

func (*Line) PointDistance

func (l *Line) PointDistance(point Point) float32

PointDistance Returns the euclidean distance from the point p to the line segment l

func (*Line) PointDistanceSquared

func (l *Line) PointDistanceSquared(point Point) float32

PointDistanceSquared returns the squared euclidean distance from the point p to the line segment l

func (*Line) PointSide

func (l *Line) PointSide(point Point) bool

PointSide returns which side of the line l the point p sits on true means the point is below/left of the line false means the point is above/right of the line or touching the line

type Loader

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

func NewLoader

func NewLoader() *Loader

func (*Loader) Add

func (l *Loader) Add(name, url string)

func (*Loader) Image

func (l *Loader) Image(name string) *Texture

func (*Loader) Json

func (l *Loader) Json(name string) string

func (*Loader) Load

func (l *Loader) Load(onFinish func())

func (*Loader) Sound

func (l *Loader) Sound(name string) *Sound

type Matrix

type Matrix struct {
	Val [9]float32
	// contains filtered or unexported fields
}

Matrix describes a 3x3 column-major matrix useful for 2D transformations.

func IdentityMatrix

func IdentityMatrix() *Matrix

IdentityMatrix returns a new identity matrix.

func (*Matrix) Identity

func (m *Matrix) Identity() *Matrix

Identity sets the matrix to the Identity matrix and returns the matrix.

func (*Matrix) Multiply

func (m *Matrix) Multiply(m2 *Matrix) *Matrix

Multiply postmultiplies m matrix with m2 and stores the result in m, returning m. Multiplaction is the result of m2 times m.

func (*Matrix) Rotate

func (m *Matrix) Rotate(deg float32) *Matrix

Rotate rorates m counter-clockwise by deg degrees.

func (*Matrix) RotateRad

func (m *Matrix) RotateRad(rad float32) *Matrix

RotateRad rotates m counter-clockwise by rad radians.

func (*Matrix) RotationComponent

func (m *Matrix) RotationComponent() float32

RotationComponent returns the current rotation component of m in degrees. This assumes uniform scaling.

func (*Matrix) RotationComponentRad

func (m *Matrix) RotationComponentRad() float32

RotationComponentRad returns the current rotation component of m in radians. This assumes uniform scaling.

func (*Matrix) Scale

func (m *Matrix) Scale(x, y float32) *Matrix

Scale scales m by x and y.

func (*Matrix) ScaleComponent

func (m *Matrix) ScaleComponent() (x, y float32)

ScaleComponent returns the current scale component of m. This assumes uniform scaling.

func (*Matrix) Set

func (m *Matrix) Set(val []float32) *Matrix

Set sets the matrix to the given float slice and returns the matrix. The float slice must have at least 9 elements. If the float slie contains more than 9 elements, only the first 9 will be copied.

func (*Matrix) Translate

func (m *Matrix) Translate(x, y float32) *Matrix

Translate translates m by the point (x, y).

func (*Matrix) TranslatePoint

func (m *Matrix) TranslatePoint(p Point) *Matrix

TranslatePoint translates m by the point p.

func (*Matrix) TranslationComponent

func (m *Matrix) TranslationComponent() (x, y float32)

TranslationComponent returns the current translation component of m. This assumes uniform scaling.

type Modifier

type Modifier int

type Point

type Point struct {
	X, Y float32
}

Point describes a coordinate in a 2 dimensional euclidean space it can also be thought of as a 2 dimensional vector from the origin

func LineIntersection

func LineIntersection(one, two Line) (Point, bool)

LineIntersection returns the point where the line segments one and two intersect and true if there is intersection, nil and false when line segments one and two do not intersect

func (*Point) Add

func (p *Point) Add(p2 Point) *Point

Add sets the components of p to the pointwise summation of p + p2

func (*Point) AddScalar

func (p *Point) AddScalar(s float32) *Point

AddScalar adds s to each component of p

func (*Point) Equal

func (p *Point) Equal(p2 Point) bool

Equal indicates whether two points have the same value, avoiding issues with float precision

func (*Point) Multiply

func (p *Point) Multiply(p2 Point) *Point

Multiply sets the components of p to the pointwise product of p * p2

func (*Point) MultiplyMatrixVector

func (p *Point) MultiplyMatrixVector(m *Matrix) *Point

MultiplyMatrixVector multiplies the matrix m with the point and returns the result.

func (*Point) MultiplyScalar

func (p *Point) MultiplyScalar(s float32) *Point

MultiplyScalar multiplies each component of p by s

func (*Point) Normalize

func (p *Point) Normalize() (Point, float32)

Normalize returns the unit vector from p, and its magnitude. if you try to normalize the null vector, the return value will be null values

func (*Point) PointDistance

func (p *Point) PointDistance(p2 Point) float32

PointDistance returns the euclidean distance between p and p2

func (*Point) PointDistanceSquared

func (p *Point) PointDistanceSquared(p2 Point) float32

PointDistanceSquared returns the squared euclidean distance between p and p2

func (*Point) ProjectOnto

func (p *Point) ProjectOnto(p2 Point) Point

ProjectOnto returns the vector produced by projecting p on to p2 returns an empty Point if they can't project onto one another

func (*Point) Set

func (p *Point) Set(x, y float32) *Point

Set sets the coordinates of p to x and y

func (*Point) Subtract

func (p *Point) Subtract(p2 Point) *Point

Subtract sets the components of p to the pointwise difference of p - p2

func (*Point) SubtractScalar

func (p *Point) SubtractScalar(s float32) *Point

SubtractScalar subtracts s from each component of p

func (Point) Within

func (p Point) Within(c Container) bool

Within reports whether the point is contained within the given container.

type Region

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

func NewRegion

func NewRegion(texture *Texture, x, y, w, h int) *Region

func (*Region) Height

func (r *Region) Height() float32

func (*Region) Texture

func (r *Region) Texture() *webgl.Texture

func (*Region) View

func (r *Region) View() (float32, float32, float32, float32)

func (*Region) Width

func (r *Region) Width() float32

type Resource

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

type Responder

type Responder interface {
	Render()
	Resize(width, height float32)
	Preload()
	Setup()
	Close()
	Update(dt float32)
	Mouse(x, y float32, button Key, action Action)
	Scroll(amount float32)
	Key(key Key, modifier Modifier, action Action)
	Type(char rune)
}

type Sound

type Sound struct {
	*audio.Player
}

type Sprite

type Sprite struct {
	Position Point
	Scale    Point
	Anchor   Point
	Rotation float32
	Color    uint32
	Alpha    float32
	Region   *Region
}

func NewSprite

func NewSprite(region *Region, x, y, scale float32) Sprite

func (*Sprite) Height

func (s *Sprite) Height() float32

func (*Sprite) Render

func (s *Sprite) Render(batch *Batch, cam *Camera)

func (*Sprite) Width

func (s *Sprite) Width() float32

type Texture

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

func NewTexture

func NewTexture(img Image) *Texture

func (*Texture) Height

func (t *Texture) Height() float32

Height returns the height of the texture.

func (*Texture) Texture

func (t *Texture) Texture() *webgl.Texture

func (*Texture) View

func (r *Texture) View() (float32, float32, float32, float32)

func (*Texture) Width

func (t *Texture) Width() float32

Width returns the width of the texture.

type Timer

type Timer struct {
	Begin          time.Time
	InactiveBegin  time.Time
	ActiveDuration time.Duration
	Run            bool
}

func MakeTimer

func MakeTimer() Timer

func (*Timer) Elapsed

func (t *Timer) Elapsed() time.Duration

func (*Timer) Reset

func (t *Timer) Reset()

func (*Timer) Start

func (t *Timer) Start()

func (*Timer) Stop

func (t *Timer) Stop()

type Trace

type Trace struct {
	Fraction    float32
	EndPosition Point
	Line
}

Trace describes all the values computed from a line trace

func LineTrace

func LineTrace(tracer Line, boundaries []Line) Trace

LineTrace runs a series of line traces from tracer to each boundary line and returns the nearest trace values

Directories

Path Synopsis
decode/convert
Package convert resamples and converts audio data
Package convert resamples and converts audio data
decode/vorbis
Package vorbis provides Ogg/Vorbis decoder.
Package vorbis provides Ogg/Vorbis decoder.
decode/wav
Package wav provides WAV (RIFF) decoder.
Package wav provides WAV (RIFF) decoder.
Package math currently is a wrapper to github.com/EngoEngine/math.
Package math currently is a wrapper to github.com/EngoEngine/math.

Jump to

Keyboard shortcuts

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