1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-26 03:56:42 -05:00

fixed lint errors in d2debututil (#678)

This commit is contained in:
lord 2020-08-04 19:23:15 -07:00 committed by GitHub
parent 4a48acb8ba
commit 120afe51a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 28 deletions

View File

@ -1,70 +1,97 @@
// Package d2debugutil provides debug utilities
package d2debugutil package d2debugutil
import ( import (
"image" "image"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2debugutil/internal/assets" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2debugutil/internal/assets"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
var ( const (
debugPrintTextImage *ebiten.Image cw = assets.CharWidth
debugPrintTextSubImages = map[rune]*ebiten.Image{} ch = assets.CharHeight
) )
func init() { // DebugPrinter is the global debug printer
img := assets.CreateTextImage() var DebugPrinter = NewDebugPrinter() //nolint:gochecknoglobals // currently global by design
debugPrintTextImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
// NewDebugPrinter creates a new debug printer
func NewDebugPrinter() *GlyphPrinter {
img, err := ebiten.NewImageFromImage(assets.CreateTextImage(), ebiten.FilterDefault)
if err != nil {
return nil
}
printer := &GlyphPrinter{
glyphImageTable: img,
glyphs: make(map[rune]*ebiten.Image),
}
return printer
} }
// DebugPrint draws the string str on the image on left top corner. // GlyphPrinter uses an image containing glyphs to draw text onto ebiten images
type GlyphPrinter struct {
glyphImageTable *ebiten.Image
glyphs map[rune]*ebiten.Image
}
// Print 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. // 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. // DebugPrint always returns nil as of 1.5.0-alpha.
func D2DebugPrint(image *ebiten.Image, str string) error { func (p *GlyphPrinter) Print(target *ebiten.Image, str string) error {
D2DebugPrintAt(image, str, 0, 0) p.PrintAt(target, str, 0, 0)
return nil return nil
} }
// DebugPrintAt draws the string str on the image at (x, y) position. // PrintAt 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
// The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement. // Basic Latin and C1 Controls and Latin-1 Supplement.
func D2DebugPrintAt(image *ebiten.Image, str string, x, y int) { func (p *GlyphPrinter) PrintAt(target *ebiten.Image, str string, x, y int) {
drawDebugText(image, str, x, y, false) p.drawDebugText(target, str, x, y, false)
} }
func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) { func (p *GlyphPrinter) drawDebugText(target *ebiten.Image, str string, ox, oy int, shadow bool) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
if shadow { if shadow {
op.ColorM.Scale(0, 0, 0, 0.5) op.ColorM.Scale(0, 0, 0, 0.5)
} }
x := 0 x := 0
y := 0 y := 0
w, _ := debugPrintTextImage.Size()
w, _ := p.glyphImageTable.Size()
for _, c := range str { for _, c := range str {
const (
cw = assets.CharWidth
ch = assets.CharHeight
)
if c == '\n' { if c == '\n' {
x = 0 x = 0
y += ch y += ch
continue continue
} }
s, ok := debugPrintTextSubImages[c]
s, ok := p.glyphs[c]
if !ok { if !ok {
n := w / cw n := w / cw
sx := (int(c) % n) * cw sx := (int(c) % n) * cw
sy := (int(c) / n) * ch sy := (int(c) / n) * ch
s = debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image) rect := image.Rect(sx, sy, sx+cw, sy+ch)
debugPrintTextSubImages[c] = s s = p.glyphImageTable.SubImage(rect).(*ebiten.Image)
p.glyphs[c] = s
} }
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Translate(float64(ox+1), float64(oy)) op.GeoM.Translate(float64(ox+1), float64(oy))
op.CompositeMode = ebiten.CompositeModeLighter op.CompositeMode = ebiten.CompositeModeLighter
_ = rt.DrawImage(s, op) _ = target.DrawImage(s, op)
x += cw x += cw
} }
} }

View File

@ -16,6 +16,7 @@
//go:generate file2byteslice -input /tmp/compressedTextRGBA -output textrgba.go -package assets -var compressedTextRGBA //go:generate file2byteslice -input /tmp/compressedTextRGBA -output textrgba.go -package assets -var compressedTextRGBA
//go:generate gofmt -s -w . //go:generate gofmt -s -w .
// Package assets provides files for use by the debug utils
package assets package assets
import ( import (
@ -28,21 +29,26 @@ import (
) )
const ( const (
CharWidth = 8 // CharWidth of all glyphs inside of the glyph table image
CharWidth = 8
// CharHeight of all glyphs inside of the glyph table image
CharHeight = 16 CharHeight = 16
) )
// CreateTextImage creates
func CreateTextImage() image.Image { func CreateTextImage() image.Image {
s, err := gzip.NewReader(bytes.NewReader(CompressedDebugText)) s, err := gzip.NewReader(bytes.NewReader(CompressedDebugText))
if err != nil { if err != nil {
panic(fmt.Sprintf("assets: gzip.NewReader failed: %v", err)) panic(fmt.Sprintf("assets: gzip.NewReader failed: %v", err))
} }
defer s.Close()
debugBmp, err := bmp.Decode(s) debugBmp, err := bmp.Decode(s)
if err != nil { if err != nil {
panic(fmt.Sprintf("assets: bmp.Decode failed: %v", err)) panic(fmt.Sprintf("assets: bmp.Decode failed: %v", err))
} }
_ = s.Close()
return debugBmp return debugBmp
} }

View File

@ -222,7 +222,8 @@ func (s *ebitenSurface) RenderSection(sfc d2interface.Surface, bound image.Recta
// DrawTextf renders the string to the surface with the given format string and a set of parameters // DrawTextf renders the string to the surface with the given format string and a set of parameters
func (s *ebitenSurface) DrawTextf(format string, params ...interface{}) { func (s *ebitenSurface) DrawTextf(format string, params ...interface{}) {
d2debugutil.D2DebugPrintAt(s.image, fmt.Sprintf(format, params...), s.stateCurrent.x, s.stateCurrent.y) str := fmt.Sprintf(format, params...)
d2debugutil.DebugPrinter.PrintAt(s.image, str, s.stateCurrent.x, s.stateCurrent.y)
} }
// DrawLine draws a line // DrawLine draws a line