ichigo/engine/debug.go
2021-09-17 13:59:38 +10:00

95 lines
1.7 KiB
Go

package engine
import (
"encoding/gob"
"fmt"
"image"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
var (
_ interface {
Drawer
Hider
} = &PerfDisplay{}
_ interface {
Drawer
Hider
Updater
} = &DebugToast{}
)
func init() {
gob.Register(&DebugToast{})
gob.Register(&PerfDisplay{})
}
// DebugToast debugprints a string for a while, then disappears.
type DebugToast struct {
ID
Hides
Pos image.Point
Timer int // ticks
Text string
}
func (d *DebugToast) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y)
}
// Draw last.
func (DebugToast) DrawAfter(x Drawer) bool {
switch x.(type) {
case *DebugToast, PerfDisplay:
return false
}
return true
}
func (DebugToast) DrawBefore(x Drawer) bool {
return false
}
func (d *DebugToast) String() string {
return fmt.Sprintf("DebugToast@%v", d.Pos)
}
func (d *DebugToast) Toast(text string) {
d.Text = text
d.Timer = 120
d.Hides = false
}
func (d *DebugToast) Update() error {
if d.Hides = d.Timer <= 0; !d.Hides {
d.Timer--
}
return nil
}
// PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
type PerfDisplay struct {
Hides
}
func (p PerfDisplay) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
}
// Draw last.
func (PerfDisplay) DrawAfter(x Drawer) bool {
switch x.(type) {
case *DebugToast, PerfDisplay:
return false
}
return true
}
func (PerfDisplay) DrawBefore(Drawer) bool {
return false
}
func (PerfDisplay) String() string { return "PerfDisplay" }