adapt Batch to Picture
This commit is contained in:
parent
bffb3598c7
commit
7e09988467
120
batch.go
120
batch.go
|
@ -2,6 +2,7 @@ package pixel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/go-gl/mathgl/mgl32"
|
"github.com/go-gl/mathgl/mgl32"
|
||||||
)
|
)
|
||||||
|
@ -12,30 +13,29 @@ import (
|
||||||
// To put an object into a Batch, just draw it onto it:
|
// To put an object into a Batch, just draw it onto it:
|
||||||
// object.Draw(batch)
|
// object.Draw(batch)
|
||||||
type Batch struct {
|
type Batch struct {
|
||||||
cont TrianglesDrawer
|
cont Drawer
|
||||||
fixpic *GLPicture
|
|
||||||
|
|
||||||
pic *GLPicture
|
|
||||||
mat mgl32.Mat3
|
mat mgl32.Mat3
|
||||||
col NRGBA
|
col NRGBA
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ BasicTarget = (*Batch)(nil)
|
||||||
|
|
||||||
// NewBatch creates an empty Batch with the specified Picture and container.
|
// NewBatch creates an empty Batch with the specified Picture and container.
|
||||||
//
|
//
|
||||||
// The container is where objects get accumulated. Batch will support precisely those vertex
|
// The container is where objects get accumulated. Batch will support precisely those vertex
|
||||||
// properties, that the supplied container supports.
|
// properties, that the supplied container supports.
|
||||||
//
|
//
|
||||||
// Note, that if the container does not support TrianglesColor, color masking will not work.
|
// Note, that if the container does not support TrianglesColor, color masking will not work.
|
||||||
func NewBatch(pic *GLPicture, container Triangles) *Batch {
|
func NewBatch(container Triangles, pic Picture) *Batch {
|
||||||
return &Batch{
|
return &Batch{
|
||||||
cont: TrianglesDrawer{Triangles: container},
|
cont: Drawer{Triangles: container, Picture: pic},
|
||||||
fixpic: pic,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear removes all objects from the Batch.
|
// Clear removes all objects from the Batch.
|
||||||
func (b *Batch) Clear() {
|
func (b *Batch) Clear() {
|
||||||
b.cont.SetLen(0)
|
b.cont.Triangles.SetLen(0)
|
||||||
b.cont.Dirty()
|
b.cont.Dirty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,33 +44,13 @@ func (b *Batch) Draw(t Target) {
|
||||||
b.cont.Draw(t)
|
b.cont.Draw(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeTriangles returns a specialized copy of the provided Triangles, that draws onto this Batch.
|
|
||||||
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles {
|
|
||||||
return &batchTriangles{
|
|
||||||
Triangles: t.Copy(),
|
|
||||||
trans: t.Copy(),
|
|
||||||
data: MakeTrianglesData(t.Len()),
|
|
||||||
batch: b,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPicture sets the current Picture that will be used with the following draws. The original
|
|
||||||
// Picture of this Picture (the one from which p was obtained by slicing) must be same as the
|
|
||||||
// original Picture of the Batch's Picture.
|
|
||||||
func (b *Batch) SetPicture(p *GLPicture) {
|
|
||||||
if p != nil && p.Texture() != b.fixpic.Texture() {
|
|
||||||
panic("batch: attempted to draw with a different underlying Picture")
|
|
||||||
}
|
|
||||||
b.pic = p
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTransform sets transforms used in the following draws onto the Batch.
|
// SetTransform sets transforms used in the following draws onto the Batch.
|
||||||
func (b *Batch) SetTransform(t ...Transform) {
|
func (b *Batch) SetTransform(t ...Transform) {
|
||||||
b.mat = transformToMat(t...)
|
b.mat = transformToMat(t...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaskColor sets a mask color used in the following draws onto the Batch.
|
// SetColorMask sets a mask color used in the following draws onto the Batch.
|
||||||
func (b *Batch) SetMaskColor(c color.Color) {
|
func (b *Batch) SetColorMask(c color.Color) {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
b.col = NRGBA{1, 1, 1, 1}
|
b.col = NRGBA{1, 1, 1, 1}
|
||||||
return
|
return
|
||||||
|
@ -78,33 +58,69 @@ func (b *Batch) SetMaskColor(c color.Color) {
|
||||||
b.col = NRGBAModel.Convert(c).(NRGBA)
|
b.col = NRGBAModel.Convert(c).(NRGBA)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch.
|
||||||
|
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles {
|
||||||
|
bt := &batchTriangles{
|
||||||
|
Triangles: t.Copy(),
|
||||||
|
orig: MakeTrianglesData(t.Len()),
|
||||||
|
trans: MakeTrianglesData(t.Len()),
|
||||||
|
b: b,
|
||||||
|
}
|
||||||
|
bt.orig.Update(t)
|
||||||
|
bt.trans.Update(&bt.orig)
|
||||||
|
return bt
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakePicture returns a specialized copy of the provided Picture that draws onto this Batch.
|
||||||
|
func (b *Batch) MakePicture(p Picture) TargetPicture {
|
||||||
|
return &batchPicture{
|
||||||
|
Picture: p,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type batchTriangles struct {
|
type batchTriangles struct {
|
||||||
Triangles
|
Triangles
|
||||||
trans Triangles
|
orig, trans TrianglesData
|
||||||
data TrianglesData
|
|
||||||
|
|
||||||
batch *Batch
|
b *Batch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bt *batchTriangles) draw(bp *batchPicture) {
|
||||||
|
for i := range bt.trans {
|
||||||
|
transPos := bt.b.mat.Mul3x1(mgl32.Vec3{
|
||||||
|
float32(bt.orig[i].Position.X()),
|
||||||
|
float32(bt.orig[i].Position.Y()),
|
||||||
|
1,
|
||||||
|
})
|
||||||
|
bt.trans[i].Position = V(float64(transPos.X()), float64(transPos.Y()))
|
||||||
|
bt.trans[i].Color = bt.orig[i].Color.Mul(bt.b.col)
|
||||||
|
if bp == nil {
|
||||||
|
bt.trans[i].Picture = V(math.Inf(+1), math.Inf(+1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bt.Triangles.Update(&bt.trans)
|
||||||
|
|
||||||
|
cont := bt.b.cont.Triangles
|
||||||
|
cont.SetLen(cont.Len() + bt.Triangles.Len())
|
||||||
|
cont.Slice(cont.Len()-bt.Triangles.Len(), cont.Len()).Update(bt.Triangles)
|
||||||
|
bt.b.cont.Dirty()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bt *batchTriangles) Draw() {
|
func (bt *batchTriangles) Draw() {
|
||||||
// need to apply transforms and mask color and picture bounds
|
bt.draw(nil)
|
||||||
bt.data.Update(bt.Triangles)
|
}
|
||||||
for i := range bt.data {
|
|
||||||
transPos := bt.batch.mat.Mul3x1(mgl32.Vec3{
|
type batchPicture struct {
|
||||||
float32(bt.data[i].Position.X()),
|
Picture
|
||||||
float32(bt.data[i].Position.Y()),
|
}
|
||||||
1,
|
|
||||||
})
|
func (bp *batchPicture) Slice(r Rect) Picture {
|
||||||
bt.data[i].Position = V(float64(transPos.X()), float64(transPos.Y()))
|
return &batchPicture{
|
||||||
bt.data[i].Color = bt.data[i].Color.Mul(bt.batch.col)
|
Picture: bp.Picture.Slice(r),
|
||||||
if bt.batch.pic != nil && bt.data[i].Picture != V(-1, -1) {
|
}
|
||||||
bt.data[i].Picture = pictureBounds(bt.batch.pic, bt.data[i].Picture)
|
}
|
||||||
}
|
|
||||||
}
|
func (bp *batchPicture) Draw(t TargetTriangles) {
|
||||||
bt.trans.Update(&bt.data)
|
t.(*batchTriangles).draw(bp)
|
||||||
|
|
||||||
cont := bt.batch.cont
|
|
||||||
cont.SetLen(cont.Len() + bt.trans.Len())
|
|
||||||
cont.Slice(cont.Len()-bt.trans.Len(), cont.Len()).Update(bt.trans)
|
|
||||||
cont.Dirty()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue