Cache SubImage for the empty images

This commit is contained in:
Hajime Hoshi 2021-01-25 01:31:18 +09:00
parent 2b77b57b75
commit 569964d7e4
3 changed files with 13 additions and 6 deletions

View File

@ -24,7 +24,8 @@ import (
) )
var ( var (
emptyImage = ebiten.NewImage(3, 3) emptyImage = ebiten.NewImage(3, 3)
emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
) )
func init() { func init() {
@ -46,7 +47,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
op.ColorM = colormcache.ColorToColorM(clr) op.ColorM = colormcache.ColorToColorM(clr)
// Filter must be 'nearest' filter (default). // Filter must be 'nearest' filter (default).
// Linear filtering would make edges blurred. // 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. // DrawRect draws a rectangle on the given destination dst.

View File

@ -69,7 +69,10 @@ func (i *Image) Clear() {
i.Fill(color.Transparent) 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() { func init() {
w, h := emptyImage.Size() w, h := emptyImage.Size()
@ -101,7 +104,7 @@ func (i *Image) Fill(clr color.Color) {
op.ColorM.Scale(rf, gf, bf, af) op.ColorM.Scale(rf, gf, bf, af)
op.CompositeMode = CompositeModeCopy 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 { func canSkipMipmap(geom GeoM, filter driver.Filter) bool {

View File

@ -26,7 +26,10 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector/internal/triangulate" "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() { func init() {
emptyImage.Fill(color.White) emptyImage.Fill(color.White)
@ -139,5 +142,5 @@ func (p *Path) Fill(dst *ebiten.Image, op *FillOptions) {
} }
base += uint16(len(seg)) 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)
} }