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.
|
|
|
|
|
|
|
|
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 (
|
2017-08-05 17:52:12 +02:00
|
|
|
primaryTime int64
|
|
|
|
lastPrimaryTime int64
|
|
|
|
frames int64
|
|
|
|
logicalTime 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()
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:52:12 +02:00
|
|
|
// ProceedPrimaryTimer increments the primary time by a frame.
|
|
|
|
func ProceedPrimaryTimer() {
|
2017-07-13 17:28:28 +02:00
|
|
|
m.Lock()
|
2017-08-05 17:52:12 +02:00
|
|
|
primaryTime++
|
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
|
|
|
|
// indicating how many logical 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()
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:52:12 +02:00
|
|
|
// Initialize logicalTime if needed.
|
|
|
|
if logicalTime == 0 {
|
2017-08-05 19:12:23 +02:00
|
|
|
logicalTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 19:12:23 +02:00
|
|
|
t := n - logicalTime
|
2017-08-05 17:52:12 +02:00
|
|
|
if t < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-08-05 17:18:38 +02:00
|
|
|
count := 0
|
|
|
|
|
2017-08-23 19:30:43 +02:00
|
|
|
// Logical clock:
|
|
|
|
// A clock that updated based on the number of frames.
|
2017-08-05 17:52:12 +02:00
|
|
|
//
|
2017-08-23 19:30:43 +02:00
|
|
|
// System clock:
|
|
|
|
// A clock that offered by the OS.
|
|
|
|
//
|
|
|
|
// Primary clock:
|
|
|
|
// A clock that is used in the higher priority over the system clock.
|
|
|
|
// Primary time is usually an audio time.
|
|
|
|
// Primary time might not exist when e.g. audio is not used.
|
|
|
|
|
|
|
|
// When sync is true, the logical time is forced to sync with the system clock.
|
2017-08-05 17:18:38 +02:00
|
|
|
sync := false
|
2017-08-05 17:52:12 +02:00
|
|
|
|
|
|
|
if primaryTime > 0 && lastPrimaryTime != primaryTime {
|
2017-08-23 19:30:43 +02:00
|
|
|
// If the primary clock is updated, use this.
|
2017-08-05 17:52:12 +02:00
|
|
|
if frames < primaryTime {
|
|
|
|
count = int(primaryTime - frames)
|
2017-08-05 17:18:38 +02:00
|
|
|
}
|
2017-08-05 17:52:12 +02:00
|
|
|
lastPrimaryTime = primaryTime
|
2017-08-05 17:18:38 +02:00
|
|
|
sync = true
|
|
|
|
} else {
|
|
|
|
// Use system clock when
|
|
|
|
// 1) Inc() is not called, or
|
2017-08-23 19:30:43 +02:00
|
|
|
// 2) the primary clock is not updated yet.
|
|
|
|
// As the primary 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.
|
2017-08-05 17:52:12 +02:00
|
|
|
// Let's force to sync the logical time with the OS clock.
|
|
|
|
sync = true
|
|
|
|
} 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)
|
2017-08-05 17:52:12 +02:00
|
|
|
if sync {
|
2017-08-05 19:12:23 +02:00
|
|
|
logicalTime = n
|
2017-08-05 17:52:12 +02:00
|
|
|
} else {
|
2017-08-05 18:43:09 +02:00
|
|
|
logicalTime += 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
|
|
|
}
|