ebiten/internal/clock/clock.go

185 lines
4.2 KiB
Go
Raw Normal View History

2017-07-13 17:28:28 +02:00
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-01-07 08:43:01 +01:00
// Package clock manages game timers.
2017-07-13 17:28:28 +02:00
package clock
import (
"sync"
"time"
2017-07-13 17:28:28 +02:00
)
var (
lastNow int64
// 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
2018-07-17 19:11:00 +02:00
currentFPS float64
currentTPS float64
lastTPS int64
tpsCalcErr int64
2018-07-17 19:11:00 +02:00
lastUpdated int64
fpsCount = 0
tpsCount = 0
m sync.Mutex
2017-07-13 17:28:28 +02:00
)
func init() {
n := now()
lastNow = n
lastSystemTime = n
lastUpdated = n
}
func CurrentFPS() float64 {
m.Lock()
v := currentFPS
m.Unlock()
return v
}
2018-07-17 19:11:00 +02:00
func CurrentTPS() float64 {
m.Lock()
v := currentTPS
m.Unlock()
return v
}
2020-12-20 13:54:28 +01:00
func min(a, b int64) int64 {
if a < b {
2020-12-20 13:54:28 +01:00
return a
}
2020-12-20 13:54:28 +01:00
return b
}
// calcTPSFactor calculates the TPS that is used for the timer and the factor for the count.
// If tps is under the baseTPS, use tps as newTPS. The factor is 1. The timer precision should be enough.
// If not, use baseTPS as newTPS and factor that can be more than 1.
func calcTPSFactor(tps, baseTPS int64) (newTPS int64, factor int) {
if tps <= baseTPS {
return tps, 1
}
if lastTPS != tps {
tpsCalcErr = 0
}
lastTPS = tps
factor = int(tps / baseTPS)
tpsCalcErr += tps - baseTPS*int64(factor)
factor += int(tpsCalcErr / baseTPS)
tpsCalcErr %= baseTPS
return baseTPS, factor
}
func calcCountFromTPS(tps int64, now int64) int {
if tps == 0 {
return 0
}
if tps < 0 {
panic("clock: tps must >= 0")
}
diff := now - lastSystemTime
2018-01-07 12:32:18 +01:00
if diff < 0 {
2017-08-05 17:52:12 +02:00
return 0
}
count := 0
2018-01-07 08:43:01 +01:00
syncWithSystemClock := false
// When TPS is big (e.g. 300), the timer precision is no longer reliable.
// Multiply the factor later instead (#1444).
var tpsFactor int
tps, tpsFactor = calcTPSFactor(tps, 60) // TODO: 60 should be the current display's FPS.
if diff > int64(time.Second)*5/tps {
// The previous time is too old.
// Let's force to sync the game time with the system clock.
2018-01-07 08:43:01 +01:00
syncWithSystemClock = true
} else {
count = int(diff * tps / int64(time.Second))
}
2020-10-17 09:08:41 +02:00
// Stabilize the count.
2018-01-07 12:32:18 +01:00
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
2020-12-20 13:58:43 +01:00
// TODO: Brush up this logic so that this will work with any FPS. Now this works only when FPS = TPS.
if count == 0 && (int64(time.Second)/tps/2) < diff {
count = 1
}
if count == 2 && (int64(time.Second)/tps*3/2) > diff {
count = 1
}
2018-01-07 08:43:01 +01:00
if syncWithSystemClock {
lastSystemTime = now
2017-08-05 17:52:12 +02:00
} else {
lastSystemTime += int64(count) * int64(time.Second) / tps
2017-08-05 17:52:12 +02:00
}
return count * tpsFactor
2017-07-13 17:28:28 +02:00
}
2018-07-17 19:11:00 +02:00
func updateFPSAndTPS(now int64, count int) {
fpsCount++
tpsCount += count
if now < lastUpdated {
panic("clock: lastUpdated must be older than now")
}
2018-07-17 19:11:00 +02:00
if time.Second > time.Duration(now-lastUpdated) {
return
}
2018-07-17 19:11:00 +02:00
currentFPS = float64(fpsCount) * float64(time.Second) / float64(now-lastUpdated)
currentTPS = float64(tpsCount) * float64(time.Second) / float64(now-lastUpdated)
lastUpdated = now
fpsCount = 0
tpsCount = 0
}
2018-07-17 19:11:00 +02:00
const UncappedTPS = -1
// Update updates the inner clock state and returns an integer value
2018-07-17 19:11:00 +02:00
// indicating how many times the game should update based on given tps.
// tps represents TPS (ticks per second).
2018-07-17 19:11:00 +02:00
// 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 {
m.Lock()
defer m.Unlock()
n := now()
if lastNow > n {
// This ensures that now() must be monotonic (#875).
panic("clock: lastNow must be older than n")
}
lastNow = n
2019-05-08 19:44:14 +02:00
2018-07-17 19:11:00 +02:00
c := 0
if tps == UncappedTPS {
c = 1
} else if tps > 0 {
c = calcCountFromTPS(int64(tps), n)
}
2018-07-17 19:11:00 +02:00
updateFPSAndTPS(n, c)
2019-05-08 19:44:14 +02:00
2018-07-17 19:11:00 +02:00
return c
}