ebitenutil: improve the performance of DebugPrint

Updates #1880
This commit is contained in:
Hajime Hoshi 2022-08-01 01:34:39 +09:00
parent dad72b874f
commit 074d370096
4 changed files with 23 additions and 18 deletions

View File

@ -49,15 +49,11 @@ func DebugPrint(image *ebiten.Image, str string) {
// //
// The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement. // The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
func DebugPrintAt(image *ebiten.Image, str string, x, y int) { func DebugPrintAt(image *ebiten.Image, str string, x, y int) {
drawDebugText(image, str, x+1, y+1, true) drawDebugText(image, str, x, y)
drawDebugText(image, str, x, y, false)
} }
func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) { func drawDebugText(rt *ebiten.Image, str string, ox, oy int) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
if shadow {
op.ColorM.Scale(0, 0, 0, 0.5)
}
x := 0 x := 0
y := 0 y := 0
w, _ := debugPrintTextImage.Size() w, _ := debugPrintTextImage.Size()

View File

@ -54,16 +54,25 @@ func run() error {
lines = append(lines, line) lines = append(lines, line)
} }
d := font.Drawer{ dst := image.NewRGBA(image.Rect(0, 0, charWidth*32+1, lineHeight*8+1))
Dst: image.NewRGBA(image.Rect(0, 0, charWidth*32, lineHeight*8)), for i, clr := range []color.Color{color.RGBA{0, 0, 0, 0x80}, color.White} {
Src: image.NewUniform(color.White), var offsetX int
Face: bitmapfont.Face, var offsetY int
Dot: fixed.P(dotX, dotY), if i == 0 {
} offsetX = 1
for _, line := range lines { offsetY = 1
d.Dot.X = fixed.I(dotX) }
d.DrawString(line) d := font.Drawer{
d.Dot.Y += fixed.I(lineHeight) Dst: dst,
Src: image.NewUniform(clr),
Face: bitmapfont.Face,
Dot: fixed.P(dotX+offsetX, dotY+offsetY),
}
for _, line := range lines {
d.Dot.X = fixed.I(dotX + offsetX)
d.DrawString(line)
d.Dot.Y += fixed.I(lineHeight)
}
} }
f, err := os.Create("text.png") f, err := os.Create("text.png")
@ -72,7 +81,7 @@ func run() error {
} }
defer f.Close() defer f.Close()
if err := png.Encode(f, d.Dst); err != nil { if err := png.Encode(f, dst); err != nil {
return err return err
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

File diff suppressed because one or more lines are too long