ebitenutil: Refactoring

This commit is contained in:
Hajime Hoshi 2018-03-04 22:49:01 +09:00
parent d9571c7e32
commit cdfee9fd74

View File

@ -17,17 +17,17 @@ package ebitenutil
import ( import (
"image" "image"
"image/color" "image/color"
"math"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil/internal/assets" "github.com/hajimehoshi/ebiten/ebitenutil/internal/assets"
) )
type debugPrintState struct { var debugPrintTextImage *ebiten.Image
textImage *ebiten.Image
}
var defaultDebugPrintState = &debugPrintState{} func init() {
img := assets.CreateTextImage()
debugPrintTextImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
// DebugPrint draws the string str on the image. // DebugPrint draws the string str on the image.
// //
@ -35,27 +35,25 @@ var defaultDebugPrintState = &debugPrintState{}
// //
// DebugPrint always returns nil as of 1.5.0-alpha. // DebugPrint always returns nil as of 1.5.0-alpha.
func DebugPrint(image *ebiten.Image, str string) error { func DebugPrint(image *ebiten.Image, str string) error {
defaultDebugPrintState.DebugPrint(image, str) drawDebugText(image, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
drawDebugText(image, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff})
return nil return nil
} }
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, ox, oy int, c color.Color) { func drawDebugText(rt *ebiten.Image, str string, ox, oy int, c color.Color) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
ur, ug, ub, ua := c.RGBA() ur, ug, ub, ua := c.RGBA()
const max = math.MaxUint16 if ua == 0 {
r := float64(ur) / max return
g := float64(ug) / max
b := float64(ub) / max
a := float64(ua) / max
if 0 < a {
r /= a
g /= a
b /= a
} }
r := float64(ur) / float64(ua)
g := float64(ug) / float64(ua)
b := float64(ub) / float64(ua)
a := float64(ua) / 0xffff
op.ColorM.Scale(r, g, b, a) op.ColorM.Scale(r, g, b, a)
x := 0 x := 0
y := 0 y := 0
w, _ := d.textImage.Size() w, _ := debugPrintTextImage.Size()
for _, c := range str { for _, c := range str {
const ( const (
cw = assets.CharWidth cw = assets.CharWidth
@ -74,17 +72,7 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, ox, oy int, c c
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Translate(float64(ox+1), float64(oy)) op.GeoM.Translate(float64(ox+1), float64(oy))
_ = rt.DrawImage(d.textImage, op) _ = rt.DrawImage(debugPrintTextImage, op)
x += cw x += cw
} }
} }
// DebugPrint prints the given text str on the given image r.
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
if d.textImage == nil {
img := assets.CreateTextImage()
d.textImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff})
}