mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Implement CurrentTPS
This commit is contained in:
parent
e99f2f9f69
commit
0593b77eb8
@ -176,8 +176,8 @@ func update(screen *ebiten.Image) error {
|
||||
|
||||
x, y := ebiten.CursorPosition()
|
||||
tpsStr := "Uncapped"
|
||||
if maxTPS := ebiten.MaxTPS(); maxTPS != ebiten.UncappedTPS {
|
||||
tpsStr = fmt.Sprintf("%d", maxTPS)
|
||||
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
|
||||
tpsStr = fmt.Sprintf("%d", t)
|
||||
}
|
||||
msg := fmt.Sprintf(`Press arrow keys to change the window size
|
||||
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
|
||||
Cursor: (%d, %d)
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
@ -84,9 +84,6 @@ func (c *graphicsContext) initializeIfNeeded() error {
|
||||
func (c *graphicsContext) Update(afterFrameUpdate func()) error {
|
||||
tps := int(MaxTPS())
|
||||
updateCount := clock.Update(tps)
|
||||
if tps == UncappedTPS {
|
||||
updateCount = 1
|
||||
}
|
||||
|
||||
if err := c.initializeIfNeeded(); err != nil {
|
||||
return err
|
||||
|
@ -25,8 +25,10 @@ var (
|
||||
lastSystemTime int64
|
||||
|
||||
currentFPS float64
|
||||
lastFPSUpdated int64
|
||||
framesForFPS int64
|
||||
currentTPS float64
|
||||
lastUpdated int64
|
||||
fpsCount = 0
|
||||
tpsCount = 0
|
||||
|
||||
started bool
|
||||
onStart func()
|
||||
@ -41,6 +43,13 @@ func CurrentFPS() float64 {
|
||||
return v
|
||||
}
|
||||
|
||||
func CurrentTPS() float64 {
|
||||
m.Lock()
|
||||
v := currentTPS
|
||||
m.Unlock()
|
||||
return v
|
||||
}
|
||||
|
||||
func OnStart(f func()) {
|
||||
m.Lock()
|
||||
onStart = f
|
||||
@ -94,23 +103,29 @@ func calcCountFromTPS(tps int64, now int64) int {
|
||||
return count
|
||||
}
|
||||
|
||||
func updateFPS(now int64) {
|
||||
if lastFPSUpdated == 0 {
|
||||
lastFPSUpdated = now
|
||||
func updateFPSAndTPS(now int64, count int) {
|
||||
if lastUpdated == 0 {
|
||||
lastUpdated = now
|
||||
}
|
||||
framesForFPS++
|
||||
if time.Second > time.Duration(now-lastFPSUpdated) {
|
||||
fpsCount++
|
||||
tpsCount += count
|
||||
if time.Second > time.Duration(now-lastUpdated) {
|
||||
return
|
||||
}
|
||||
currentFPS = float64(framesForFPS) * float64(time.Second) / float64(now-lastFPSUpdated)
|
||||
lastFPSUpdated = now
|
||||
framesForFPS = 0
|
||||
currentFPS = float64(fpsCount) * float64(time.Second) / float64(now-lastUpdated)
|
||||
currentTPS = float64(tpsCount) * float64(time.Second) / float64(now-lastUpdated)
|
||||
lastUpdated = now
|
||||
fpsCount = 0
|
||||
tpsCount = 0
|
||||
}
|
||||
|
||||
const UncappedTPS = -1
|
||||
|
||||
// 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).
|
||||
// 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.
|
||||
func Update(tps int) int {
|
||||
@ -125,9 +140,12 @@ func Update(tps int) int {
|
||||
}
|
||||
|
||||
n := now()
|
||||
updateFPS(n)
|
||||
if tps > 0 {
|
||||
return calcCountFromTPS(int64(tps), n)
|
||||
c := 0
|
||||
if tps == UncappedTPS {
|
||||
c = 1
|
||||
} else if tps > 0 {
|
||||
c = calcCountFromTPS(int64(tps), n)
|
||||
}
|
||||
return 0
|
||||
updateFPSAndTPS(n, c)
|
||||
return c
|
||||
}
|
||||
|
8
run.go
8
run.go
@ -564,8 +564,14 @@ func MaxTPS() int {
|
||||
return int(atomic.LoadInt32(¤tMaxTPS))
|
||||
}
|
||||
|
||||
// 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.
|
||||
const UncappedTPS = -1
|
||||
const UncappedTPS = clock.UncappedTPS
|
||||
|
||||
// SetMaxTPS sets the maximum TPS (ticks per second),
|
||||
// that represents how many updating function is called per second.
|
||||
|
Loading…
Reference in New Issue
Block a user