ebiten/internal/clock/clock.go

121 lines
2.4 KiB
Go
Raw Normal View History

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 (
"sync"
"time"
2017-07-13 17:28:28 +02:00
)
var (
frames int64
2018-01-07 08:43:01 +01:00
// lastSystemTime is the last system time in the previous Update.
lastSystemTime int64
currentFPS float64
lastFPSUpdated int64
framesForFPS int64
started bool
onStart func()
m sync.Mutex
2017-07-13 17:28:28 +02:00
)
func CurrentFPS() float64 {
m.Lock()
v := currentFPS
m.Unlock()
return v
}
func OnStart(f func()) {
m.Lock()
onStart = f
m.Unlock()
}
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
}
// 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.
2018-07-15 15:43:14 +02:00
func Update(logicFPS int) int {
2017-07-13 17:28:28 +02:00
m.Lock()
defer m.Unlock()
if !started {
if onStart != nil {
onStart()
}
started = true
}
n := now()
// 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
}
count := 0
2018-01-07 08:43:01 +01:00
syncWithSystemClock := false
if diff > int64(time.Second)*5/60 {
// 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
} else {
2018-07-15 15:43:14 +02:00
count = int(diff * int64(logicFPS) / int64(time.Second))
}
// Stabilize FPS.
2018-01-07 12:32:18 +01:00
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
2018-07-15 15:43:14 +02:00
if count == 0 && (int64(time.Second)/int64(logicFPS)/2) < diff {
count = 1
}
2018-07-15 15:43:14 +02:00
if count == 2 && (int64(time.Second)/int64(logicFPS)*3/2) > diff {
count = 1
}
frames += int64(count)
2018-01-07 08:43:01 +01:00
if syncWithSystemClock {
lastSystemTime = n
2017-08-05 17:52:12 +02:00
} else {
2018-07-15 15:43:14 +02:00
lastSystemTime += int64(count) * int64(time.Second) / int64(logicFPS)
2017-08-05 17:52:12 +02:00
}
2017-08-05 19:12:23 +02:00
updateFPS(n)
2017-08-05 17:52:12 +02:00
return count
2017-07-13 17:28:28 +02:00
}