-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug_print.go
177 lines (138 loc) · 3.62 KB
/
debug_print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//go:build minedev
package minesweeper
import (
"fmt"
"image/color"
"strings"
eb "github.com/hajimehoshi/ebiten/v2"
ebt "github.com/hajimehoshi/ebiten/v2/text/v2"
)
type DebugMsg struct {
Key string
Value string
}
var TheDebugPrintManager struct {
DebugMsgs []DebugMsg
PersistentDebugMsgs []DebugMsg
DebugMsgRenderTarget *eb.Image
builder strings.Builder
}
func DebugPrintf(key, fmtStr string, values ...any) {
DebugPuts(key, fmt.Sprintf(fmtStr, values...))
}
func DebugPrint(key string, values ...any) {
DebugPuts(key, fmt.Sprint(values...))
}
func DebugPuts(key, value string) {
dm := &TheDebugPrintManager
for i, msg := range dm.DebugMsgs {
if msg.Key == key {
dm.DebugMsgs[i].Value = value
return
}
}
dm.DebugMsgs = append(dm.DebugMsgs, DebugMsg{
Key: key,
Value: value,
})
}
func DebugPrintfPersist(key, fmtStr string, values ...any) {
DebugPutsPersist(key, fmt.Sprintf(fmtStr, values...))
}
func DebugPrintPersist(key string, values ...any) {
DebugPutsPersist(key, fmt.Sprint(values...))
}
func DebugPutsPersist(key, value string) {
dm := &TheDebugPrintManager
for i, msg := range dm.PersistentDebugMsgs {
if msg.Key == key {
dm.DebugMsgs[i].Value = value
return
}
}
dm.PersistentDebugMsgs = append(dm.PersistentDebugMsgs, DebugMsg{
Key: key,
Value: value,
})
}
func DrawDebugMsgs(dst *eb.Image) {
dm := &TheDebugPrintManager
dm.builder.Reset()
msgCounter := 0
for _, msg := range dm.PersistentDebugMsgs {
// builder doesn't actually errors out
// no need to check error
dm.builder.WriteString(msg.Key)
dm.builder.WriteString(": ")
dm.builder.WriteString(msg.Value)
msgCounter++
if msgCounter != len(dm.PersistentDebugMsgs)+len(dm.DebugMsgs) {
dm.builder.WriteString("\n")
}
}
for _, msg := range dm.DebugMsgs {
dm.builder.WriteString(msg.Key)
dm.builder.WriteString(": ")
dm.builder.WriteString(msg.Value)
msgCounter++
if msgCounter != len(dm.PersistentDebugMsgs)+len(dm.DebugMsgs) {
dm.builder.WriteString("\n")
}
}
const faceSize = 20
const hozMargin = 5
const vertMargin = 5
scale := faceSize / FaceSize(ClearFace)
faceLineSpacing := FaceLineSpacing(ClearFace) + 3
text := dm.builder.String()
w, h := ebt.Measure(text, ClearFace, faceLineSpacing)
// update width and height of background rect
boxW, boxH := w*scale+hozMargin*2, h*scale+vertMargin*2
rect := FRectWH(boxW, boxH)
createBuf := dm.DebugMsgRenderTarget == nil
createBuf = createBuf || dm.DebugMsgRenderTarget.Bounds().Dx() < int(boxW+1)
createBuf = createBuf || dm.DebugMsgRenderTarget.Bounds().Dy() < int(boxH+1)
if createBuf {
if dm.DebugMsgRenderTarget != nil {
dm.DebugMsgRenderTarget.Deallocate()
}
dm.DebugMsgRenderTarget = eb.NewImageWithOptions(
RectWH(int(boxW+1), int(boxH+1)),
&eb.NewImageOptions{Unmanaged: true},
)
}
dm.DebugMsgRenderTarget.Clear()
// draw background
FillRect(
dm.DebugMsgRenderTarget,
rect,
color.NRGBA{255, 255, 255, 255},
)
FillRect(
dm.DebugMsgRenderTarget,
rect.Inset(2),
color.NRGBA{0, 0, 0, 255},
)
// draw text
{
op := &DrawTextOptions{}
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(
hozMargin, vertMargin,
)
op.ColorScale.ScaleWithColor(color.NRGBA{255, 255, 255, 255})
op.LayoutOptions.LineSpacing = faceLineSpacing
DrawText(dm.DebugMsgRenderTarget, text, ClearFace, op)
}
// draw DebugMsgRenderTarget
{
dstRect := RectToFRect(dst.Bounds())
op := &DrawImageOptions{}
op.GeoM.Translate(dstRect.Max.X-boxW, dstRect.Max.Y-boxH)
DrawImage(dst, dm.DebugMsgRenderTarget, op)
}
}
func ClearDebugMsgs() {
dm := &TheDebugPrintManager
dm.DebugMsgs = dm.DebugMsgs[:0]
}