Documentation ¶
Overview ¶
Package pixel implements platform and backend agnostic core of the Pixel game development library.
It specifies the core Target, Triangles, Picture pattern and implements standard elements, such as Sprite, Batch, Vec, Matrix and RGBA in addition to the basic Triangles and Picture implementations: TrianglesData and PictureData.
Index ¶
- Constants
- Variables
- func Clamp(x, min, max float64) float64
- type Action
- type Anchor
- type BasicTarget
- type Batch
- type Bezier
- type Button
- type Circle
- func (c Circle) Area() float64
- func (c Circle) Bounds() Rect
- func (c Circle) Contains(u Vec) bool
- func (c Circle) Formula() (h, k float64)
- func (c Circle) Intersect(d Circle) Circle
- func (c Circle) IntersectLine(l Line) Vec
- func (c Circle) IntersectRect(r Rect) Vec
- func (c Circle) IntersectionPoints(l Line) []Vec
- func (c Circle) Moved(delta Vec) Circle
- func (c Circle) Norm() Circle
- func (c Circle) Resized(radiusDelta float64) Circle
- func (c Circle) String() string
- func (c Circle) Union(d Circle) Circle
- type ComposeMethod
- type ComposeTarget
- type Drawer
- type GamepadAxis
- type GamepadButton
- type InputHandler
- func (ih *InputHandler) ButtonEvent(button Button, action Action)
- func (ih *InputHandler) CharEvent(r rune)
- func (ih *InputHandler) JustPressed(button Button) bool
- func (ih *InputHandler) JustReleased(button Button) bool
- func (ih *InputHandler) MouseEnteredEvent(entered bool)
- func (ih *InputHandler) MouseInsideWindow() bool
- func (ih *InputHandler) MouseMoveEvent(pos Vec)
- func (ih *InputHandler) MousePosition() Vec
- func (ih *InputHandler) MousePreviousPosition() Vec
- func (ih *InputHandler) MousePreviousScroll() Vec
- func (ih *InputHandler) MouseScroll() Vec
- func (ih *InputHandler) MouseScrollEvent(x, y float64)
- func (ih *InputHandler) Pressed(button Button) bool
- func (ih *InputHandler) Repeated(button Button) bool
- func (ih *InputHandler) SetMousePosition(pos Vec)
- func (ih *InputHandler) Typed() string
- func (ih *InputHandler) Update()
- type Joystick
- type Line
- func (l Line) Bounds() Rect
- func (l Line) Center() Vec
- func (l Line) Closest(v Vec) Vec
- func (l Line) Contains(v Vec) bool
- func (l Line) Formula() (m, b float64)
- func (l Line) Intersect(k Line) (Vec, bool)
- func (l Line) IntersectCircle(c Circle) Vec
- func (l Line) IntersectRect(r Rect) Vec
- func (l Line) Len() float64
- func (l Line) Moved(delta Vec) Line
- func (l Line) Rotated(around Vec, angle float64) Line
- func (l Line) Scaled(scale float64) Line
- func (l Line) ScaledXY(around Vec, scale float64) Line
- func (l Line) String() string
- type Matrix
- func (m Matrix) Chained(next Matrix) Matrix
- func (m Matrix) Moved(delta Vec) Matrix
- func (m Matrix) Project(u Vec) Vec
- func (m Matrix) Rotated(around Vec, angle float64) Matrix
- func (m Matrix) Scaled(around Vec, scale float64) Matrix
- func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix
- func (m Matrix) String() string
- func (m Matrix) Unproject(u Vec) Vec
- type Picture
- type PictureColor
- type PictureData
- type RGBA
- type Rect
- func (rect Rect) AlignedTo(anchor Anchor) Rect
- func (r Rect) AnchorPos(anchor Anchor) Vec
- func (r Rect) Area() float64
- func (r Rect) Bounds() Rect
- func (r Rect) Center() Vec
- func (r Rect) Contains(u Vec) bool
- func (r Rect) Edges() [4]Line
- func (r Rect) H() float64
- func (r Rect) Intersect(s Rect) Rect
- func (r Rect) IntersectCircle(c Circle) Vec
- func (r Rect) IntersectLine(l Line) Vec
- func (r Rect) IntersectionPoints(l Line) []Vec
- func (r Rect) Intersects(s Rect) bool
- func (r Rect) Moved(delta Vec) Rect
- func (r Rect) Norm() Rect
- func (r Rect) Resized(anchor, size Vec) Rect
- func (r Rect) ResizedMin(size Vec) Rect
- func (r Rect) Size() Vec
- func (r Rect) String() string
- func (r Rect) Union(s Rect) Rect
- func (r Rect) Vertices() [4]Vec
- func (r Rect) W() float64
- type Sprite
- type Target
- type TargetPicture
- type TargetTriangles
- type Triangles
- type TrianglesClipped
- type TrianglesColor
- type TrianglesData
- func (td *TrianglesData) ClipRect(i int) (rect Rect, has bool)
- func (td *TrianglesData) Color(i int) RGBA
- func (td *TrianglesData) Copy() Triangles
- func (td *TrianglesData) Len() int
- func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64)
- func (td *TrianglesData) Position(i int) Vec
- func (td *TrianglesData) SetLen(len int)
- func (td *TrianglesData) Slice(i, j int) Triangles
- func (td *TrianglesData) Update(t Triangles)
- type TrianglesPicture
- type TrianglesPosition
- type Vec
- func (u Vec) Add(v Vec) Vec
- func (u Vec) Angle() float64
- func (u Vec) Cross(v Vec) float64
- func (u Vec) Dot(v Vec) float64
- func (u Vec) Eq(v Vec) bool
- func (u Vec) Floor() Vec
- func (u Vec) Len() float64
- func (u Vec) Map(f func(float64) float64) Vec
- func (u Vec) Normal() Vec
- func (u Vec) Project(v Vec) Vec
- func (u Vec) Rotated(angle float64) Vec
- func (u Vec) Scaled(c float64) Vec
- func (u Vec) ScaledXY(v Vec) Vec
- func (u Vec) SqLen() float64
- func (u Vec) String() string
- func (u Vec) Sub(v Vec) Vec
- func (u Vec) To(v Vec) Vec
- func (u Vec) Unit() Vec
- func (u Vec) XY() (x, y float64)
Constants ¶
const ( // List of all mouse buttons. MouseButton1 Button = iota MouseButton2 MouseButton3 MouseButton4 MouseButton5 MouseButton6 MouseButton7 MouseButton8 // List of all keyboard buttons. KeySpace KeyApostrophe KeyComma KeyMinus KeyPeriod KeySlash Key0 Key1 Key2 Key3 Key4 Key5 Key6 Key7 Key8 Key9 KeySemicolon KeyEqual KeyA KeyB KeyC KeyD KeyE KeyF KeyG KeyH KeyI KeyJ KeyK KeyL KeyM KeyN KeyO KeyP KeyQ KeyR KeyS KeyT KeyU KeyV KeyW KeyX KeyY KeyZ KeyLeftBracket KeyBackslash KeyRightBracket KeyGraveAccent KeyWorld1 KeyWorld2 KeyEscape KeyEnter KeyTab KeyBackspace KeyInsert KeyDelete KeyRight KeyLeft KeyDown KeyUp KeyPageUp KeyPageDown KeyHome KeyEnd KeyCapsLock KeyScrollLock KeyNumLock KeyPrintScreen KeyPause KeyF1 KeyF2 KeyF3 KeyF4 KeyF5 KeyF6 KeyF7 KeyF8 KeyF9 KeyF10 KeyF11 KeyF12 KeyF13 KeyF14 KeyF15 KeyF16 KeyF17 KeyF18 KeyF19 KeyF20 KeyF21 KeyF22 KeyF23 KeyF24 KeyF25 KeyKP0 KeyKP1 KeyKP2 KeyKP3 KeyKP4 KeyKP5 KeyKP6 KeyKP7 KeyKP8 KeyKP9 KeyKPDecimal KeyKPDivide KeyKPMultiply KeyKPSubtract KeyKPAdd KeyKPEnter KeyKPEqual KeyLeftShift KeyLeftControl KeyLeftAlt KeyLeftSuper KeyRightShift KeyRightControl KeyRightAlt KeyRightSuper KeyMenu // Last iota NumButtons int = iota // Aliases MouseButtonLeft = MouseButton1 MouseButtonRight = MouseButton2 MouseButtonMiddle = MouseButton3 )
const ( Joystick1 Joystick = iota Joystick2 Joystick3 Joystick4 Joystick5 Joystick6 Joystick7 Joystick8 Joystick9 Joystick10 Joystick11 Joystick12 Joystick13 Joystick14 Joystick15 Joystick16 // Last iota NumJoysticks int = iota )
const ( AxisLeftX GamepadAxis = iota AxisLeftY AxisRightX AxisRightY AxisLeftTrigger AxisRightTrigger // Last iota. NumAxes int = iota )
const ( GamepadA GamepadButton = iota GamepadB GamepadX GamepadY GamepadLeftBumper GamepadRightBumper GamepadBack GamepadStart GamepadGuide GamepadLeftThumb GamepadRightThumb GamepadDpadUp GamepadDpadRight GamepadDpadDown GamepadDpadLeft // Last iota NumGamepadButtons int = iota // Aliases GamepadCross = GamepadA GamepadCircle = GamepadB GamepadSquare = GamepadX GamepadTriangle = GamepadY )
Variables ¶
var ( Center = Anchor{0.5, 0.5} Top = Anchor{0.5, 0} TopRight = Anchor{0, 0} Right = Anchor{0, 0.5} BottomRight = Anchor{0, 1} Bottom = Anchor{0.5, 1} BottomLeft = Anchor{1, 1} Left = Anchor{1, 0.5} TopLeft = Anchor{1, 0} )
var IM = Matrix{1, 0, 0, 1, 0, 0}
IM stands for identity matrix. Does nothing, no transformation.
var RGBAModel = color.ModelFunc(rgbaModel)
RGBAModel converts colors to RGBA format.
var ZB = Constant(V(1, 0))
ZB is Zero Bezier Curve that skips calculation and always returns V(1, 0) Its mainly because Calculation uses lot of function calls and in case of particles, it can make some difference
var ZR = Rect{Min: ZV, Max: ZV}
ZR is a zero rectangle.
var ZV = Vec{0, 0}
ZV is a zero vector.
Functions ¶
Types ¶
type Anchor ¶
type Anchor Vec
Anchor is a vector used to define anchors, such as `Center`, `Top`, `TopRight`, etc.
type BasicTarget ¶
type BasicTarget interface { Target // SetMatrix sets a Matrix that every point will be projected by. SetMatrix(Matrix) // SetColorMask sets a color that will be multiplied with the TrianglesColor property of all // Triangles. SetColorMask(color.Color) }
BasicTarget is a Target with additional basic adjustment methods.
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch is a Target that allows for efficient drawing of many objects with the same Picture.
To put an object into a Batch, just draw it onto it:
object.Draw(batch)
func NewBatch ¶
NewBatch creates an empty Batch with the specified Picture and container.
The container is where objects get accumulated. Batch will support precisely those Triangles properties, that the supplied container supports. If you retain access to the container and change it, call Dirty to notify Batch about the change.
Note, that if the container does not support TrianglesColor, color masking will not work.
func (*Batch) Dirty ¶
func (b *Batch) Dirty()
Dirty notifies Batch about an external modification of it's container. If you retain access to the Batch's container and change it, call Dirty to notify Batch about the change.
container := &pixel.TrianglesData{} batch := pixel.NewBatch(container, nil) container.SetLen(10) // container changed from outside of Batch batch.Dirty() // notify Batch about the change
func (*Batch) MakePicture ¶
func (b *Batch) MakePicture(p Picture) TargetPicture
MakePicture returns a specialized copy of the provided Picture that draws onto this Batch.
func (*Batch) MakeTriangles ¶
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles
MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch.
func (*Batch) SetColorMask ¶
SetColorMask sets a mask color used in the following draws onto the Batch.
type Bezier ¶
type Bezier struct {
Start, StartHandle, EndHandle, End Vec
// contains filtered or unexported fields
}
Bezier is cubic Bézier curve used for interpolation. For more info see https://en.wikipedia.org/wiki/B%C3%A9zier_curve, In case you are looking for visualization see https://www.desmos.com/calculator/d1ofwre0fr
func B ¶
B returns new curve. if curve is just placeholder use constant. Handles are relative to start and end point so:
pixel.B(ZV, ZV, ZV, V(1, 0)) == Bezier{ZV, ZV, V(1, 0), V(1, 0)}
type Circle ¶
Circle is a 2D circle. It is defined by two properties:
- Center vector
- Radius float64
func C ¶
C returns a new Circle with the given radius and center coordinates.
Note that a negative radius is valid.
func (Circle) Contains ¶
Contains checks whether a vector `u` is contained within this Circle (including it's perimeter).
func (Circle) Formula ¶
Formula returns the values of h and k, for the equation of the circle: (x-h)^2 + (y-k)^2 = r^2 where r is the radius of the circle.
func (Circle) Intersect ¶
Intersect returns the maximal Circle which is covered by both `c` and `d`.
If `c` and `d` don't overlap, this function returns a zero-sized circle at the centerpoint between the two Circle's centers.
func (Circle) IntersectLine ¶
IntersectLine will return the shortest Vec such that if the Circle is moved by the Vec returned, the Line and Rect no longer intersect.
func (Circle) IntersectRect ¶
IntersectRect returns a minimal required Vector, such that moving the circle by that vector would stop the Circle and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only the perimeters touch.
This function will return a non-zero vector if:
- The Rect contains the Circle, partially or fully
- The Circle contains the Rect, partially of fully
func (Circle) IntersectionPoints ¶
IntersectionPoints returns all the points where the Circle intersects with the line provided. This can be zero, one or two points, depending on the location of the shapes. The points of intersection will be returned in order of closest-to-l.A to closest-to-l.B.
func (Circle) Norm ¶
Norm returns the Circle in normalized form - this sets the radius to its absolute value.
c := pixel.C(-10, pixel.ZV) c.Norm() // returns pixel.Circle{pixel.Vec{0, 0}, 10}
func (Circle) Resized ¶
Resized returns the Circle resized by the given delta. The Circles center is use as the anchor.
c := pixel.C(pixel.ZV, 10) c.Resized(-5) // returns pixel.Circle{pixel.Vec{0, 0}, 5} c.Resized(25) // returns pixel.Circle{pixel.Vec{0, 0}, 35}
type ComposeMethod ¶
type ComposeMethod int
ComposeMethod is a Porter-Duff composition method.
const ( ComposeOver ComposeMethod = iota ComposeIn ComposeOut ComposeAtop ComposeRover ComposeRin ComposeRout ComposeRatop ComposeXor ComposePlus ComposeCopy )
Here's the list of all available Porter-Duff composition methods. Use ComposeOver for the basic alpha blending.
func (ComposeMethod) Compose ¶
func (cm ComposeMethod) Compose(a, b RGBA) RGBA
Compose composes two colors together according to the ComposeMethod. A is the foreground, B is the background.
type ComposeTarget ¶
type ComposeTarget interface { BasicTarget // SetComposeMethod sets a Porter-Duff composition method to be used. SetComposeMethod(ComposeMethod) }
ComposeTarget is a BasicTarget capable of Porter-Duff composition.
type Drawer ¶
type Drawer struct { Triangles Triangles Picture Picture Cached bool // contains filtered or unexported fields }
Drawer glues all the fundamental interfaces (Target, Triangles, Picture) into a coherent and the only intended usage pattern.
Drawer makes it possible to draw any combination of Triangles and Picture onto any Target efficiently.
To create a Drawer, just assign it's Triangles and Picture fields:
d := pixel.Drawer{Triangles: t, Picture: p}
If Triangles is nil, nothing will be drawn. If Picture is nil, Triangles will be drawn without a Picture.
Whenever you change the Triangles, call Dirty to notify Drawer that Triangles changed. You don't need to notify Drawer about a change of the Picture.
Note, that Drawer caches the results of MakePicture from Targets it's drawn to for each Picture it's set to. What it means is that using a Drawer with an unbounded number of Pictures leads to a memory leak, since Drawer caches them and never forgets. In such a situation, create a new Drawer for each Picture.
type GamepadAxis ¶
type GamepadAxis int
GamepadAxis corresponds to a gamepad axis.
const UnknownGamepadAxis GamepadAxis = -1
Gamepad axis IDs.
func (GamepadAxis) String ¶
func (ga GamepadAxis) String() string
String returns a human-readable string describing the GamepadAxis.
type GamepadButton ¶
type GamepadButton int
GamepadButton corresponds to a gamepad button.
const UnknownGampadButton GamepadButton = -1
Gamepad button IDs.
func (GamepadButton) String ¶
func (gb GamepadButton) String() string
String returns a human-readable string describing the GamepadButton.
type InputHandler ¶
type InputHandler struct {
// contains filtered or unexported fields
}
func (*InputHandler) ButtonEvent ¶
func (ih *InputHandler) ButtonEvent(button Button, action Action)
ButtonEvent sets the action state of a button for the next update
func (*InputHandler) CharEvent ¶
func (ih *InputHandler) CharEvent(r rune)
CharEvent adds to the typed string for the next update
func (*InputHandler) JustPressed ¶
func (ih *InputHandler) JustPressed(button Button) bool
JustPressed returns whether the Button has been pressed in the last frame.
func (*InputHandler) JustReleased ¶
func (ih *InputHandler) JustReleased(button Button) bool
JustReleased returns whether the Button has been released in the last frame.
func (*InputHandler) MouseEnteredEvent ¶
func (ih *InputHandler) MouseEnteredEvent(entered bool)
MouseEnteredEvent is called when the mouse enters or leaves the window
func (*InputHandler) MouseInsideWindow ¶
func (ih *InputHandler) MouseInsideWindow() bool
MouseInsideWindow returns true if the mouse position is within the Window's Bounds
func (*InputHandler) MouseMoveEvent ¶
func (ih *InputHandler) MouseMoveEvent(pos Vec)
MouseMoveEvent sets the mouse position for the next update
func (*InputHandler) MousePosition ¶
func (ih *InputHandler) MousePosition() Vec
MousePosition returns the current mouse position in the Window's Bounds
func (*InputHandler) MousePreviousPosition ¶
func (ih *InputHandler) MousePreviousPosition() Vec
MousePreviousPosition returns the previous mouse position in the Window's Bounds
func (*InputHandler) MousePreviousScroll ¶
func (ih *InputHandler) MousePreviousScroll() Vec
MousePreviousScroll returns the previous mouse scroll amount (in both axes)
func (*InputHandler) MouseScroll ¶
func (ih *InputHandler) MouseScroll() Vec
MouseScroll returns the mouse scroll amount (in both axes) since the last update
func (*InputHandler) MouseScrollEvent ¶
func (ih *InputHandler) MouseScrollEvent(x, y float64)
MouseScrollEvent adds to the scroll offset for the next update
func (*InputHandler) Pressed ¶
func (ih *InputHandler) Pressed(button Button) bool
Pressed returns whether the Button is currently pressed down.
func (*InputHandler) Repeated ¶
func (ih *InputHandler) Repeated(button Button) bool
Repeated returns whether a repeat event has been triggered on button.
Repeat event occurs repeatedly when a button is held down for some time.
func (*InputHandler) SetMousePosition ¶
func (ih *InputHandler) SetMousePosition(pos Vec)
SetMousePosition overrides the mouse position Called when the mouse is set to a point in the backend Window
func (*InputHandler) Typed ¶
func (ih *InputHandler) Typed() string
Typed returns the text typed on the keyboard since the last update
func (*InputHandler) Update ¶
func (ih *InputHandler) Update()
type Joystick ¶
type Joystick int
Joystick is a joystick or controller (gamepad).
const UnknownJoystick Joystick = -1
List all of the joysticks.
type Line ¶
type Line struct {
A, B Vec
}
Line is a 2D line segment, between points A and B.
func (Line) Bounds ¶
Bounds returns the lines bounding box. This is in the form of a normalized Rect.
func (Line) Center ¶
Center will return the point at center of the line; that is, the point equidistant from either end.
func (Line) Closest ¶
Closest will return the point on the line which is closest to the Vec provided.
func (Line) Formula ¶
Formula will return the values that represent the line in the formula: y = mx + b This function will return math.Inf+, math.Inf- for a vertical line.
func (Line) Intersect ¶
Intersect will return the point of intersection for the two line segments. If the line segments do not intersect, this function will return the zero-vector and false.
func (Line) IntersectCircle ¶
IntersectCircle will return the shortest Vec such that moving the Line by that Vec will cause the Line and Circle to no longer intesect. If they do not intersect at all, this function will return a zero-vector.
func (Line) IntersectRect ¶
IntersectRect will return the shortest Vec such that moving the Line by that Vec will cause the Line and Rect to no longer intesect. If they do not intersect at all, this function will return a zero-vector.
type Matrix ¶
type Matrix [6]float64
Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such as movement, scaling and rotations.
Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For example:
pixel.IM.Moved(pixel.V(100, 200)).Rotated(pixel.ZV, math.Pi/2)
This code creates a Matrix that first moves everything by 100 units horizontally and 200 units vertically and then rotates everything by 90 degrees around the origin.
Layout is: [0] [2] [4] [1] [3] [5]
0 0 1 (implicit row)
func (Matrix) Chained ¶
Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied after the transformations of this Matrix.
func (Matrix) Project ¶
Project applies all transformations added to the Matrix to a vector u and returns the result.
Time complexity is O(1).
func (Matrix) Rotated ¶
Rotated rotates everything around a given point by the given angle in radians.
func (Matrix) ScaledXY ¶
ScaledXY scales everything around a given point by the scale factor in each axis respectively.
type Picture ¶
type Picture interface { // Bounds returns the rectangle of the Picture. All data is located within this rectangle. // Querying properties outside the rectangle should return default value of that property. Bounds() Rect }
Picture represents a rectangular area of raster data, such as a color. It has Bounds which specify the rectangle where data is located.
type PictureColor ¶
PictureColor specifies Picture with Color property, so that every position inside the Picture's Bounds has a color.
Positions outside the Picture's Bounds must return full transparent (Alpha(0)).
type PictureData ¶
PictureData specifies an in-memory rectangular area of pixels and implements Picture and PictureColor.
Pixels are small rectangles of unit size of form (x, y, x+1, y+1), where x and y are integers. PictureData contains and assigns a color to all pixels that are at least partially contained within it's Bounds (Rect).
The struct's innards are exposed for convenience, manual modification is at your own risk.
The format of the pixels is color.RGBA and not pixel.RGBA for a very serious reason: pixel.RGBA takes up 8x more memory than color.RGBA.
func MakePictureData ¶
func MakePictureData(rect Rect) *PictureData
MakePictureData creates a zero-initialized PictureData covering the given rectangle.
func PictureDataFromImage ¶
func PictureDataFromImage(img image.Image) *PictureData
PictureDataFromImage converts an image.Image into PictureData.
The resulting PictureData's Bounds will be the equivalent of the supplied image.Image's Bounds.
func PictureDataFromPicture ¶
func PictureDataFromPicture(pic Picture) *PictureData
PictureDataFromPicture converts an arbitrary Picture into PictureData (the conversion may be lossy, because PictureData works with unit-sized pixels).
Bounds are preserved.
func (*PictureData) Bounds ¶
func (pd *PictureData) Bounds() Rect
Bounds returns the bounds of this PictureData.
func (*PictureData) Color ¶
func (pd *PictureData) Color(at Vec) RGBA
Color returns the color located at the given position.
func (*PictureData) Image ¶
func (pd *PictureData) Image() *image.RGBA
Image converts PictureData into an image.RGBA.
The resulting image.RGBA's Bounds will be equivalent of the PictureData's Bounds.
func (*PictureData) Index ¶
func (pd *PictureData) Index(at Vec) int
Index returns the index of the pixel at the specified position inside the Pix slice.
type RGBA ¶
type RGBA struct {
R, G, B, A float64
}
RGBA represents an alpha-premultiplied RGBA color with components within range [0, 1].
The difference between color.RGBA is that the value range is [0, 1] and the values are floats.
func RGB ¶
RGB returns a fully opaque RGBA color with the given RGB values.
A common way to construct a transparent color is to create one with RGB constructor, then multiply it by a color obtained from the Alpha constructor.
func ToRGBA ¶
ToRGBA converts a color to RGBA format. Using this function is preferred to using RGBAModel, for performance (using RGBAModel introduces additional unnecessary allocations).
func (RGBA) Add ¶
Add adds color d to color c component-wise and returns the result (the components are not clamped).
func (RGBA) Mul ¶
Mul multiplies color c by color d component-wise (the components are not clamped).
func (RGBA) RGBA ¶
RGBA returns alpha-premultiplied red, green, blue and alpha components of the RGBA color.
type Rect ¶
type Rect struct {
Min, Max Vec
}
Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two points, Min and Max.
The invariant should hold, that Max's components are greater or equal than Min's components respectively.
func R ¶
R returns a new Rect with given the Min and Max coordinates.
Note that the returned rectangle is not automatically normalized.
func (Rect) Center ¶
Center returns the position of the center of the Rect. `rect.Center()` is equivalent to `rect.Anchor(pixel.Anchor.Center)`
func (Rect) Contains ¶
Contains checks whether a vector u is contained within this Rect (including it's borders).
func (Rect) Intersect ¶
Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.
If r and s don't overlap, this function returns a zero-rectangle.
func (Rect) IntersectCircle ¶
IntersectCircle returns a minimal required Vector, such that moving the rect by that vector would stop the Circle and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only the perimeters touch.
This function will return a non-zero vector if:
- The Rect contains the Circle, partially or fully
- The Circle contains the Rect, partially of fully
func (Rect) IntersectLine ¶
IntersectLine will return the shortest Vec such that if the Rect is moved by the Vec returned, the Line and Rect no longer intersect.
func (Rect) IntersectionPoints ¶
IntersectionPoints returns all the points where the Rect intersects with the line provided. This can be zero, one or two points, depending on the location of the shapes. The points of intersection will be returned in order of closest-to-l.A to closest-to-l.B.
func (Rect) Intersects ¶
Intersects returns whether or not the given Rect intersects at any point with this Rect.
This function is overall about 5x faster than Intersect, so it is better to use if you have no need for the returned Rect from Intersect.
func (Rect) Norm ¶
Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
func (Rect) Resized ¶
Resized returns the Rect resized to the given size while keeping the position of the given anchor.
r.Resized(r.Min, size) // resizes while keeping the position of the lower-left corner r.Resized(r.Max, size) // same with the top-right corner r.Resized(r.Center(), size) // resizes around the center
This function does not make sense for resizing a rectangle of zero area and will panic. Use ResizedMin in the case of zero area.
func (Rect) ResizedMin ¶
ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's Min.
Sizes of zero area are safe here.
func (Rect) String ¶
String returns the string representation of the Rect.
r := pixel.R(100, 50, 200, 300) r.String() // returns "Rect(100, 50, 200, 300)" fmt.Println(r) // Rect(100, 50, 200, 300)
func (Rect) Union ¶
Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.
type Sprite ¶
type Sprite struct {
// contains filtered or unexported fields
}
Sprite is a drawable frame of a Picture. It's anchored by the center of it's Picture's frame.
Frame specifies a rectangular portion of the Picture that will be drawn. For example, this creates a Sprite that draws the whole Picture:
sprite := pixel.NewSprite(pic, pic.Bounds())
Note, that Sprite caches the results of MakePicture from Targets it's drawn to for each Picture it's set to. What it means is that using a Sprite with an unbounded number of Pictures leads to a memory leak, since Sprite caches them and never forgets. In such a situation, create a new Sprite for each Picture.
func (*Sprite) Draw ¶
Draw draws the Sprite onto the provided Target. The Sprite will be transformed by the given Matrix.
This method is equivalent to calling DrawColorMask with nil color mask.
func (*Sprite) DrawColorMask ¶
DrawColorMask draws the Sprite onto the provided Target. The Sprite will be transformed by the given Matrix and all of it's color will be multiplied by the given mask.
If the mask is nil, a fully opaque white mask will be used, which causes no effect.
type Target ¶
type Target interface { // MakeTriangles generates a specialized copy of the provided Triangles. // // When calling Draw method on the returned TargetTriangles, the TargetTriangles will be // drawn onto the Target that generated them. // // Note, that not every Target has to recognize all possible types of Triangles. Some may // only recognize TrianglesPosition and TrianglesColor and ignore all other properties (if // present) when making new TargetTriangles. This varies from Target to Target. MakeTriangles(Triangles) TargetTriangles // MakePicture generates a specialized copy of the provided Picture. // // When calling Draw method on the returned TargetPicture, the TargetPicture will be drawn // onto the Target that generated it together with the TargetTriangles supplied to the Draw // method. MakePicture(Picture) TargetPicture }
Target is something that can be drawn onto, such as a window, a canvas, and so on.
You can notice, that there are no "drawing" methods in a Target. That's because all drawing happens indirectly through Triangles and Picture instances generated via MakeTriangles and MakePicture method.
type TargetPicture ¶
type TargetPicture interface { Picture // Draw draws the supplied TargetTriangles (which must be generated by the same Target as // this TargetPicture) with this TargetPicture. The TargetTriangles should utilize the data // from this TargetPicture in some way. Draw(TargetTriangles) }
TargetPicture is a Picture generated by a Target using MakePicture method. This Picture can be drawn onto that (no other) Target together with a TargetTriangles generated by the same Target.
The TargetTriangles specify where, shape and how the Picture should be drawn.
type TargetTriangles ¶
type TargetTriangles interface { Triangles // Draw draws Triangles onto an associated Target. Draw() }
TargetTriangles are Triangles generated by a Target with MakeTriangles method. They can be drawn onto that (no other) Target.
type Triangles ¶
type Triangles interface { // Len returns the number of vertices. The number of triangles is the number of vertices // divided by 3. Len() int // SetLen resizes Triangles to len vertices. If Triangles B were obtained by calling Slice // method on Triangles A, the relationship between A and B is undefined after calling SetLen // on either one of them. SetLen(len int) // Slice returns a sub-Triangles of this Triangles, covering vertices in range [i, j). // // If Triangles B were obtained by calling Slice(4, 9) on Triangles A, then A and B must // share the same underlying data. Modifying B must change the contents of A in range // [4, 9). The vertex with index 0 at B is the vertex with index 4 in A, and so on. // // Returned Triangles must have the same underlying type. Slice(i, j int) Triangles // Update copies vertex properties from the supplied Triangles into this Triangles. // // Properies not supported by these Triangles should be ignored. Properties not supported by // the supplied Triangles should be left untouched. // // The two Triangles must have the same Len. Update(Triangles) // Copy creates an exact independent copy of this Triangles (with the same underlying type). Copy() Triangles }
Triangles represents a list of vertices, where each three vertices form a triangle. (First, second and third is the first triangle, fourth, fifth and sixth is the second triangle, etc.)
type TrianglesClipped ¶
TrianglesClipped specifies Triangles with Clipping Rectangle property.
The first value returned from ClipRect method is the clipping rectangle. The second one specifies if the triangle is clipped.
type TrianglesColor ¶
TrianglesColor specifies Triangles with Color property.
type TrianglesData ¶
type TrianglesData []struct { Position Vec Color RGBA Picture Vec Intensity float64 ClipRect Rect IsClipped bool }
TrianglesData specifies a list of Triangles vertices with three common properties: TrianglesPosition, TrianglesColor and TrianglesPicture.
func MakeTrianglesData ¶
func MakeTrianglesData(len int) *TrianglesData
MakeTrianglesData creates TrianglesData of length len initialized with default property values.
Prefer this function to make(TrianglesData, len), because make zeros them, while this function does the correct intialization.
func (*TrianglesData) ClipRect ¶
func (td *TrianglesData) ClipRect(i int) (rect Rect, has bool)
ClipRect returns the clipping rectangle property of the i-th vertex.
func (*TrianglesData) Color ¶
func (td *TrianglesData) Color(i int) RGBA
Color returns the color property of i-th vertex.
func (*TrianglesData) Copy ¶
func (td *TrianglesData) Copy() Triangles
Copy returns an exact independent copy of this TrianglesData.
func (*TrianglesData) Len ¶
func (td *TrianglesData) Len() int
Len returns the number of vertices in TrianglesData.
func (*TrianglesData) Picture ¶
func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64)
Picture returns the picture property of i-th vertex.
func (*TrianglesData) Position ¶
func (td *TrianglesData) Position(i int) Vec
Position returns the position property of i-th vertex.
func (*TrianglesData) SetLen ¶
func (td *TrianglesData) SetLen(len int)
SetLen resizes TrianglesData to len, while keeping the original content.
If len is greater than TrianglesData's current length, the new data is filled with default values ((0, 0), white, (0, 0), 0).
func (*TrianglesData) Slice ¶
func (td *TrianglesData) Slice(i, j int) Triangles
Slice returns a sub-Triangles of this TrianglesData.
func (*TrianglesData) Update ¶
func (td *TrianglesData) Update(t Triangles)
Update copies vertex properties from the supplied Triangles into this TrianglesData.
TrianglesPosition, TrianglesColor and TrianglesTexture are supported.
type TrianglesPicture ¶
TrianglesPicture specifies Triangles with Picture property.
The first value returned from Picture method is Picture coordinates. The second one specifies the weight of the Picture. Value of 0 means, that Picture should be completely ignored, 1 means that is should be fully included and anything in between means anything in between.
type TrianglesPosition ¶
TrianglesPosition specifies Triangles with Position property.
type Vec ¶
type Vec struct {
X, Y float64
}
Vec is a 2D vector type with X and Y coordinates.
Create vectors with the V constructor:
u := pixel.V(1, 2) v := pixel.V(8, -3)
Use various methods to manipulate them:
w := u.Add(v) fmt.Println(w) // Vec(9, -1) fmt.Println(u.Sub(v)) // Vec(-7, 5) u = pixel.V(2, 3) v = pixel.V(8, 1) if u.X < 0 { fmt.Println("this won't happen") } x := u.Unit().Dot(v.Unit())
func Lerp ¶
Lerp returns a linear interpolation between vectors a and b.
This function basically returns a point along the line between a and b and t chooses which one. If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will return the appropriate point between a and b and so on.
func (Vec) Angle ¶
Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].
func (Vec) Eq ¶
Eq will compare two vectors and return whether they are equal accounting for rounding errors. At worst, the result is correct to 7 significant digits.
func (Vec) Map ¶
Map applies the function f to both x and y components of the vector u and returns the modified vector.
u := pixel.V(10.5, -1.5) v := u.Map(math.Floor) // v is Vec(10, -2), both components of u floored
func (Vec) Normal ¶
Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.
func (Vec) Project ¶
Project returns a projection (or component) of vector u in the direction of vector v.
Behaviour is undefined if v is a zero vector.
func (Vec) String ¶
String returns the string representation of the vector u.
u := pixel.V(4.5, -1.3) u.String() // returns "Vec(4.5, -1.3)" fmt.Println(u) // Vec(4.5, -1.3)
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
backends
|
|
opengl
Package opengl implements efficient OpenGL targets and utilities for the Pixel game development library, specifically Window and Canvas.
|
Package opengl implements efficient OpenGL targets and utilities for the Pixel game development library, specifically Window and Canvas. |
ext
|
|
imdraw
Package imdraw implements a basic primitive geometry shape and pictured polygon drawing for Pixel with a nice immediate-mode-like API.
|
Package imdraw implements a basic primitive geometry shape and pictured polygon drawing for Pixel with a nice immediate-mode-like API. |
text
Package text implements efficient text drawing for the Pixel library.
|
Package text implements efficient text drawing for the Pixel library. |