Merge pull request #297 from zergon321/add-sprite-uncached

Added an opportunity to disable caching for sprites
This commit is contained in:
Allen Ray 2021-10-04 18:50:28 -04:00 committed by GitHub
commit 389317124e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.
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

View File

@ -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)

View File

@ -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

View File

@ -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()