2014-12-24 03:04:10 +01:00
|
|
|
// 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"
|
2014-12-10 17:59:38 +01:00
|
|
|
"image/color"
|
2014-12-22 15:08:13 +01:00
|
|
|
"math"
|
2017-05-27 20:10:32 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil/internal/assets"
|
2014-12-10 17:59:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type debugPrintState struct {
|
2018-02-26 19:13:58 +01:00
|
|
|
textImage *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.
|
2017-03-04 04:02:41 +01:00
|
|
|
//
|
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.
|
|
|
|
//
|
2017-03-04 04:02:41 +01:00
|
|
|
// 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 {
|
2017-03-04 04:02:41 +01:00
|
|
|
defaultDebugPrintState.DebugPrint(image, str)
|
|
|
|
return nil
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|
|
|
|
|
2017-05-27 20:10:32 +02:00
|
|
|
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, ox, oy int, c color.Color) {
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
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.ColorM.Scale(r, g, b, a)
|
2017-05-27 20:10:32 +02:00
|
|
|
x := 0
|
|
|
|
y := 0
|
2018-02-10 17:56:13 +01:00
|
|
|
w, _ := d.textImage.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
|
|
|
|
r := image.Rect(sx, sy, sx+cw, sy+ch)
|
|
|
|
op.SourceRect = &r
|
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(float64(x), float64(y))
|
|
|
|
op.GeoM.Translate(float64(ox+1), float64(oy))
|
|
|
|
_ = rt.DrawImage(d.textImage, op)
|
|
|
|
x += cw
|
|
|
|
}
|
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.
|
2017-03-04 04:02:41 +01:00
|
|
|
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
|
2014-12-22 02:36:42 +01:00
|
|
|
if d.textImage == nil {
|
2018-02-10 17:56:13 +01:00
|
|
|
img := assets.CreateTextImage()
|
2017-03-04 04:02:41 +01:00
|
|
|
d.textImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|
2017-03-04 04:02:41 +01:00
|
|
|
d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
|
|
|
d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff})
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|