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.
|
|
|
|
//
|
|
|
|
// There are three types of clocks internally:
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Game clock:
|
|
|
|
// A clock representing the actual game progress.
|
|
|
|
// A game clock is basically updated based on the number of frames.
|
|
|
|
// A game clock is adjusted by the audio clock when needed.
|
2017-07-13 17:28:28 +02:00
|
|
|
package clock
|
|
|
|
|
|
|
|
import (
|
2017-08-05 17:18:38 +02:00
|
|
|
"time"
|
|
|
|
|
2017-07-13 17:28:28 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
|
|
|
)
|
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
const FPS = 60
|
|
|
|
|
2017-07-13 17:28:28 +02:00
|
|
|
var (
|
2018-01-07 08:43:01 +01:00
|
|
|
audioTimeInFrames int64
|
|
|
|
lastAudioTimeInFrames int64
|
|
|
|
|
|
|
|
frames int64
|
|
|
|
gameTime 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 08:43:01 +01:00
|
|
|
// ProceedAudioTimer increments the audio time by a frame.
|
|
|
|
func ProceedAudioTimer() {
|
2017-07-13 17:28:28 +02:00
|
|
|
m.Lock()
|
2018-01-07 08:43:01 +01:00
|
|
|
audioTimeInFrames++
|
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 08:43:01 +01:00
|
|
|
// Initialize gameTime if needed.
|
|
|
|
if gameTime == 0 {
|
|
|
|
gameTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-07 08:43:01 +01:00
|
|
|
t := n - gameTime
|
2017-08-05 17:52:12 +02:00
|
|
|
if t < 0 {
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
// As the audio clock can be updated discountinuously, the system clock is still needed.
|
2017-08-05 17:18:38 +02:00
|
|
|
|
2017-08-05 18:43:09 +02:00
|
|
|
if t > 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 {
|
2017-08-05 18:43:09 +02:00
|
|
|
count = int(t * FPS / int64(time.Second))
|
2017-08-05 17:18:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stabilize FPS.
|
2017-08-05 18:43:09 +02:00
|
|
|
if count == 0 && (int64(time.Second)/FPS/2) < t {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
2017-08-05 18:43:09 +02:00
|
|
|
if count == 2 && (int64(time.Second)/FPS*3/2) > t {
|
2017-08-05 17:18:38 +02:00
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
if count > 3 {
|
|
|
|
count = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
frames += int64(count)
|
2018-01-07 08:43:01 +01:00
|
|
|
if syncWithSystemClock {
|
|
|
|
gameTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
} else {
|
2018-01-07 08:43:01 +01:00
|
|
|
gameTime += 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
|
|
|
}
|