Last active
November 16, 2021 19:06
-
-
Save Zyko0/785d079c9b9facbacc3df56c279d8f73 to your computer and use it in GitHub Desktop.
ebitenutil/debugprint.go: Using triangles instead of images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. | |
package ebitenutil | |
import ( | |
"image" | |
"github.com/hajimehoshi/ebiten/v2" | |
"github.com/hajimehoshi/ebiten/v2/ebitenutil/internal/assets" | |
) | |
var ( | |
debugPrintTextImage = ebiten.NewImageFromImage(assets.CreateTextImage()) | |
debugPrintTextSubImages = map[rune]*ebiten.Image{} | |
) | |
// DebugPrint draws the string str on the image on left top corner. | |
// | |
// 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 DebugPrint(image *ebiten.Image, str string) { | |
DebugPrintAt(image, str, 0, 0) | |
} | |
// 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) { | |
op := &ebiten.DrawImageOptions{} | |
if shadow { | |
op.ColorM.Scale(0, 0, 0, 0.5) | |
} | |
x := 0 | |
y := 0 | |
w, _ := debugPrintTextImage.Size() | |
for _, c := range str { | |
const ( | |
cw = assets.CharWidth | |
ch = assets.CharHeight | |
) | |
if c == '\n' { | |
x = 0 | |
y += ch | |
continue | |
} | |
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 | |
} | |
op.GeoM.Reset() | |
op.GeoM.Translate(float64(x), float64(y)) | |
op.GeoM.Translate(float64(ox+1), float64(oy)) | |
rt.DrawImage(s, op) | |
x += cw | |
} | |
} | |
func DebugPrintWithTrianglesAt(image *ebiten.Image, str string, x, y int) { | |
drawDebugTextWithTriangles(image, str, x+1, y+1, true) | |
drawDebugTextWithTriangles(image, str, x, y, false) | |
} | |
var boxIndices = [6]uint16{0, 1, 2, 1, 2, 3} | |
func drawDebugTextWithTriangles(rt *ebiten.Image, str string, ox, oy int, shadow bool) { | |
op := &ebiten.DrawTrianglesOptions{} | |
if shadow { | |
op.ColorM.Scale(0, 0, 0, 0.5) | |
} | |
x := 0 | |
y := 0 | |
w, _ := debugPrintTextImage.Size() | |
vertices := make([]ebiten.Vertex, 0, len(str)*4) | |
indices := make([]uint16, 0, len(str)*6) | |
for i, c := range str { | |
const ( | |
cw = assets.CharWidth | |
ch = assets.CharHeight | |
) | |
if c == '\n' { | |
x = 0 | |
y += ch | |
continue | |
} | |
n := w / cw | |
sx := (int(c) % n) * cw | |
sy := (int(c) / n) * ch | |
vertices = append(vertices, []ebiten.Vertex{ | |
{ | |
DstX: float32(x + ox + 1), | |
DstY: float32(y + oy), | |
SrcX: float32(sx), | |
SrcY: float32(sy), | |
ColorR: 1, | |
ColorG: 1, | |
ColorB: 1, | |
ColorA: 1, | |
}, | |
{ | |
DstX: float32(x + ox + 1 + cw), | |
DstY: float32(y + oy), | |
SrcX: float32(sx + cw), | |
SrcY: float32(sy), | |
ColorR: 1, | |
ColorG: 1, | |
ColorB: 1, | |
ColorA: 1, | |
}, | |
{ | |
DstX: float32(x + ox + 1), | |
DstY: float32(y + oy + ch), | |
SrcX: float32(sx), | |
SrcY: float32(sy + ch), | |
ColorR: 1, | |
ColorG: 1, | |
ColorB: 1, | |
ColorA: 1, | |
}, | |
{ | |
DstX: float32(x + ox + 1 + cw), | |
DstY: float32(y + oy + ch), | |
SrcX: float32(sx + cw), | |
SrcY: float32(sy + ch), | |
ColorR: 1, | |
ColorG: 1, | |
ColorB: 1, | |
ColorA: 1, | |
}, | |
}...) | |
indiceCursor := uint16(i * 4) | |
indices = append(indices, []uint16{ | |
boxIndices[0] + indiceCursor, | |
boxIndices[1] + indiceCursor, | |
boxIndices[2] + indiceCursor, | |
boxIndices[3] + indiceCursor, | |
boxIndices[4] + indiceCursor, | |
boxIndices[5] + indiceCursor, | |
}...) | |
x += cw | |
} | |
rt.DrawTriangles(vertices, indices, debugPrintTextImage, op) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment