2021-07-23 13:12:54 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-07-30 17:26:23 +10:00
|
|
|
"encoding/gob"
|
2021-07-23 13:12:54 +10:00
|
|
|
"fmt"
|
2021-08-27 14:12:31 +10:00
|
|
|
"image"
|
2021-07-23 17:05:05 +10:00
|
|
|
"math"
|
2021-07-23 13:12:54 +10:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
)
|
|
|
|
|
2021-09-01 09:48:01 +10:00
|
|
|
var (
|
|
|
|
_ interface {
|
|
|
|
Drawer
|
|
|
|
Hider
|
|
|
|
} = &PerfDisplay{}
|
|
|
|
|
|
|
|
_ interface {
|
|
|
|
Drawer
|
|
|
|
Hider
|
|
|
|
Updater
|
|
|
|
} = &DebugToast{}
|
|
|
|
)
|
2021-08-18 16:34:51 +10:00
|
|
|
|
2021-07-30 17:26:23 +10:00
|
|
|
func init() {
|
2021-09-01 09:48:01 +10:00
|
|
|
gob.Register(&DebugToast{})
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&PerfDisplay{})
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:33:21 +10:00
|
|
|
// DebugToast debugprints a string for a while, then disappears.
|
|
|
|
type DebugToast struct {
|
|
|
|
ID
|
|
|
|
Hidden
|
2021-08-27 14:12:31 +10:00
|
|
|
Pos image.Point
|
2021-08-24 19:33:21 +10:00
|
|
|
Timer int // ticks
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2021-09-01 09:48:01 +10:00
|
|
|
func (d *DebugToast) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
|
2021-08-27 14:12:31 +10:00
|
|
|
ebitenutil.DebugPrintAt(screen, d.Text, d.Pos.X, d.Pos.Y)
|
2021-08-24 19:33:21 +10:00
|
|
|
}
|
|
|
|
|
2021-09-09 19:11:25 +10:00
|
|
|
func (d *DebugToast) DrawOrder() float64 {
|
2021-09-02 15:55:38 +10:00
|
|
|
// Always draw on top
|
2021-09-09 19:11:25 +10:00
|
|
|
return math.MaxFloat64
|
2021-08-24 19:33:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DebugToast) Toast(text string) {
|
|
|
|
d.Text = text
|
|
|
|
d.Timer = 120
|
|
|
|
d.Hidden = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DebugToast) Update() error {
|
|
|
|
if d.Hidden = d.Timer <= 0; !d.Hidden {
|
|
|
|
d.Timer--
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:03:10 +10:00
|
|
|
// PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.
|
2021-08-01 17:08:26 +10:00
|
|
|
type PerfDisplay struct {
|
2021-08-24 19:33:21 +10:00
|
|
|
Hidden
|
2021-08-01 17:08:26 +10:00
|
|
|
}
|
2021-07-23 13:12:54 +10:00
|
|
|
|
2021-09-01 09:48:01 +10:00
|
|
|
func (p PerfDisplay) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions) {
|
2021-07-25 15:03:10 +10:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f FPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
|
2021-07-23 13:12:54 +10:00
|
|
|
}
|
2021-07-23 13:46:19 +10:00
|
|
|
|
2021-09-09 19:11:25 +10:00
|
|
|
func (PerfDisplay) DrawOrder() float64 {
|
2021-07-23 17:05:05 +10:00
|
|
|
// Always draw on top
|
2021-09-09 19:11:25 +10:00
|
|
|
return math.MaxFloat64
|
2021-07-23 13:46:19 +10:00
|
|
|
}
|