From cee897e1a560a6c988304230dd674dde72634b4e Mon Sep 17 00:00:00 2001 From: faiface Date: Thu, 23 Mar 2017 19:59:03 +0100 Subject: [PATCH] add Sprite.SetMatrix and Sprite.SetColorMask --- sprite.go | 58 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/sprite.go b/sprite.go index f90fa90..190c469 100644 --- a/sprite.go +++ b/sprite.go @@ -1,10 +1,16 @@ package pixel -// Sprite is a drawable Picture. It's always anchored by the center of it's Picture. +// Sprite is a drawable Picture. It's anchored by the center of it's Picture. +// +// To achieve different anchoring, transformations and color masking, use SetMatrix and SetColorMask +// methods. type Sprite struct { tri *TrianglesData bounds Rect d Drawer + + matrix Matrix + mask NRGBA } // NewSprite creates a Sprite from the supplied Picture. @@ -14,6 +20,8 @@ func NewSprite(pic Picture) *Sprite { tri: tri, d: Drawer{Triangles: tri}, } + s.matrix = IM + s.mask = NRGBA{1, 1, 1, 1} s.SetPicture(pic) return s } @@ -28,6 +36,40 @@ func (s *Sprite) SetPicture(pic Picture) { } s.bounds = pic.Bounds() + s.calcData() +} + +// Picture returns the current Sprite's Picture. +func (s *Sprite) Picture() Picture { + return s.d.Picture +} + +// SetMatrix sets a Matrix that this Sprite will be transformed by. This overrides any previously +// set Matrix. +// +// Note, that this has nothing to do with BasicTarget's SetMatrix method. This only affects this +// Sprite and is usable with any Target. +func (s *Sprite) SetMatrix(matrix Matrix) { + s.matrix = matrix + s.calcData() +} + +// SetColorMask sets a color that this Sprite will be multiplied by. This overrides any previously +// set color mask. +// +// Note, that this has nothing to do with BasicTarget's SetColorMask method. This only affects this +// Sprite and is usable with any Target. +func (s *Sprite) SetColorMask(mask NRGBA) { + s.mask = mask + s.calcData() +} + +// Draw draws the Sprite onto the provided Target. +func (s *Sprite) Draw(t Target) { + s.d.Draw(t) +} + +func (s *Sprite) calcData() { var ( center = s.bounds.Center() horizontal = X(s.bounds.W() / 2) @@ -42,20 +84,12 @@ func (s *Sprite) SetPicture(pic Picture) { (*s.tri)[5].Position = -horizontal + vertical for i := range *s.tri { - (*s.tri)[i].Color = NRGBA{1, 1, 1, 1} + (*s.tri)[i].Color = s.mask (*s.tri)[i].Picture = center + (*s.tri)[i].Position (*s.tri)[i].Intensity = 1 + + (*s.tri)[i].Position = s.matrix.Project((*s.tri)[i].Position) } s.d.Dirty() } - -// Picture returns the current Sprite's Picture. -func (s *Sprite) Picture() Picture { - return s.d.Picture -} - -// Draw draws the Sprite onto the provided Target. -func (s *Sprite) Draw(t Target) { - s.d.Draw(t) -}