ebiten/ebitenutil/debugprint.go

118 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-10 17:59:38 +01:00
package ebitenutil
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil/internal/assets"
2014-12-10 17:59:38 +01:00
"image/color"
"math"
2015-01-05 02:08:00 +01:00
"strings"
2014-12-10 17:59:38 +01:00
)
2015-01-05 02:08:00 +01:00
type debugPrintImageParts string
func (f debugPrintImageParts) Len() int {
return len(f)
}
func (f debugPrintImageParts) Dst(i int) (x0, y0, x1, y1 int) {
cw, ch := assets.TextImageCharWidth, assets.TextImageCharHeight
x := i - strings.LastIndex(string(f)[:i], "\n") - 1
y := strings.Count(string(f)[:i], "\n")
x *= cw
y *= ch
if x < 0 {
return 0, 0, 0, 0
}
return x, y, x + cw, y + ch
}
func (f debugPrintImageParts) Src(i int) (x0, y0, x1, y1 int) {
cw, ch := assets.TextImageCharWidth, assets.TextImageCharHeight
const n = assets.TextImageWidth / assets.TextImageCharWidth
code := int(f[i])
if code == '\n' {
return 0, 0, 0, 0
}
x := (code % n) * cw
y := (code / n) * ch
return x, y, x + cw, y + ch
}
2014-12-10 17:59:38 +01:00
type debugPrintState struct {
textImage *ebiten.Image
debugPrintRenderTarget *ebiten.Image
2014-12-10 17:59:38 +01:00
}
2015-01-05 02:08:00 +01:00
var defaultDebugPrintState = &debugPrintState{}
2014-12-10 17:59:38 +01:00
2016-08-01 18:47:25 +02:00
// DebugPrint draws the string str on the image.
func DebugPrint(image *ebiten.Image, str string) error {
return defaultDebugPrintState.DebugPrint(image, str)
2014-12-10 17:59:38 +01:00
}
2016-08-01 19:23:23 +02:00
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) error {
2015-01-05 02:08:00 +01:00
ur, ug, ub, ua := c.RGBA()
const max = math.MaxUint16
r := float64(ur) / max
g := float64(ug) / max
b := float64(ub) / max
a := float64(ua) / max
if 0 < a {
r /= a
g /= a
b /= a
}
op := &ebiten.DrawImageOptions{
ImageParts: debugPrintImageParts(str),
2014-12-10 17:59:38 +01:00
}
2015-01-05 02:08:00 +01:00
op.GeoM.Translate(float64(x+1), float64(y))
op.ColorM.Scale(r, g, b, a)
2016-08-01 19:23:23 +02:00
if err := rt.DrawImage(d.textImage, op); err != nil {
return err
}
return nil
2014-12-10 17:59:38 +01:00
}
2014-12-28 16:21:40 +01:00
// DebugPrint prints the given text str on the given image r.
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) error {
if d.textImage == nil {
2014-12-10 17:59:38 +01:00
img, err := assets.TextImage()
if err != nil {
2014-12-28 16:21:40 +01:00
return err
2014-12-10 17:59:38 +01:00
}
d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
2014-12-10 17:59:38 +01:00
if err != nil {
2014-12-28 16:21:40 +01:00
return err
2014-12-10 17:59:38 +01:00
}
}
if d.debugPrintRenderTarget == nil {
2014-12-10 17:59:38 +01:00
width, height := 256, 256
var err error
d.debugPrintRenderTarget, err = ebiten.NewImage(width, height, ebiten.FilterNearest)
2014-12-10 17:59:38 +01:00
if err != nil {
2014-12-28 16:21:40 +01:00
return err
2014-12-10 17:59:38 +01:00
}
}
2016-08-01 19:23:23 +02:00
if err := d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80}); err != nil {
return err
}
if err := d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
return err
}
2014-12-28 16:21:40 +01:00
return nil
2014-12-10 17:59:38 +01:00
}