ui: Implement CurrentTPS

This commit is contained in:
Hajime Hoshi 2018-07-18 02:11:00 +09:00
parent e99f2f9f69
commit 0593b77eb8
4 changed files with 45 additions and 24 deletions

View File

@ -176,8 +176,8 @@ func update(screen *ebiten.Image) error {
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
tpsStr := "Uncapped" tpsStr := "Uncapped"
if maxTPS := ebiten.MaxTPS(); maxTPS != ebiten.UncappedTPS { if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
tpsStr = fmt.Sprintf("%d", maxTPS) tpsStr = fmt.Sprintf("%d", t)
} }
msg := fmt.Sprintf(`Press arrow keys to change the window size msg := fmt.Sprintf(`Press arrow keys to change the window size
Press S key to change the window scale Press S key to change the window scale
@ -190,7 +190,7 @@ Press T key to switch TPS (ticks per second)
Press Q key to quit Press Q key to quit
Cursor: (%d, %d) Cursor: (%d, %d)
FPS: %0.2f FPS: %0.2f
TPS: %s`, x, y, ebiten.CurrentFPS(), tpsStr) TPS: Current: %0.2f / Max: %s`, x, y, ebiten.CurrentFPS(), ebiten.CurrentTPS(), tpsStr)
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil return nil
} }

View File

@ -84,9 +84,6 @@ func (c *graphicsContext) initializeIfNeeded() error {
func (c *graphicsContext) Update(afterFrameUpdate func()) error { func (c *graphicsContext) Update(afterFrameUpdate func()) error {
tps := int(MaxTPS()) tps := int(MaxTPS())
updateCount := clock.Update(tps) updateCount := clock.Update(tps)
if tps == UncappedTPS {
updateCount = 1
}
if err := c.initializeIfNeeded(); err != nil { if err := c.initializeIfNeeded(); err != nil {
return err return err

View File

@ -24,9 +24,11 @@ var (
// lastSystemTime is the last system time in the previous Update. // lastSystemTime is the last system time in the previous Update.
lastSystemTime int64 lastSystemTime int64
currentFPS float64 currentFPS float64
lastFPSUpdated int64 currentTPS float64
framesForFPS int64 lastUpdated int64
fpsCount = 0
tpsCount = 0
started bool started bool
onStart func() onStart func()
@ -41,6 +43,13 @@ func CurrentFPS() float64 {
return v return v
} }
func CurrentTPS() float64 {
m.Lock()
v := currentTPS
m.Unlock()
return v
}
func OnStart(f func()) { func OnStart(f func()) {
m.Lock() m.Lock()
onStart = f onStart = f
@ -94,23 +103,29 @@ func calcCountFromTPS(tps int64, now int64) int {
return count return count
} }
func updateFPS(now int64) { func updateFPSAndTPS(now int64, count int) {
if lastFPSUpdated == 0 { if lastUpdated == 0 {
lastFPSUpdated = now lastUpdated = now
} }
framesForFPS++ fpsCount++
if time.Second > time.Duration(now-lastFPSUpdated) { tpsCount += count
if time.Second > time.Duration(now-lastUpdated) {
return return
} }
currentFPS = float64(framesForFPS) * float64(time.Second) / float64(now-lastFPSUpdated) currentFPS = float64(fpsCount) * float64(time.Second) / float64(now-lastUpdated)
lastFPSUpdated = now currentTPS = float64(tpsCount) * float64(time.Second) / float64(now-lastUpdated)
framesForFPS = 0 lastUpdated = now
fpsCount = 0
tpsCount = 0
} }
const UncappedTPS = -1
// Update updates the inner clock state and returns an integer value // Update updates the inner clock state and returns an integer value
// indicating how many game frames the game should update based on given tps. // indicating how many times the game should update based on given tps.
// tps represents TPS (ticks per second). // tps represents TPS (ticks per second).
// If tps <= 0, Update always returns 0. // If tps is UncappedTPS, Update always returns 1.
// If tps <= 0 and not UncappedTPS, Update always returns 0.
// //
// Update is expected to be called per frame. // Update is expected to be called per frame.
func Update(tps int) int { func Update(tps int) int {
@ -125,9 +140,12 @@ func Update(tps int) int {
} }
n := now() n := now()
updateFPS(n) c := 0
if tps > 0 { if tps == UncappedTPS {
return calcCountFromTPS(int64(tps), n) c = 1
} else if tps > 0 {
c = calcCountFromTPS(int64(tps), n)
} }
return 0 updateFPSAndTPS(n, c)
return c
} }

8
run.go
View File

@ -564,8 +564,14 @@ func MaxTPS() int {
return int(atomic.LoadInt32(&currentMaxTPS)) return int(atomic.LoadInt32(&currentMaxTPS))
} }
// CurrentTPS returns the current TPS (ticks per second),
// that represents how many update function is called in a second.
func CurrentTPS() float64 {
return clock.CurrentTPS()
}
// UncappedTPS is a special TPS value that means the game doesn't have limitation on TPS. // UncappedTPS is a special TPS value that means the game doesn't have limitation on TPS.
const UncappedTPS = -1 const UncappedTPS = clock.UncappedTPS
// SetMaxTPS sets the maximum TPS (ticks per second), // SetMaxTPS sets the maximum TPS (ticks per second),
// that represents how many updating function is called per second. // that represents how many updating function is called per second.