59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package system
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"code.rocketnine.space/tslocum/gohan-bunnymark/component"
|
|
"code.rocketnine.space/tslocum/gohan-bunnymark/world"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
|
"golang.org/x/image/colornames"
|
|
"golang.org/x/image/font/basicfont"
|
|
)
|
|
|
|
type Metrics struct {
|
|
Settings *component.Settings
|
|
}
|
|
|
|
func (m *Metrics) Update(_ gohan.Entity) error {
|
|
s := m.Settings
|
|
|
|
select {
|
|
case <-s.Ticker.C:
|
|
s.Objects.Update(float64(gohan.CurrentEntities()))
|
|
s.Tps.Update(ebiten.CurrentTPS())
|
|
s.Fps.Update(ebiten.CurrentFPS())
|
|
default:
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Metrics) Draw(_ gohan.Entity, screen *ebiten.Image) error {
|
|
s := m.Settings
|
|
|
|
str := fmt.Sprintf(
|
|
"GPU: %s\nTPS: %.2f, FPS: %.2f, Objects: %.f\nBatching: %t, Amount: %d\nResolution: %dx%d",
|
|
s.Gpu, s.Tps.Last(), s.Fps.Last(), s.Objects.Last(),
|
|
!s.Colorful, s.Amount,
|
|
world.ScreenWidth, world.ScreenHeight,
|
|
)
|
|
|
|
rect := text.BoundString(basicfont.Face7x13, str)
|
|
width, height := float64(rect.Dx()), float64(rect.Dy())
|
|
|
|
padding := 20.0
|
|
rectW, rectH := width+padding, height+padding
|
|
plotW, plotH := 100.0, 40.0
|
|
|
|
ebitenutil.DrawRect(screen, 0, 0, rectW, rectH, color.RGBA{A: 128})
|
|
text.Draw(screen, str, basicfont.Face7x13, int(padding)/2, 10+int(padding)/2, colornames.White)
|
|
|
|
s.Tps.Draw(screen, 0, padding+rectH, plotW, plotH)
|
|
s.Fps.Draw(screen, 0, padding+rectH*2, plotW, plotH)
|
|
s.Objects.Draw(screen, 0, padding+rectH*3, plotW, plotH)
|
|
return nil
|
|
}
|