From b86a3db7b8c372ea72ae4ad633031cce77e90ee6 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 17 May 2017 23:45:22 +0200 Subject: [PATCH] change Sprite.Draw and Canvas.Draw signatures (include Matrix) --- lights/main.go | 20 +++++++------------- platformer/main.go | 5 ++--- smoke/main.go | 13 +++++++------ xor/main.go | 2 +- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lights/main.go b/lights/main.go index c1a6d3f..dd41386 100644 --- a/lights/main.go +++ b/lights/main.go @@ -27,10 +27,6 @@ func loadPicture(path string) (pixel.Picture, error) { return pixel.PictureDataFromImage(img), nil } -type drawer interface { - Draw(pixel.Target) -} - type colorlight struct { color pixel.RGBA point pixel.Vec @@ -43,7 +39,7 @@ type colorlight struct { imd *imdraw.IMDraw } -func (cl *colorlight) apply(src, noise drawer, dst pixel.ComposeTarget) { +func (cl *colorlight) apply(dst pixel.ComposeTarget, center pixel.Vec, src, noise *pixel.Sprite) { // create the light arc if not created already if cl.imd == nil { imd := imdraw.New(nil) @@ -66,12 +62,12 @@ func (cl *colorlight) apply(src, noise drawer, dst pixel.ComposeTarget) { // draw the noise inside the light dst.SetMatrix(pixel.IM) dst.SetComposeMethod(pixel.ComposeIn) - noise.Draw(dst) + noise.Draw(dst, pixel.IM.Moved(center)) // draw an image inside the noisy light dst.SetColorMask(cl.color) dst.SetComposeMethod(pixel.ComposeIn) - src.Draw(dst) + src.Draw(dst, pixel.IM.Moved(center)) // draw the light reflected from the dust dst.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point)) @@ -101,9 +97,7 @@ func run() { } panda := pixel.NewSprite(pandaPic, pandaPic.Bounds()) - panda.SetMatrix(pixel.IM.Moved(win.Bounds().Center())) noise := pixel.NewSprite(noisePic, noisePic.Bounds()) - noise.SetMatrix(pixel.IM.Moved(win.Bounds().Center())) colors := []pixel.RGBA{ pixel.RGB(1, 0, 0), @@ -174,7 +168,7 @@ func run() { // draw the panda visible outside the light win.SetColorMask(pixel.Alpha(0.4)) win.SetComposeMethod(pixel.ComposePlus) - panda.Draw(win) + panda.Draw(win, pixel.IM.Moved(win.Bounds().Center())) allLight.Clear(pixel.Alpha(0)) allLight.SetComposeMethod(pixel.ComposePlus) @@ -182,13 +176,13 @@ func run() { // accumulate all the lights for i := range lights { oneLight.Clear(pixel.Alpha(0)) - lights[i].apply(panda, noise, oneLight) - oneLight.Draw(allLight) + lights[i].apply(oneLight, oneLight.Bounds().Center(), panda, noise) + oneLight.Draw(allLight, pixel.IM.Moved(allLight.Bounds().Center())) } // compose the final result win.SetColorMask(pixel.Alpha(1)) - allLight.Draw(win) + allLight.Draw(win, pixel.IM.Moved(win.Bounds().Center())) win.Update() diff --git a/platformer/main.go b/platformer/main.go index ee2971e..dab3628 100644 --- a/platformer/main.go +++ b/platformer/main.go @@ -215,7 +215,7 @@ func (ga *gopherAnim) draw(t pixel.Target, phys *gopherPhys) { } // draw the correct frame with the correct position and direction ga.sprite.Set(ga.sheet, ga.frame) - ga.sprite.SetMatrix(pixel.IM. + ga.sprite.Draw(t, pixel.IM. ScaledXY(0, pixel.V( phys.rect.W()/ga.sprite.Frame().W(), phys.rect.H()/ga.sprite.Frame().H(), @@ -223,7 +223,6 @@ func (ga *gopherAnim) draw(t pixel.Target, phys *gopherPhys) { ScaledXY(0, pixel.V(-ga.dir, 1)). Moved(phys.rect.Center()), ) - ga.sprite.Draw(t) } type goal struct { @@ -385,7 +384,7 @@ func run() { win.Bounds().H()/canvas.Bounds().H(), ), ).Moved(win.Bounds().Center())) - canvas.Draw(win) + canvas.Draw(win, pixel.IM.Moved(canvas.Bounds().Center())) win.Update() } } diff --git a/smoke/main.go b/smoke/main.go index 40007a2..4b366c2 100644 --- a/smoke/main.go +++ b/smoke/main.go @@ -54,13 +54,14 @@ func (p *particles) DrawAll(t pixel.Target) { for e := p.parts.Front(); e != nil; e = e.Next() { part := e.Value.(*particle) - part.Sprite.SetMatrix(pixel.IM. - Scaled(0, part.Scale). - Rotated(0, part.Rot). - Moved(part.Pos), + part.Sprite.DrawColorMask( + t, + pixel.IM. + Scaled(0, part.Scale). + Rotated(0, part.Rot). + Moved(part.Pos), + part.Mask, ) - part.Sprite.SetColorMask(part.Mask) - part.Sprite.Draw(t) } } diff --git a/xor/main.go b/xor/main.go index 9b4c5c3..a062de4 100644 --- a/xor/main.go +++ b/xor/main.go @@ -66,7 +66,7 @@ func run() { imd.Draw(canvas) win.Clear(colornames.Green) - canvas.Draw(win) + canvas.Draw(win, pixel.IM.Moved(win.Bounds().Center())) win.Update() } }