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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-01-07 12:19:18 +01:00
|
|
|
// lastSystemTime is the last system time in the previous Update.
|
|
|
|
lastSystemTime int64
|
2017-08-05 19:09:33 +02:00
|
|
|
|
2018-07-17 19:11:00 +02:00
|
|
|
currentFPS float64
|
|
|
|
currentTPS float64
|
|
|
|
lastUpdated int64
|
|
|
|
fpsCount = 0
|
|
|
|
tpsCount = 0
|
2017-08-05 19:09:33 +02:00
|
|
|
|
2018-09-02 17:35:07 +02:00
|
|
|
onStartCalled bool
|
|
|
|
onStart func()
|
2017-08-05 19:09:33 +02:00
|
|
|
|
|
|
|
m sync.Mutex
|
2017-07-13 17:28:28 +02:00
|
|
|
)
|
|
|
|
|
2017-08-05 19:09:33 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-26 11:04:20 +02:00
|
|
|
func OnStart(f func()) {
|
2017-08-05 18:43:09 +02:00
|
|
|
m.Lock()
|
2018-05-26 11:04:20 +02:00
|
|
|
onStart = f
|
2017-08-05 18:43:09 +02:00
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
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-01-07 12:19:18 +01:00
|
|
|
// Initialize lastSystemTime if needed.
|
|
|
|
if lastSystemTime == 0 {
|
2018-07-16 17:27:42 +02:00
|
|
|
lastSystemTime = now
|
2017-08-05 17:52:12 +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
|
|
|
|
|
2018-07-15 20:02:55 +02:00
|
|
|
if diff > int64(time.Second)*5/60 {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Stabilize FPS.
|
2018-01-07 12:32:18 +01:00
|
|
|
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
|
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
|
|
|
|
2017-08-05 17:52:12 +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) {
|
|
|
|
if lastUpdated == 0 {
|
|
|
|
lastUpdated = now
|
2018-07-16 17:27:42 +02:00
|
|
|
}
|
2018-07-17 19:11:00 +02:00
|
|
|
fpsCount++
|
|
|
|
tpsCount += count
|
|
|
|
if time.Second > time.Duration(now-lastUpdated) {
|
2018-07-16 17:27:42 +02:00
|
|
|
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-16 17:27:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 19:11:00 +02:00
|
|
|
const UncappedTPS = -1
|
|
|
|
|
2018-07-16 17:27:42 +02:00
|
|
|
// 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.
|
2018-07-16 17:27:42 +02:00
|
|
|
// 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.
|
2018-07-16 17:27:42 +02:00
|
|
|
//
|
|
|
|
// Update is expected to be called per frame.
|
|
|
|
func Update(tps int) int {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-09-02 17:35:07 +02:00
|
|
|
if !onStartCalled {
|
2018-07-16 17:27:42 +02:00
|
|
|
if onStart != nil {
|
|
|
|
onStart()
|
2018-09-02 17:35:07 +02:00
|
|
|
onStartCalled = true
|
2018-07-16 17:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := now()
|
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-16 18:42:19 +02:00
|
|
|
}
|
2018-07-17 19:11:00 +02:00
|
|
|
updateFPSAndTPS(n, c)
|
|
|
|
return c
|
2018-07-16 17:27:42 +02:00
|
|
|
}
|