1
0

debugprint.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:generate go run gen.go
  15. package ebitenutil
  16. import (
  17. "bytes"
  18. _ "embed"
  19. "image"
  20. _ "image/png"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. )
  23. //go:embed text.png
  24. var text_png []byte
  25. var (
  26. debugPrintTextImage *ebiten.Image
  27. debugPrintTextSubImages = map[rune]*ebiten.Image{}
  28. )
  29. func init() {
  30. img, _, err := image.Decode(bytes.NewReader(text_png))
  31. if err != nil {
  32. panic(err)
  33. }
  34. debugPrintTextImage = ebiten.NewImageFromImage(img)
  35. }
  36. // DebugPrint draws the string str on the image at (0, 0) position (the upper-left corner in most cases).
  37. //
  38. // The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
  39. func DebugPrint(image *ebiten.Image, str string) {
  40. DebugPrintAt(image, str, 0, 0)
  41. }
  42. // DebugPrintAt draws the string str on the image at (x, y) position.
  43. //
  44. // The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.
  45. func DebugPrintAt(image *ebiten.Image, str string, x, y int) {
  46. drawDebugText(image, str, x, y)
  47. }
  48. func drawDebugText(rt *ebiten.Image, str string, ox, oy int) {
  49. op := &ebiten.DrawImageOptions{}
  50. x := 0
  51. y := 0
  52. w := debugPrintTextImage.Bounds().Dx()
  53. for _, c := range str {
  54. const (
  55. cw = 6
  56. ch = 16
  57. )
  58. if c == '\n' {
  59. x = 0
  60. y += ch
  61. continue
  62. }
  63. s, ok := debugPrintTextSubImages[c]
  64. if !ok {
  65. n := w / cw
  66. sx := (int(c) % n) * cw
  67. sy := (int(c) / n) * ch
  68. s = debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image)
  69. debugPrintTextSubImages[c] = s
  70. }
  71. op.GeoM.Reset()
  72. op.GeoM.Translate(float64(x), float64(y))
  73. op.GeoM.Translate(float64(ox+1), float64(oy))
  74. rt.DrawImage(s, op)
  75. x += cw
  76. }
  77. }