mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
vector: reduce memory allocations in the utility functions
This commit is contained in:
parent
38b8ba5677
commit
071024e89f
@ -18,6 +18,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
@ -27,6 +28,18 @@ var (
|
|||||||
whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
|
whiteSubImage = whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cachedVertices []ebiten.Vertex
|
||||||
|
cachedIndices []uint16
|
||||||
|
cacheM sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func useCachedVerticesAndIndices(fn func([]ebiten.Vertex, []uint16) (vs []ebiten.Vertex, is []uint16)) {
|
||||||
|
cacheM.Lock()
|
||||||
|
defer cacheM.Unlock()
|
||||||
|
cachedVertices, cachedIndices = fn(cachedVertices[:0], cachedIndices[:0])
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
b := whiteImage.Bounds()
|
b := whiteImage.Bounds()
|
||||||
pix := make([]byte, 4*b.Dx()*b.Dy())
|
pix := make([]byte, 4*b.Dx()*b.Dy())
|
||||||
@ -63,9 +76,12 @@ func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32,
|
|||||||
path.LineTo(x1, y1)
|
path.LineTo(x1, y1)
|
||||||
strokeOp := &StrokeOptions{}
|
strokeOp := &StrokeOptions{}
|
||||||
strokeOp.Width = strokeWidth
|
strokeOp.Width = strokeWidth
|
||||||
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
|
|
||||||
|
|
||||||
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
|
||||||
|
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
|
||||||
|
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
||||||
|
return vs, is
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DrawFilledRect fills a rectangle with the specified width and color.
|
// DrawFilledRect fills a rectangle with the specified width and color.
|
||||||
@ -75,9 +91,12 @@ func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Co
|
|||||||
path.LineTo(x, y+height)
|
path.LineTo(x, y+height)
|
||||||
path.LineTo(x+width, y+height)
|
path.LineTo(x+width, y+height)
|
||||||
path.LineTo(x+width, y)
|
path.LineTo(x+width, y)
|
||||||
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
|
||||||
|
|
||||||
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
|
||||||
|
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
|
||||||
|
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
||||||
|
return vs, is
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrokeRect strokes a rectangle with the specified width and color.
|
// StrokeRect strokes a rectangle with the specified width and color.
|
||||||
@ -94,18 +113,24 @@ func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth floa
|
|||||||
strokeOp := &StrokeOptions{}
|
strokeOp := &StrokeOptions{}
|
||||||
strokeOp.Width = strokeWidth
|
strokeOp.Width = strokeWidth
|
||||||
strokeOp.MiterLimit = 10
|
strokeOp.MiterLimit = 10
|
||||||
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
|
|
||||||
|
|
||||||
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
|
||||||
|
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
|
||||||
|
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
||||||
|
return vs, is
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color.
|
// DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color.
|
||||||
func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) {
|
func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) {
|
||||||
var path Path
|
var path Path
|
||||||
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
|
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
|
||||||
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
|
|
||||||
|
|
||||||
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
|
||||||
|
vs, is = path.AppendVerticesAndIndicesForFilling(vs, is)
|
||||||
|
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
||||||
|
return vs, is
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color.
|
// StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color.
|
||||||
@ -118,7 +143,10 @@ func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr
|
|||||||
|
|
||||||
strokeOp := &StrokeOptions{}
|
strokeOp := &StrokeOptions{}
|
||||||
strokeOp.Width = strokeWidth
|
strokeOp.Width = strokeWidth
|
||||||
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
|
|
||||||
|
|
||||||
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
useCachedVerticesAndIndices(func(vs []ebiten.Vertex, is []uint16) ([]ebiten.Vertex, []uint16) {
|
||||||
|
vs, is = path.AppendVerticesAndIndicesForStroke(vs, is, strokeOp)
|
||||||
|
drawVerticesForUtil(dst, vs, is, clr, antialias)
|
||||||
|
return vs, is
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user