ebitenutil: Refactoring

This commit is contained in:
Hajime Hoshi 2018-02-11 01:56:13 +09:00
parent eb2f3cf4f9
commit ae04c2d680
2 changed files with 10 additions and 9 deletions

View File

@ -54,15 +54,18 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, ox, oy int, c c
op.ColorM.Scale(r, g, b, a)
x := 0
y := 0
w, _ := d.textImage.Size()
for _, c := range str {
const cw = assets.TextImageCharWidth
const ch = assets.TextImageCharHeight
const (
cw = assets.CharWidth
ch = assets.CharHeight
)
if c == '\n' {
x = 0
y += ch
continue
}
const n = assets.TextImageWidth / cw
n := w / cw
sx := (int(c) % n) * cw
sy := (int(c) / n) * ch
r := image.Rect(sx, sy, sx+cw, sy+ch)
@ -78,7 +81,7 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, ox, oy int, c c
// 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.TextImage()
img := assets.CreateTextImage()
d.textImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
}
if d.debugPrintRenderTarget == nil {

View File

@ -25,13 +25,11 @@ import (
)
const (
TextImageWidth = 192
TextImageHeight = 128
TextImageCharWidth = TextImageWidth / 32
TextImageCharHeight = TextImageHeight / 8
CharWidth = 6
CharHeight = 16
)
func TextImage() image.Image {
func CreateTextImage() image.Image {
img, _, err := image.Decode(bytes.NewBuffer(textPng))
if err != nil {
panic(fmt.Sprintf("assets: image.Decode failed, %v", err))