-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.go
63 lines (53 loc) · 1.48 KB
/
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
63
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
"image/color"
)
type debugFunc func() string
// Encapsulates all debugging information.
type Debug struct {
showDebug bool
debugFunctions []debugFunc
}
var debug = new(Debug)
func init() {
AddDebugMessage(func() string {
return fmt.Sprintf("Levelcode %s", randomSeed.Code)
})
AddDebugMessage(func() string {
return fmt.Sprintf("X=%.2v", gameState.player.Body.X)
})
AddDebugMessage(func() string {
return fmt.Sprintf("Translation=%.2f", -float64(gameState.player.Body.X)-width/2)
})
}
func AddDebugMessage(f debugFunc) {
debug.debugFunctions = append(debug.debugFunctions, f)
}
func CheckDebugKey() {
if inpututil.IsKeyJustReleased(ebiten.KeyD) {
debug.showDebug = !debug.showDebug
}
}
func (*Debug) Draw(screen *ebiten.Image) {
if !debug.showDebug {
return
}
// Show text
rowHeight := 14
y := 50 // Draw under level code display.
for _, function := range debug.debugFunctions {
msg := function()
ebitenutil.DebugPrintAt(screen, msg, 10, y)
y += rowHeight
}
// Show cursor position.
px, py := ebiten.CursorPosition()
crossColor := color.RGBA{80, 80, 80, 255}
ebitenutil.DrawLine(screen, float64(px), 0, float64(px), float64(height), crossColor)
ebitenutil.DrawLine(screen, 0, float64(py), float64(width), float64(py), crossColor)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("%v/%v", px, py), px+5, py+10)
}