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
|
|
|
|
2022-07-31 18:13:23 +02:00
|
|
|
//go:generate go run gen.go
|
|
|
|
|
2014-12-10 17:59:38 +01:00
|
|
|
package ebitenutil
|
|
|
|
|
|
|
|
import (
|
2022-07-31 18:13:23 +02:00
|
|
|
"bytes"
|
2022-09-15 16:25:21 +02:00
|
|
|
_ "embed"
|
2017-05-27 20:10:32 +02:00
|
|
|
"image"
|
2022-07-31 18:13:23 +02:00
|
|
|
_ "image/png"
|
2017-05-27 20:10:32 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2014-12-10 17:59:38 +01:00
|
|
|
)
|
|
|
|
|
2022-09-15 16:25:21 +02:00
|
|
|
//go:embed text.png
|
|
|
|
var text_png []byte
|
|
|
|
|
2018-03-04 18:08:44 +01:00
|
|
|
var (
|
2022-07-31 18:13:23 +02:00
|
|
|
debugPrintTextImage *ebiten.Image
|
2019-11-24 12:30:54 +01:00
|
|
|
debugPrintTextSubImages = map[rune]*ebiten.Image{}
|
2018-03-04 18:08:44 +01:00
|
|
|
)
|
2014-12-10 17:59:38 +01:00
|
|
|
|
2022-07-31 18:13:23 +02:00
|
|
|
func init() {
|
2022-09-15 16:25:21 +02:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(text_png))
|
2022-07-31 18:13:23 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
debugPrintTextImage = ebiten.NewImageFromImage(img)
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:21:13 +02:00
|
|
|
// DebugPrint draws the string str on the image on left top corner.
|
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.
|
2020-10-05 18:05:33 +02:00
|
|
|
func DebugPrint(image *ebiten.Image, str string) {
|
2018-09-21 22:21:13 +02:00
|
|
|
DebugPrintAt(image, str, 0, 0)
|
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.
|
2018-09-21 22:21:13 +02: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.
|
|
|
|
func DebugPrintAt(image *ebiten.Image, str string, x, y int) {
|
2022-07-31 18:34:39 +02:00
|
|
|
drawDebugText(image, str, x, y)
|
2018-09-21 22:21:13 +02:00
|
|
|
}
|
|
|
|
|
2022-07-31 18:34:39 +02:00
|
|
|
func drawDebugText(rt *ebiten.Image, str string, ox, oy int) {
|
2017-05-27 20:10:32 +02:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
x := 0
|
|
|
|
y := 0
|
2023-01-19 15:42:35 +01:00
|
|
|
w := debugPrintTextImage.Bounds().Dx()
|
2017-05-27 20:10:32 +02:00
|
|
|
for _, c := range str {
|
2018-02-10 17:56:13 +01:00
|
|
|
const (
|
2022-07-31 18:13:23 +02:00
|
|
|
cw = 6
|
|
|
|
ch = 16
|
2018-02-10 17:56:13 +01:00
|
|
|
)
|
2017-05-27 20:10:32 +02:00
|
|
|
if c == '\n' {
|
|
|
|
x = 0
|
|
|
|
y += ch
|
|
|
|
continue
|
|
|
|
}
|
2019-11-24 12:30:54 +01:00
|
|
|
s, ok := debugPrintTextSubImages[c]
|
|
|
|
if !ok {
|
|
|
|
n := w / cw
|
|
|
|
sx := (int(c) % n) * cw
|
|
|
|
sy := (int(c) / n) * ch
|
|
|
|
s = debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image)
|
|
|
|
debugPrintTextSubImages[c] = s
|
|
|
|
}
|
2017-05-27 20:10:32 +02:00
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(float64(x), float64(y))
|
|
|
|
op.GeoM.Translate(float64(ox+1), float64(oy))
|
2020-10-05 17:21:11 +02:00
|
|
|
rt.DrawImage(s, op)
|
2017-05-27 20:10:32 +02:00
|
|
|
x += cw
|
|
|
|
}
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|