gohan/examples/twinstick/system/input_profile.go

66 lines
1.2 KiB
Go

//go:build example
// +build example
package system
import (
"log"
"os"
"path"
"runtime"
"runtime/pprof"
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type profileSystem struct {
Weapon *component.Weapon
cpuProfile *os.File
}
func NewProfileSystem() *profileSystem {
return &profileSystem{}
}
func (s *profileSystem) Update(_ gohan.Entity) error {
if ebiten.IsKeyPressed(ebiten.KeyControl) && inpututil.IsKeyJustPressed(ebiten.KeyP) {
if s.cpuProfile == nil {
log.Println("CPU profiling started...")
runtime.SetCPUProfileRate(10000)
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
s.cpuProfile, err = os.Create(path.Join(homeDir, "gohan.prof"))
if err != nil {
return err
}
err = pprof.StartCPUProfile(s.cpuProfile)
if err != nil {
return err
}
} else {
pprof.StopCPUProfile()
s.cpuProfile.Close()
s.cpuProfile = nil
log.Println("CPU profiling stopped")
}
}
return nil
}
func (s *profileSystem) Draw(_ gohan.Entity, _ *ebiten.Image) error {
return gohan.ErrUnregister
}