Add support for running in headless mode

This commit is contained in:
Trevor Slocum 2023-01-30 10:04:12 -08:00
parent 2b2e45d7f2
commit 23af24f792
5 changed files with 58 additions and 18 deletions

View File

@ -23,8 +23,10 @@ func parseFlags() {
flag.StringVar(&hostAddress, "host", "", "start hosting a match against a remote opponent at the specified address:port")
flag.StringVar(&connectAddress, "connect", "", "connect to a match hosted by a remote opponent at the specified address:port")
flag.IntVar(&world.LocalPort, "local", 0, "set local port (this should be different from your opponent's local port)")
flag.BoolVar(&printDebug, "debug", false, "enable printing debug messages")
flag.IntVar(&world.Debug, "debug", 0, "debug level (0 - disabled, 1 - print fps and net stats, 2 - draw hitboxes)")
flag.BoolVar(&printDebug, "debug-ggpo", false, "print GGPO debug messages")
flag.IntVar(&world.TPS, "tps", world.DefaultTPS, "set ticks per second (this is not normally required)")
flag.BoolVar(&world.Headless, "headless", false, "run in headless mode (for debug purposes)")
flag.Parse()
if printDebug {

View File

@ -8,6 +8,7 @@ import (
"os"
"strconv"
"strings"
"time"
"code.rocketnine.space/tslocum/boxbrawl/component"
"code.rocketnine.space/tslocum/boxbrawl/entity"
@ -676,6 +677,23 @@ func (g *Game) Draw(screen *ebiten.Image) {
}
}
// RunHeadless runs the game without using the display. Game.Draw is never called.
func (g *Game) RunHeadless() error {
var (
t = time.NewTicker(time.Second / time.Duration(world.TPS))
err error
)
for {
select {
case <-t.C:
err = g.Update()
if err != nil {
return err
}
}
}
}
func (g *Game) Exit() {
os.Exit(0)
}

26
main.go
View File

@ -20,15 +20,6 @@ func main() {
parseFlags()
ebiten.SetTPS(world.TPS)
ebiten.SetFullscreen(world.Fullscreen)
if world.DisableVsync {
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMaximum)
} else {
ebiten.SetFPSMode(ebiten.FPSModeVsyncOn)
}
g, err := game.NewGame()
if err != nil {
log.Fatal(err)
@ -44,6 +35,23 @@ func main() {
g.Exit()
}()
// Run in headless mode.
if world.Headless {
err = g.RunHeadless()
if err != nil {
log.Fatal(err)
}
return
}
// Run normally.
ebiten.SetTPS(world.TPS)
ebiten.SetFullscreen(world.Fullscreen)
if world.DisableVsync {
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMaximum)
} else {
ebiten.SetFPSMode(ebiten.FPSModeVsyncOn)
}
err = ebiten.RunGame(g)
if err != nil {
log.Fatal(err)

View File

@ -17,8 +17,8 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const uiStartPrompt = `Box Brawl
`
const uiStartPrompt = `BOX BRAWL`
const uiBrowserIntro = `Click anywhere to enable keyboard input.`
const uiComputerPrompt = `Press <Enter> to play against the computer.`
const uiHostPrompt = `Press <H> to host a match against a remote player.`
const uiHostInfoPrompt = `Type your opponent's IP address and port (address:port) to host a match.`
@ -66,6 +66,7 @@ func (u *UISystem) updateBuffer() {
prompt := []byte(uiStartPrompt)
if world.WASM {
prompt = append(prompt, []byte("\n\n"+uiBrowserIntro)...)
prompt = append(prompt, []byte("\n\n"+uiComputerPrompt)...)
prompt = append(prompt, []byte("\n\n"+uiBrowserPrompt)...)
} else if world.ConnectionActive {
@ -313,8 +314,10 @@ func (u *UISystem) Draw(e gohan.Entity, screen *ebiten.Image) error {
}
if world.Debug != 0 {
var ping int64
var framesBehind float64
var (
ping int64
framesBehind float64
)
if !world.ConnectPromptVisible && !world.Local {
p1Stats, err := world.Backend.GetNetworkStats(ggpo.PlayerHandle(1))
if err != nil {
@ -333,18 +336,26 @@ func (u *UISystem) Draw(e gohan.Entity, screen *ebiten.Image) error {
ping = yourStats.Network.Ping
framesBehind = math.Round(float64(yourStats.Timesync.LocalFramesBehind))
}
framesLabel := "AHEAD"
if framesBehind > 0 {
framesLabel = "BEHIND"
}
var (
tps float64
fps float64
)
if !world.Headless {
tps = ebiten.ActualTPS()
fps = ebiten.ActualFPS()
}
debugText := fmt.Sprintf("FRAMES %s %.0f\nPING %d\nTPS %0.0f\nFPS %0.0f",
framesLabel,
math.Abs(framesBehind),
ping,
ebiten.ActualTPS(),
ebiten.ActualFPS())
tps,
fps)
ebitenutil.DebugPrintAt(screen, debugText, 2, 0)
}
return nil

View File

@ -41,6 +41,7 @@ var (
Fullscreen bool
DisableVsync bool
Headless bool // Run without using the display
ScreenWidth, ScreenHeight = 0, 0
@ -54,7 +55,7 @@ var (
ConnectPromptConfirmed bool
ConnectionActive bool
Debug = 1 // TODO
Debug int
WASM bool