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.
|
|
|
|
//
|
2018-01-07 12:19:18 +01:00
|
|
|
// There are two types of clocks internally:
|
2018-01-07 08:43:01 +01:00
|
|
|
//
|
|
|
|
// System clock:
|
|
|
|
// A clock offered by the OS.
|
|
|
|
//
|
|
|
|
// Audio clock:
|
|
|
|
// An audio clock that is used in the higher priority over the system clock.
|
|
|
|
// An audio clock might not exist when the audio is not used.
|
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
|
|
|
)
|
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
const FPS = 60
|
|
|
|
|
2017-07-13 17:28:28 +02:00
|
|
|
var (
|
2018-01-07 12:19:18 +01:00
|
|
|
frames int64
|
2018-01-07 08:43:01 +01:00
|
|
|
audioTimeInFrames int64
|
|
|
|
lastAudioTimeInFrames int64
|
|
|
|
|
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
|
|
|
|
|
|
|
currentFPS float64
|
|
|
|
lastFPSUpdated int64
|
|
|
|
framesForFPS int64
|
|
|
|
|
|
|
|
ping func()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
func RegisterPing(pingFunc func()) {
|
|
|
|
m.Lock()
|
|
|
|
ping = pingFunc
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-01-07 10:32:12 +01:00
|
|
|
// ProceedAudioTimer increments the audio time by the given number of frames.
|
|
|
|
func ProceedAudioTimer(num int64) {
|
2017-07-13 17:28:28 +02:00
|
|
|
m.Lock()
|
2018-01-07 10:32:12 +01:00
|
|
|
audioTimeInFrames += num
|
2017-07-13 17:28:28 +02:00
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-08-05 19:09:33 +02:00
|
|
|
func updateFPS(now int64) {
|
|
|
|
if lastFPSUpdated == 0 {
|
|
|
|
lastFPSUpdated = now
|
|
|
|
}
|
|
|
|
framesForFPS++
|
|
|
|
if time.Second > time.Duration(now-lastFPSUpdated) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentFPS = float64(framesForFPS) * float64(time.Second) / float64(now-lastFPSUpdated)
|
|
|
|
lastFPSUpdated = now
|
|
|
|
framesForFPS = 0
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
// Update updates the inner clock state and returns an integer value
|
2018-01-07 08:43:01 +01:00
|
|
|
// indicating how many game frames the game should update.
|
2017-08-05 19:12:23 +02:00
|
|
|
func Update() int {
|
2017-07-13 17:28:28 +02:00
|
|
|
m.Lock()
|
2017-08-05 17:18:38 +02:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-08-05 19:12:23 +02:00
|
|
|
n := now()
|
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
if ping != nil {
|
|
|
|
ping()
|
|
|
|
}
|
|
|
|
|
2018-01-07 12:19:18 +01:00
|
|
|
// Initialize lastSystemTime if needed.
|
|
|
|
if lastSystemTime == 0 {
|
|
|
|
lastSystemTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 12:32:18 +01:00
|
|
|
diff := n - lastSystemTime
|
|
|
|
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
|
|
|
|
|
|
|
|
if audioTimeInFrames > 0 && lastAudioTimeInFrames != audioTimeInFrames {
|
|
|
|
// If the audio clock is updated, use this.
|
|
|
|
if frames < audioTimeInFrames {
|
|
|
|
count = int(audioTimeInFrames - frames)
|
2017-08-05 17:18:38 +02:00
|
|
|
}
|
2018-01-07 08:43:01 +01:00
|
|
|
lastAudioTimeInFrames = audioTimeInFrames
|
2018-01-07 12:19:18 +01:00
|
|
|
|
|
|
|
// Now the current lastSystemTime value is not meaningful,
|
|
|
|
// force to sync lastSystemTime with the system timer.
|
2018-01-07 08:43:01 +01:00
|
|
|
syncWithSystemClock = true
|
2017-08-05 17:18:38 +02:00
|
|
|
} else {
|
2018-01-07 08:43:01 +01:00
|
|
|
// Use system clock when the audio clock is not updated yet.
|
2018-01-07 12:32:18 +01:00
|
|
|
// As the audio clock can be updated discountinuously,
|
|
|
|
// the system clock is still needed.
|
2017-08-05 17:18:38 +02:00
|
|
|
|
2018-01-07 12:32:18 +01:00
|
|
|
if diff > 5*int64(time.Second)/FPS {
|
2017-08-05 17:23:31 +02:00
|
|
|
// The previous time is too old.
|
2018-01-07 08:43:01 +01:00
|
|
|
// Let's force to sync the game time with the system clock.
|
|
|
|
syncWithSystemClock = true
|
2017-08-05 17:52:12 +02:00
|
|
|
} else {
|
2018-01-07 12:32:18 +01:00
|
|
|
count = int(diff * FPS / 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, ...
|
|
|
|
if count == 0 && (int64(time.Second)/FPS/2) < diff {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
2018-01-07 12:32:18 +01:00
|
|
|
if count == 2 && (int64(time.Second)/FPS*3/2) > diff {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
frames += int64(count)
|
2018-01-07 08:43:01 +01:00
|
|
|
if syncWithSystemClock {
|
2018-01-07 12:19:18 +01:00
|
|
|
lastSystemTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
} else {
|
2018-01-07 12:19:18 +01:00
|
|
|
lastSystemTime += int64(count) * int64(time.Second) / FPS
|
2017-08-05 17:52:12 +02:00
|
|
|
}
|
2017-08-05 19:09:33 +02:00
|
|
|
|
2017-08-05 19:12:23 +02:00
|
|
|
updateFPS(n)
|
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
|
|
|
}
|