Cached parameter added.

This commit is contained in:
zergon321 2021-10-01 18:31:18 +00:00
parent d119f130f6
commit 33024feabf
4 changed files with 15 additions and 3 deletions

View File

@ -26,7 +26,7 @@ var _ BasicTarget = (*Batch)(nil)
// //
// 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(container Triangles, pic Picture) *Batch { func NewBatch(container Triangles, pic Picture) *Batch {
b := &Batch{cont: Drawer{Triangles: container, Picture: pic}} b := &Batch{cont: Drawer{Triangles: container, Picture: pic, Cached: true}}
b.SetMatrix(IM) b.SetMatrix(IM)
b.SetColorMask(Alpha(1)) b.SetColorMask(Alpha(1))
return b return b

View File

@ -23,6 +23,7 @@ package pixel
type Drawer struct { type Drawer struct {
Triangles Triangles Triangles Triangles
Picture Picture Picture Picture
Cached bool
targets map[Target]*drawerTarget targets map[Target]*drawerTarget
allTargets []*drawerTarget allTargets []*drawerTarget
@ -91,7 +92,10 @@ func (d *Drawer) Draw(t Target) {
pic := dt.pics[d.Picture] pic := dt.pics[d.Picture]
if pic == nil { if pic == nil {
pic = t.MakePicture(d.Picture) pic = t.MakePicture(d.Picture)
dt.pics[d.Picture] = pic
if d.Cached {
dt.pics[d.Picture] = pic
}
} }
pic.Draw(dt.tris) pic.Draw(dt.tris)

View File

@ -27,7 +27,7 @@ func NewSprite(pic Picture, frame Rect) *Sprite {
tri := MakeTrianglesData(6) tri := MakeTrianglesData(6)
s := &Sprite{ s := &Sprite{
tri: tri, tri: tri,
d: Drawer{Triangles: tri}, d: Drawer{Triangles: tri, Cached: true},
} }
s.matrix = IM s.matrix = IM
s.mask = Alpha(1) s.mask = Alpha(1)
@ -44,6 +44,13 @@ func (s *Sprite) Set(pic Picture, frame Rect) {
} }
} }
// SetCached makes the sprite cache all the
// incoming pictures if the argument is true, and
// doesn't make it do that if the argument is false.
func (s *Sprite) SetCached(cached bool) {
s.d.Cached = cached
}
// Picture returns the current Sprite's Picture. // Picture returns the current Sprite's Picture.
func (s *Sprite) Picture() Picture { func (s *Sprite) Picture() Picture {
return s.d.Picture return s.d.Picture

View File

@ -136,6 +136,7 @@ func New(orig pixel.Vec, atlas *Atlas) *Text {
txt.transD.Picture = txt.atlas.pic txt.transD.Picture = txt.atlas.pic
txt.transD.Triangles = &txt.trans txt.transD.Triangles = &txt.trans
txt.transD.Cached = true
txt.Clear() txt.Clear()