ebiten/ebitenutil/debugprint.go

79 lines
2.2 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 (
2017-05-27 20:10:32 +02:00
"image"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil/internal/assets"
2014-12-10 17:59:38 +01:00
)
var (
debugPrintTextImage *ebiten.Image
)
2014-12-10 17:59:38 +01:00
2018-03-04 14:49:01 +01:00
func init() {
img := assets.CreateTextImage()
debugPrintTextImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
2014-12-10 17:59:38 +01:00
// DebugPrint draws the string str on the image on left top corner.
//
2018-02-19 18:02:30 +01:00
// The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
//
// DebugPrint always returns nil as of 1.5.0-alpha.
2016-08-01 18:47:25 +02:00
func DebugPrint(image *ebiten.Image, str string) error {
DebugPrintAt(image, str, 0, 0)
return nil
2014-12-10 17:59:38 +01:00
}
2018-09-21 22:23:26 +02:00
// DebugPrintAt draws the string str on the image at (x, y) position.
//
// 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) {
drawDebugText(image, str, x+1, y+1, true)
drawDebugText(image, str, x, y, false)
}
func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) {
2017-05-27 20:10:32 +02:00
op := &ebiten.DrawImageOptions{}
if shadow {
op.ColorM.Scale(0, 0, 0, 0.5)
}
2017-05-27 20:10:32 +02:00
x := 0
y := 0
2018-03-04 14:49:01 +01:00
w, _ := debugPrintTextImage.Size()
2017-05-27 20:10:32 +02:00
for _, c := range str {
2018-02-10 17:56:13 +01:00
const (
cw = assets.CharWidth
ch = assets.CharHeight
)
2017-05-27 20:10:32 +02:00
if c == '\n' {
x = 0
y += ch
continue
}
2018-02-10 17:56:13 +01:00
n := w / cw
2017-05-27 20:10:32 +02:00
sx := (int(c) % n) * cw
sy := (int(c) / n) * ch
op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Translate(float64(ox+1), float64(oy))
_ = rt.DrawImage(debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image), op)
2017-05-27 20:10:32 +02:00
x += cw
}
2014-12-10 17:59:38 +01:00
}