mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
clock: Assert that now() must be monotonic
This change adds panics to ensure that now() must be monotonic. Bug: #875
This commit is contained in:
parent
b2c6ddf7f6
commit
f9cec31bf6
@ -21,7 +21,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
lastNow int64
|
||||||
|
|
||||||
// lastSystemTime is the last system time in the previous Update.
|
// lastSystemTime is the last system time in the previous Update.
|
||||||
|
// lastSystemTime indicates the logical time in the game, so this can be bigger than the curren time.
|
||||||
lastSystemTime int64
|
lastSystemTime int64
|
||||||
|
|
||||||
currentFPS float64
|
currentFPS float64
|
||||||
@ -33,6 +36,13 @@ var (
|
|||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
n := now()
|
||||||
|
lastNow = n
|
||||||
|
lastSystemTime = n
|
||||||
|
lastUpdated = n
|
||||||
|
}
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
v := currentFPS
|
v := currentFPS
|
||||||
@ -55,15 +65,8 @@ func calcCountFromTPS(tps int64, now int64) int {
|
|||||||
panic("clock: tps must >= 0")
|
panic("clock: tps must >= 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize lastSystemTime if needed.
|
|
||||||
if lastSystemTime == 0 {
|
|
||||||
lastSystemTime = now
|
|
||||||
}
|
|
||||||
|
|
||||||
diff := now - lastSystemTime
|
diff := now - lastSystemTime
|
||||||
if diff < 0 {
|
if diff < 0 {
|
||||||
// TODO: Should this panic?
|
|
||||||
lastSystemTime = now
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,17 +100,10 @@ func calcCountFromTPS(tps int64, now int64) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateFPSAndTPS(now int64, count int) {
|
func updateFPSAndTPS(now int64, count int) {
|
||||||
if lastUpdated == 0 {
|
|
||||||
lastUpdated = now
|
|
||||||
}
|
|
||||||
fpsCount++
|
fpsCount++
|
||||||
tpsCount += count
|
tpsCount += count
|
||||||
if now < lastUpdated {
|
if now < lastUpdated {
|
||||||
// TODO: Should this panic?
|
panic("clock: lastUpdated must be older than now")
|
||||||
lastUpdated = now
|
|
||||||
fpsCount = 0
|
|
||||||
tpsCount = 0
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if time.Second > time.Duration(now-lastUpdated) {
|
if time.Second > time.Duration(now-lastUpdated) {
|
||||||
return
|
return
|
||||||
@ -133,6 +129,11 @@ func Update(tps int) int {
|
|||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
n := now()
|
n := now()
|
||||||
|
if lastNow > n {
|
||||||
|
// This ensures that now() must be monotonic (#875).
|
||||||
|
panic("clock: lastNow must be older than n")
|
||||||
|
}
|
||||||
|
lastNow = n
|
||||||
|
|
||||||
c := 0
|
c := 0
|
||||||
if tps == UncappedTPS {
|
if tps == UncappedTPS {
|
||||||
|
Loading…
Reference in New Issue
Block a user