From 569964d7e4de9c200c33423f15ad56fd74417066 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 25 Jan 2021 01:31:18 +0900 Subject: [PATCH] Cache SubImage for the empty images --- ebitenutil/shapes.go | 5 +++-- image.go | 7 +++++-- vector/path.go | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ebitenutil/shapes.go b/ebitenutil/shapes.go index 901ddca23..a0284dab3 100644 --- a/ebitenutil/shapes.go +++ b/ebitenutil/shapes.go @@ -24,7 +24,8 @@ import ( ) var ( - emptyImage = ebiten.NewImage(3, 3) + emptyImage = ebiten.NewImage(3, 3) + emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image) ) func init() { @@ -46,7 +47,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) { op.ColorM = colormcache.ColorToColorM(clr) // Filter must be 'nearest' filter (default). // Linear filtering would make edges blurred. - dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op) + dst.DrawImage(emptySubImage, op) } // DrawRect draws a rectangle on the given destination dst. diff --git a/image.go b/image.go index 6f342bd9e..4dbe077c4 100644 --- a/image.go +++ b/image.go @@ -69,7 +69,10 @@ func (i *Image) Clear() { i.Fill(color.Transparent) } -var emptyImage = NewImage(3, 3) +var ( + emptyImage = NewImage(3, 3) + emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*Image) +) func init() { w, h := emptyImage.Size() @@ -101,7 +104,7 @@ func (i *Image) Fill(clr color.Color) { op.ColorM.Scale(rf, gf, bf, af) op.CompositeMode = CompositeModeCopy - i.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*Image), op) + i.DrawImage(emptySubImage, op) } func canSkipMipmap(geom GeoM, filter driver.Filter) bool { diff --git a/vector/path.go b/vector/path.go index a00e64df4..ef8fe05fb 100644 --- a/vector/path.go +++ b/vector/path.go @@ -26,7 +26,10 @@ import ( "github.com/hajimehoshi/ebiten/v2/vector/internal/triangulate" ) -var emptyImage = ebiten.NewImage(3, 3) +var ( + emptyImage = ebiten.NewImage(3, 3) + emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image) +) func init() { emptyImage.Fill(color.White) @@ -139,5 +142,5 @@ func (p *Path) Fill(dst *ebiten.Image, op *FillOptions) { } base += uint16(len(seg)) } - dst.DrawTriangles(vertices, indices, emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), nil) + dst.DrawTriangles(vertices, indices, emptySubImage, nil) }