add Copy method to Triangles interface

This commit is contained in:
faiface 2017-01-12 11:44:54 +01:00
parent 6587b54040
commit f7fe00fe4d
4 changed files with 53 additions and 6 deletions

View File

@ -79,6 +79,13 @@ func (td *TrianglesData) Append(t Triangles) {
*td = append(*td, newTd...)
}
// Copy returns an exact independent copy of this TrianglesData.
func (td *TrianglesData) Copy() Triangles {
copyTd := make(TrianglesData, td.Len())
copyTd.Update(td)
return &copyTd
}
// Position returns the position property of i-th vertex.
func (td *TrianglesData) Position(i int) Vec {
return (*td)[i].Position
@ -117,6 +124,11 @@ func (td *TrianglesColorData) Append(t Triangles) {
(*TrianglesData)(td).Append(t)
}
// Copy returns an exact independent copy of this TrianglesColorData.
func (td *TrianglesColorData) Copy() Triangles {
return (*TrianglesColorData)((*TrianglesData)(td).Copy().(*TrianglesData))
}
// Position returns the position property of i-th vertex.
func (td *TrianglesColorData) Position(i int) Vec {
return (*TrianglesData)(td).Position(i)
@ -150,6 +162,11 @@ func (td *TrianglesTextureData) Append(t Triangles) {
(*TrianglesData)(td).Append(t)
}
// Copy returns an exact independent copy of this TrianglesTextureData.
func (td *TrianglesTextureData) Copy() Triangles {
return (*TrianglesTextureData)((*TrianglesData)(td).Copy().(*TrianglesData))
}
// Position returns the position property of i-th vertex.
func (td *TrianglesTextureData) Position(i int) Vec {
return (*TrianglesData)(td).Position(i)
@ -198,13 +215,24 @@ func (td *TrianglesDrawer) Draw(target Target) {
tri.Draw()
}
// Update updates the wrapped Triangles with the supplied Triangles. Call only this method to
// update the wrapped Triangles, otherwise the TrianglesDrawer will not work correctly.
// Update updates the wrapped Triangles with the supplied Triangles.
//
// Call only this method to update the wrapped Triangles, otherwise the TrianglesDrawer will not
// work correctly.
func (td *TrianglesDrawer) Update(t Triangles) {
td.dirty = true
td.Triangles.Update(t)
}
// Append appends the supplied Triangles to the wrapped Triangles.
//
// Call only this method to append to the wrapped Triangles, otherwise the TrianglesDrawer will not
// work correctly.
func (td *TrianglesDrawer) Append(t Triangles) {
td.dirty = true
td.Triangles.Append(t)
}
// Sprite is a picture, positioned somewhere, with an optional mask color.
type Sprite struct {
td TrianglesDrawer

View File

@ -49,6 +49,9 @@ type Triangles interface {
//
// Behavior regarding unsupported properties should be same as with Update.
Append(Triangles)
// Copy creates an exact independent copy of this Triangles (with the same underlying type).
Copy() Triangles
}
// Drawer is something that can be drawn onto any Target.

10
util.go
View File

@ -1,5 +1,7 @@
package pixel
import "github.com/go-gl/mathgl/mgl32"
func clamp(x, low, high float64) float64 {
if x < low {
return low
@ -9,3 +11,11 @@ func clamp(x, low, high float64) float64 {
}
return x
}
func transformToMat(t ...Transform) mgl32.Mat3 {
mat := mgl32.Ident3()
for i := range t {
mat = mat.Mul3(t[i].Mat())
}
return mat
}

View File

@ -470,6 +470,15 @@ func (wt *windowTriangles) Append(t Triangles) {
wt.submitData()
}
func (wt *windowTriangles) Copy() Triangles {
copyWt := &windowTriangles{
w: wt.w,
vs: pixelgl.MakeVertexSlice(wt.w.shader, 0, 0),
}
copyWt.Update(wt)
return copyWt
}
func (wt *windowTriangles) Position(i int) Vec {
v := wt.data[i][positionVec2].(mgl32.Vec2)
return V(float64(v.X()), float64(v.Y()))
@ -512,10 +521,7 @@ func (w *Window) SetPicture(p *Picture) {
//
// Transforms are applied right-to-left.
func (w *Window) SetTransform(t ...Transform) {
w.mat = mgl32.Ident3()
for i := range t {
w.mat = w.mat.Mul3(t[i].Mat())
}
w.mat = transformToMat(t...)
}
// SetMaskColor sets a global mask color for the Window.