-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller_debug.go
62 lines (50 loc) · 1.88 KB
/
controller_debug.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
package mipix
import "fmt"
import "github.com/hajimehoshi/ebiten/v2"
import "github.com/hajimehoshi/ebiten/v2/ebitenutil" // not desirable, but let's ignore it for the moment
func (self *controller) debugDrawf(format string, args ...any) {
self.debugInfo = append(self.debugInfo, fmt.Sprintf(format, args...))
}
func (self *controller) debugPrintfr(firstTick, lastTick uint64, format string, args ...any) {
if self.currentTick >= firstTick && self.currentTick <= lastTick {
fmt.Printf(format, args...)
}
}
func (self *controller) debugPrintfe(everyNTicks uint64, format string, args ...any) {
if self.currentTick % everyNTicks == 0 {
fmt.Printf(format, args...)
}
}
func (self *controller) debugPrintfk(key ebiten.Key, format string, args ...any) {
if ebiten.IsKeyPressed(key) {
fmt.Printf(format, args...)
}
}
// --- internal ---
func (self *controller) debugDrawAll(target *ebiten.Image) {
if len(self.debugInfo) == 0 { return }
// determine offscreen size
targetBounds := target.Bounds()
targetWidth, targetHeight := float64(targetBounds.Dx()), float64(targetBounds.Dy())
height := 256.0/ebiten.Monitor().DeviceScaleFactor()
width := height*(targetWidth/targetHeight)
offWidth, offHeight := int(width), int(height)
// create offscreen if necessary
if self.debugOffscreen == nil {
self.debugOffscreen = NewOffscreen(offWidth, offHeight)
} else {
currWidth, currHeight := self.debugOffscreen.Size()
if currWidth != offWidth || currHeight != offHeight {
self.debugOffscreen = NewOffscreen(offWidth, offHeight)
} else { // (unless skip draw, but debug is only called if needsRedraw)
self.debugOffscreen.Clear()
}
}
// draw info to offscreen and project
for i, info := range self.debugInfo {
ebitenutil.DebugPrintAt(self.debugOffscreen.Target(), info, 1, 1 + i*12)
}
self.debugOffscreen.Project(target)
// clear debug info
self.debugInfo = self.debugInfo[ : 0]
}