| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import "image/color" |
| 7 |
|
| 8 |
// Batch is a Target that allows for efficient drawing of many objects with the same Picture. |
| 9 |
// |
| 10 |
// To put an object into a Batch, just draw it onto it: |
| 11 |
// |
| 12 |
// object.Draw(batch) |
| 13 |
type Batch struct { |
| 14 |
cont Drawer |
| 15 |
|
| 16 |
mat Matrix |
| 17 |
col color.Color |
| 18 |
} |
| 19 |
|
| 20 |
var _ BasicTarget = (*Batch)(nil) |
| 21 |
|
| 22 |
// NewBatch creates an empty Batch with the specified Picture and container. |
| 23 |
// |
| 24 |
// The container is where objects get accumulated. Batch will support precisely those Triangles |
| 25 |
// properties, that the supplied container supports. If you retain access to the container and |
| 26 |
// change it, call Dirty to notify Batch about the change. |
| 27 |
// |
| 28 |
// Note, that if the container does not support TrianglesColor, color masking will not work. |
| 29 |
func NewBatch(container Triangles, pic Picture) *Batch { |
| 30 |
b := &Batch{cont: Drawer{Triangles: container, Picture: pic}} |
| 31 |
b.SetMatrix(IM) |
| 32 |
return b |
| 33 |
} |
| 34 |
|
| 35 |
// Dirty notifies Batch about an external modification of it's container. If you retain access to |
| 36 |
// the Batch's container and change it, call Dirty to notify Batch about the change. |
| 37 |
// |
| 38 |
// container := &gfx.TrianglesData{} |
| 39 |
// batch := gfx.NewBatch(container, nil) |
| 40 |
// container.SetLen(10) // container changed from outside of Batch |
| 41 |
// batch.Dirty() // notify Batch about the change |
| 42 |
func (b *Batch) Dirty() { |
| 43 |
b.cont.Dirty() |
| 44 |
} |
| 45 |
|
| 46 |
// Clear removes all objects from the Batch. |
| 47 |
func (b *Batch) Clear() { |
| 48 |
b.cont.Triangles.SetLen(0) |
| 49 |
b.cont.Dirty() |
| 50 |
} |
| 51 |
|
| 52 |
// Draw draws all objects that are currently in the Batch onto another Target. |
| 53 |
func (b *Batch) Draw(t Target) { |
| 54 |
b.cont.Draw(t) |
| 55 |
} |
| 56 |
|
| 57 |
// SetMatrix sets a Matrix that every point will be projected by. |
| 58 |
func (b *Batch) SetMatrix(m Matrix) { |
| 59 |
b.mat = m |
| 60 |
} |
| 61 |
|
| 62 |
// MakeTriangles returns a specialized copy of the provided Triangles that draws onto this Batch. |
| 63 |
func (b *Batch) MakeTriangles(t Triangles) TargetTriangles { |
| 64 |
bt := &batchTriangles{ |
| 65 |
tri: t.Copy(), |
| 66 |
tmp: MakeTrianglesData(t.Len()), |
| 67 |
dst: b, |
| 68 |
} |
| 69 |
return bt |
| 70 |
} |
| 71 |
|
| 72 |
// MakePicture returns a specialized copy of the provided Picture that draws onto this Batch. |
| 73 |
func (b *Batch) MakePicture(p Picture) TargetPicture { |
| 74 |
if p != b.cont.Picture { |
| 75 |
panic(Errorf("(%T).MakePicture: Picture is not the Batch's Picture", b)) |
| 76 |
} |
| 77 |
bp := &batchPicture{ |
| 78 |
pic: p, |
| 79 |
dst: b, |
| 80 |
} |
| 81 |
return bp |
| 82 |
} |
| 83 |
|
| 84 |
type batchTriangles struct { |
| 85 |
tri Triangles |
| 86 |
tmp *TrianglesData |
| 87 |
dst *Batch |
| 88 |
} |
| 89 |
|
| 90 |
func (bt *batchTriangles) Len() int { |
| 91 |
return bt.tri.Len() |
| 92 |
} |
| 93 |
|
| 94 |
func (bt *batchTriangles) SetLen(len int) { |
| 95 |
bt.tri.SetLen(len) |
| 96 |
bt.tmp.SetLen(len) |
| 97 |
} |
| 98 |
|
| 99 |
func (bt *batchTriangles) Slice(i, j int) Triangles { |
| 100 |
return &batchTriangles{ |
| 101 |
tri: bt.tri.Slice(i, j), |
| 102 |
tmp: bt.tmp.Slice(i, j).(*TrianglesData), |
| 103 |
dst: bt.dst, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func (bt *batchTriangles) Update(t Triangles) { |
| 108 |
bt.tri.Update(t) |
| 109 |
} |
| 110 |
|
| 111 |
func (bt *batchTriangles) Copy() Triangles { |
| 112 |
return &batchTriangles{ |
| 113 |
tri: bt.tri.Copy(), |
| 114 |
tmp: bt.tmp.Copy().(*TrianglesData), |
| 115 |
dst: bt.dst, |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func (bt *batchTriangles) draw(_ *batchPicture) { |
| 120 |
bt.tmp.Update(bt.tri) |
| 121 |
|
| 122 |
for i := range *bt.tmp { |
| 123 |
(*bt.tmp)[i].Position = bt.dst.mat.Project((*bt.tmp)[i].Position) |
| 124 |
//(*bt.tmp)[i].Color = bt.dst.col.Mul((*bt.tmp)[i].Color) |
| 125 |
} |
| 126 |
|
| 127 |
cont := bt.dst.cont.Triangles |
| 128 |
cont.SetLen(cont.Len() + bt.tri.Len()) |
| 129 |
added := cont.Slice(cont.Len()-bt.tri.Len(), cont.Len()) |
| 130 |
added.Update(bt.tri) |
| 131 |
added.Update(bt.tmp) |
| 132 |
bt.dst.cont.Dirty() |
| 133 |
} |
| 134 |
|
| 135 |
func (bt *batchTriangles) Draw() { |
| 136 |
bt.draw(nil) |
| 137 |
} |
| 138 |
|
| 139 |
type batchPicture struct { |
| 140 |
pic Picture |
| 141 |
dst *Batch |
| 142 |
} |
| 143 |
|
| 144 |
func (bp *batchPicture) Bounds() Rect { |
| 145 |
return bp.pic.Bounds() |
| 146 |
} |
| 147 |
|
| 148 |
func (bp *batchPicture) Draw(t TargetTriangles) { |
| 149 |
bt := t.(*batchTriangles) |
| 150 |
if bp.dst != bt.dst { |
| 151 |
panic(Errorf("(%T).Draw: TargetTriangles generated by different Batch", bp)) |
| 152 |
} |
| 153 |
bt.draw(bp) |
| 154 |
} |