From 33024feabf787547f8252eabf324fcde4e4881ce Mon Sep 17 00:00:00 2001 From: zergon321 Date: Fri, 1 Oct 2021 18:31:18 +0000 Subject: [PATCH] Cached parameter added. --- batch.go | 2 +- drawer.go | 6 +++++- sprite.go | 9 ++++++++- text/text.go | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/batch.go b/batch.go index b1b0ec7..9cf21c4 100644 --- a/batch.go +++ b/batch.go @@ -26,7 +26,7 @@ var _ BasicTarget = (*Batch)(nil) // // Note, that if the container does not support TrianglesColor, color masking will not work. 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.SetColorMask(Alpha(1)) return b diff --git a/drawer.go b/drawer.go index 408f807..117e8e3 100644 --- a/drawer.go +++ b/drawer.go @@ -23,6 +23,7 @@ package pixel type Drawer struct { Triangles Triangles Picture Picture + Cached bool targets map[Target]*drawerTarget allTargets []*drawerTarget @@ -91,7 +92,10 @@ func (d *Drawer) Draw(t Target) { pic := dt.pics[d.Picture] if pic == nil { pic = t.MakePicture(d.Picture) - dt.pics[d.Picture] = pic + + if d.Cached { + dt.pics[d.Picture] = pic + } } pic.Draw(dt.tris) diff --git a/sprite.go b/sprite.go index 017d985..7042c40 100644 --- a/sprite.go +++ b/sprite.go @@ -27,7 +27,7 @@ func NewSprite(pic Picture, frame Rect) *Sprite { tri := MakeTrianglesData(6) s := &Sprite{ tri: tri, - d: Drawer{Triangles: tri}, + d: Drawer{Triangles: tri, Cached: true}, } s.matrix = IM 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. func (s *Sprite) Picture() Picture { return s.d.Picture diff --git a/text/text.go b/text/text.go index 84365c2..8e047a9 100644 --- a/text/text.go +++ b/text/text.go @@ -136,6 +136,7 @@ func New(orig pixel.Vec, atlas *Atlas) *Text { txt.transD.Picture = txt.atlas.pic txt.transD.Triangles = &txt.trans + txt.transD.Cached = true txt.Clear()