diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index cb10ac9c9..dad461b51 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -152,7 +152,7 @@ func update(screen *ebiten.Image) error { ebiten.SetRunnableInBackground(runnableInBackground) ebiten.SetCursorVisible(cursorVisible) ebiten.SetVsyncEnabled(vsyncEnabled) - ebiten.SetTPS(tps) + ebiten.SetMaxTPS(tps) if inpututil.IsKeyJustPressed(ebiten.KeyI) { ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) @@ -175,6 +175,10 @@ func update(screen *ebiten.Image) error { screen.DrawImage(gophersImage, op) x, y := ebiten.CursorPosition() + tpsStr := "Uncapped" + if tps := ebiten.TPS(); tps != ebiten.UncappedTPS { + tpsStr = fmt.Sprintf("%d", tps) + } msg := fmt.Sprintf(`Press arrow keys to change the window size Press S key to change the window scale Press F key to switch the fullscreen state @@ -186,7 +190,7 @@ Press T key to switch TPS (ticks per second) Press Q key to quit Cursor: (%d, %d) FPS: %0.2f -TPS: %d`, x, y, ebiten.CurrentFPS(), ebiten.TPS()) +TPS: %s`, x, y, ebiten.CurrentFPS(), tpsStr) ebitenutil.DebugPrint(screen, msg) return nil } diff --git a/graphicscontext.go b/graphicscontext.go index 0b2d2ff18..33da41a32 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -83,11 +83,9 @@ func (c *graphicsContext) initializeIfNeeded() error { func (c *graphicsContext) Update(afterFrameUpdate func()) error { tps := int(TPS()) - count := clock.Update(tps) - - updateCount := 1 - if tps >= 0 { - updateCount = count + updateCount := clock.Update(tps) + if tps == UncappedTPS { + updateCount = 1 } if err := c.initializeIfNeeded(); err != nil { diff --git a/run.go b/run.go index 04c75443f..9aa280aef 100644 --- a/run.go +++ b/run.go @@ -564,13 +564,20 @@ func TPS() int { return int(atomic.LoadInt32(¤tTPS)) } -// SetTPS sets the TPS (ticks per second), that represents how many updating function is called -// per second. +// UncappedTPS is a special TPS value that means the game doesn't have limitation on TPS. +const UncappedTPS = -1 + +// SetMaxTPS sets the maximum TPS (ticks per second), +// that represents how many updating function is called per second. // The initial value is 60. // -// If the given tps is negative, TPS is unfixed and the game is updated per frame. +// If tps is UncappedTPS, TPS is uncapped and the game is updated per frame. +// If tps is negative but not UncappedTPS, SetMaxTPS panics. // -// SetTPS is concurrent-safe. -func SetTPS(tps int) { +// SetMaxTPS is concurrent-safe. +func SetMaxTPS(tps int) { + if tps < 0 && tps != UncappedTPS { + panic("ebiten: tps must be >= 0 or UncappedTPS") + } atomic.StoreInt32(¤tTPS, int32(tps)) }