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 (
|
2018-05-09 16:41:06 +02:00
|
|
|
"sync"
|
2017-08-05 17:18:38 +02:00
|
|
|
"time"
|
2017-07-13 17:28:28 +02:00
|
|
|
)
|
|
|
|
|
2022-07-12 05:34:50 +02:00
|
|
|
const (
|
|
|
|
DefaultTPS = 60
|
|
|
|
SyncWithFPS = -1
|
|
|
|
)
|
|
|
|
|
2017-07-13 17:28:28 +02:00
|
|
|
var (
|
2022-07-12 05:34:50 +02:00
|
|
|
// tps represents TPS (ticks per second).
|
|
|
|
tps = DefaultTPS
|
|
|
|
|
2019-05-24 15:48:24 +02:00
|
|
|
lastNow int64
|
|
|
|
|
2018-01-07 12:19:18 +01:00
|
|
|
// lastSystemTime is the last system time in the previous Update.
|
2019-05-24 15:48:24 +02:00
|
|
|
// lastSystemTime indicates the logical time in the game, so this can be bigger than the curren time.
|
2018-01-07 12:19:18 +01:00
|
|
|
lastSystemTime int64
|
2017-08-05 19:09:33 +02:00
|
|
|
|
2022-04-13 17:57:47 +02:00
|
|
|
actualFPS float64
|
|
|
|
actualTPS float64
|
2022-04-13 17:56:37 +02:00
|
|
|
prevTPS int64
|
2018-07-17 19:11:00 +02:00
|
|
|
lastUpdated int64
|
|
|
|
fpsCount = 0
|
|
|
|
tpsCount = 0
|
2017-08-05 19:09:33 +02:00
|
|
|
|
|
|
|
m sync.Mutex
|
2017-07-13 17:28:28 +02:00
|
|
|
)
|
|
|
|
|
2019-05-24 15:48:24 +02:00
|
|
|
func init() {
|
|
|
|
n := now()
|
|
|
|
lastNow = n
|
|
|
|
lastSystemTime = n
|
|
|
|
lastUpdated = n
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:57:47 +02:00
|
|
|
func ActualFPS() float64 {
|
2017-08-05 19:09:33 +02:00
|
|
|
m.Lock()
|
2022-04-13 17:57:47 +02:00
|
|
|
v := actualFPS
|
2017-08-05 19:09:33 +02:00
|
|
|
m.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:57:47 +02:00
|
|
|
func ActualTPS() float64 {
|
2018-07-17 19:11:00 +02:00
|
|
|
m.Lock()
|
2022-04-13 17:57:47 +02:00
|
|
|
v := actualTPS
|
2018-07-17 19:11:00 +02:00
|
|
|
m.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-04-24 16:00:56 +02:00
|
|
|
func max(a, b int64) int64 {
|
2020-12-20 13:20:59 +01:00
|
|
|
if a < b {
|
2021-04-24 16:00:56 +02:00
|
|
|
return b
|
2020-12-20 13:20:59 +01:00
|
|
|
}
|
2021-04-24 16:00:56 +02:00
|
|
|
return a
|
2020-12-20 15:40:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:27:42 +02:00
|
|
|
func calcCountFromTPS(tps int64, now int64) int {
|
|
|
|
if tps == 0 {
|
|
|
|
return 0
|
2017-08-05 19:09:33 +02:00
|
|
|
}
|
2018-07-16 17:27:42 +02:00
|
|
|
if tps < 0 {
|
|
|
|
panic("clock: tps must >= 0")
|
2017-08-05 19:09:33 +02:00
|
|
|
}
|
2018-05-26 11:04:20 +02:00
|
|
|
|
2018-07-16 17:27:42 +02:00
|
|
|
diff := now - lastSystemTime
|
2018-01-07 12:32:18 +01:00
|
|
|
if diff < 0 {
|
2017-08-05 17:52:12 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:18:38 +02:00
|
|
|
count := 0
|
2018-01-07 08:43:01 +01:00
|
|
|
syncWithSystemClock := false
|
|
|
|
|
2021-04-24 16:00:56 +02:00
|
|
|
// Detect whether the previous time is too old.
|
2021-04-24 16:24:11 +02:00
|
|
|
// Use either 5 ticks or 5/60 sec in the case when TPS is too big like 300 (#1444).
|
2022-04-13 17:56:37 +02:00
|
|
|
if diff > max(int64(time.Second)*5/tps, int64(time.Second)*5/60) || prevTPS != tps {
|
2018-05-26 17:30:08 +02:00
|
|
|
// 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
|
2017-08-05 17:18:38 +02:00
|
|
|
} else {
|
2018-07-16 17:27:42 +02:00
|
|
|
count = int(diff * tps / int64(time.Second))
|
2017-08-05 17:18:38 +02:00
|
|
|
}
|
2022-04-13 17:56:37 +02:00
|
|
|
prevTPS = tps
|
2017-08-05 17:18:38 +02:00
|
|
|
|
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.
|
2018-07-16 17:27:42 +02:00
|
|
|
if count == 0 && (int64(time.Second)/tps/2) < diff {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
2018-07-16 17:27:42 +02:00
|
|
|
if count == 2 && (int64(time.Second)/tps*3/2) > diff {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
|
2018-01-07 08:43:01 +01:00
|
|
|
if syncWithSystemClock {
|
2018-07-16 17:27:42 +02:00
|
|
|
lastSystemTime = now
|
2017-08-05 17:52:12 +02:00
|
|
|
} else {
|
2018-07-16 17:27:42 +02:00
|
|
|
lastSystemTime += int64(count) * int64(time.Second) / tps
|
2017-08-05 17:52:12 +02:00
|
|
|
}
|
2017-08-05 19:09:33 +02:00
|
|
|
|
2021-04-24 16:00:56 +02:00
|
|
|
return count
|
2017-07-13 17:28:28 +02:00
|
|
|
}
|
2018-07-16 17:27:42 +02:00
|
|
|
|
2018-07-17 19:11:00 +02:00
|
|
|
func updateFPSAndTPS(now int64, count int) {
|
|
|
|
fpsCount++
|
|
|
|
tpsCount += count
|
2019-05-23 19:24:18 +02:00
|
|
|
if now < lastUpdated {
|
2019-05-24 15:48:24 +02:00
|
|
|
panic("clock: lastUpdated must be older than now")
|
2019-05-23 19:24:18 +02:00
|
|
|
}
|
2018-07-17 19:11:00 +02:00
|
|
|
if time.Second > time.Duration(now-lastUpdated) {
|
2018-07-16 17:27:42 +02:00
|
|
|
return
|
|
|
|
}
|
2022-04-13 17:57:47 +02:00
|
|
|
actualFPS = float64(fpsCount) * float64(time.Second) / float64(now-lastUpdated)
|
|
|
|
actualTPS = float64(tpsCount) * float64(time.Second) / float64(now-lastUpdated)
|
2018-07-17 19:11:00 +02:00
|
|
|
lastUpdated = now
|
|
|
|
fpsCount = 0
|
|
|
|
tpsCount = 0
|
2018-07-16 17:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the inner clock state and returns an integer value
|
2022-07-12 05:34:50 +02:00
|
|
|
// indicating how many times the game should update based on the current tps.
|
|
|
|
//
|
2021-07-22 15:44:58 +02:00
|
|
|
// If tps is SyncWithFPS, Update always returns 1.
|
|
|
|
// If tps <= 0 and not SyncWithFPS, Update always returns 0.
|
2018-07-16 17:27:42 +02:00
|
|
|
//
|
|
|
|
// Update is expected to be called per frame.
|
2022-07-12 05:34:50 +02:00
|
|
|
func Update() int {
|
2018-07-16 17:27:42 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
n := now()
|
2019-05-24 15:48:24 +02:00
|
|
|
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
|
2021-07-22 15:44:58 +02:00
|
|
|
if tps == SyncWithFPS {
|
2018-07-17 19:11:00 +02:00
|
|
|
c = 1
|
|
|
|
} else if tps > 0 {
|
|
|
|
c = calcCountFromTPS(int64(tps), n)
|
2018-07-16 18:42:19 +02:00
|
|
|
}
|
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
|
2018-07-16 17:27:42 +02:00
|
|
|
}
|
2022-07-12 05:34:50 +02:00
|
|
|
|
|
|
|
func SetTPS(newTPS int) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
tps = newTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
func TPS() int {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
return tps
|
|
|
|
}
|