ebiten/ebitenutil/debugprint.go

89 lines
2.7 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/internal/assets"
2014-12-23 17:15:16 +01:00
"image"
2014-12-10 17:59:38 +01:00
"image/color"
"math"
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
y int
}
var defaultDebugPrintState = new(debugPrintState)
func DebugPrint(r *ebiten.Image, str string) {
defaultDebugPrintState.DebugPrint(r, str)
2014-12-10 17:59:38 +01:00
}
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) {
2014-12-27 09:37:31 +01:00
parts := []ebiten.ImagePart{}
2014-12-10 17:59:38 +01:00
locationX, locationY := 0, 0
for _, c := range str {
if c == '\n' {
locationX = 0
locationY += assets.TextImageCharHeight
continue
}
code := int(c)
const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth
2014-12-23 17:15:16 +01:00
srcX := (code % xCharNum) * assets.TextImageCharWidth
srcY := (code / xCharNum) * assets.TextImageCharHeight
2014-12-23 18:04:56 +01:00
dst := image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight)
src := image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight)
2014-12-27 09:37:31 +01:00
parts = append(parts, ebiten.ImagePart{Dst: dst, Src: src})
2014-12-10 17:59:38 +01:00
locationX += assets.TextImageCharWidth
}
cc := color.NRGBA64Model.Convert(c).(color.NRGBA64)
r := float64(cc.R) / math.MaxUint16
g := float64(cc.G) / math.MaxUint16
b := float64(cc.B) / math.MaxUint16
a := float64(cc.A) / math.MaxUint16
2014-12-26 15:01:47 +01:00
rt.DrawImage(d.textImage, &ebiten.DrawImageOptions{
2014-12-27 09:37:31 +01:00
Parts: parts,
GeoM: ebiten.TranslateGeo(float64(x+1), float64(y)),
ColorM: ebiten.ScaleColor(r, g, b, a),
})
2014-12-10 17:59:38 +01:00
}
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
if d.textImage == nil {
2014-12-10 17:59:38 +01:00
img, err := assets.TextImage()
if err != nil {
panic(err)
}
d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
2014-12-10 17:59:38 +01:00
if err != nil {
panic(err)
}
}
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 {
panic(err)
}
}
d.drawText(r, str, 1, d.y+1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
d.drawText(r, str, 0, d.y, color.NRGBA{0xff, 0xff, 0xff, 0xff})
2014-12-10 17:59:38 +01:00
}