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 ¶
- Variables
- type BasicTarget
- type Batch
- type ComposeMethod
- type ComposeTarget
- type Drawer
- 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 (r Rect) Center() Vec
- func (r Rect) Contains(u Vec) bool
- func (r Rect) H() float64
- 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) W() float64
- func (r Rect) WithMax(max Vec) Rect
- func (r Rect) WithMin(min Vec) Rect
- type Sprite
- type Target
- type TargetPicture
- type TargetTriangles
- type Triangles
- type TrianglesColor
- type TrianglesData
- 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) Angle() float64
- func (u Vec) Cross(v Vec) float64
- func (u Vec) Dot(v Vec) float64
- func (u Vec) Len() float64
- func (u Vec) Map(f func(float64) float64) 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) String() string
- func (u Vec) Unit() Vec
- func (u Vec) WithX(x float64) Vec
- func (u Vec) WithY(y float64) Vec
- func (u Vec) X() float64
- func (u Vec) XY() (x, y float64)
- func (u Vec) Y() float64
Constants ¶
This section is empty.
Variables ¶
var IM = Matrix(mgl64.Ident3())
IM stands for identity matrix. Does nothing, no transformation.
var RGBAModel = color.ModelFunc(rgbaModel)
RGBAModel converts colors to RGBA format.
Functions ¶
This section is empty.
Types ¶
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 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 // 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 Matrix ¶
type Matrix [9]float64
Matrix is a 3x3 transformation matrix that can be used for all kinds of spacial 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(0, 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.
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 witih 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 introduced 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 (Rect) Contains ¶
Contains checks whether a vector u is contained within this Rect (including it's borders).
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 sizes 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)
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())
To achieve different anchoring, transformations and color masking, use SetMatrix and SetColorMask methods.
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) SetColorMask ¶
SetColorMask sets a color that this Sprite will be multiplied by. This overrides any previously set color mask.
Note, that this has nothing to do with BasicTarget's SetColorMask method. This only affects this Sprite and is usable with any Target.
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 TrianglesColor ¶
TrianglesColor specifies Triangles with Color property.
type TrianglesData ¶
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) 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 propery.
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 complex128
Vec is a 2D vector type. It is unusually implemented as complex128 for convenience. Since Go does not allow operator overloading, implementing vector as a struct leads to a bunch of methods for addition, subtraction and multiplication of vectors. With complex128, much of this functionality is given through operators.
Create vectors with the V constructor:
u := pixel.V(1, 2) v := pixel.V(8, -3)
Add and subtract them using the standard + and - operators:
w := u + v fmt.Println(w) // Vec(9, -1) fmt.Println(u - v) // Vec(-7, 5)
Additional standard vector operations can be obtained with methods:
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) 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) 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 |
---|---|
examples
|
|
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. |
Package pixelgl implements efficient OpenGL targets and utilities for the Pixel game development library, specifically Window and Canvas.
|
Package pixelgl implements efficient OpenGL targets and utilities for the Pixel game development library, specifically Window and Canvas. |