From 055da8dfbb73e793e1baa141cf0c00bbd6729877 Mon Sep 17 00:00:00 2001
From: faiface <faiface@ksp.sk>
Date: Tue, 7 Mar 2017 22:47:55 +0100
Subject: [PATCH] add Sprite

---
 graphics.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 graphics.go

diff --git a/graphics.go b/graphics.go
new file mode 100644
index 0000000..70f3dd4
--- /dev/null
+++ b/graphics.go
@@ -0,0 +1,53 @@
+package pixel
+
+// Sprite is a drawable Picture. It's always anchored by the center of it's Picture.
+type Sprite struct {
+	tri *TrianglesData
+	d   Drawer
+}
+
+// NewSprite creates a Sprite from the supplied Picture.
+func NewSprite(pic Picture) *Sprite {
+	tri := MakeTrianglesData(6)
+	s := &Sprite{
+		tri: tri,
+		d:   Drawer{Triangles: tri},
+	}
+	s.SetPicture(pic)
+	return s
+}
+
+// SetPicture changes the Sprite's Picture. The new Picture may have a different size, everything
+// works.
+func (s *Sprite) SetPicture(pic Picture) {
+	var (
+		bounds     = pic.Bounds()
+		center     = bounds.Center()
+		horizontal = V(bounds.W()/2, 0)
+		vertical   = V(0, bounds.H()/2)
+	)
+	(*s.tri)[0].Position = -horizontal - vertical
+	(*s.tri)[1].Position = +horizontal - vertical
+	(*s.tri)[2].Position = +horizontal + vertical
+	(*s.tri)[3].Position = -horizontal - vertical
+	(*s.tri)[4].Position = +horizontal + vertical
+	(*s.tri)[5].Position = -horizontal + vertical
+	for i := range *s.tri {
+		(*s.tri)[i].Color = NRGBA{1, 1, 1, 1}
+		(*s.tri)[i].Picture = center + (*s.tri)[i].Position
+		(*s.tri)[i].Intensity = 1
+	}
+
+	s.d.Dirty()
+	s.d.Picture = pic
+}
+
+// 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)
+}
-- 
GitLab