internal/clock: Update takes FPS

This commit is contained in:
Hajime Hoshi 2018-07-15 22:43:14 +09:00
parent a0c62e90fc
commit 18ec1d8265
3 changed files with 8 additions and 10 deletions

View File

@ -82,7 +82,7 @@ func (c *graphicsContext) initializeIfNeeded() error {
} }
func (c *graphicsContext) Update(afterFrameUpdate func()) error { func (c *graphicsContext) Update(afterFrameUpdate func()) error {
updateCount := clock.Update() updateCount := clock.Update(FPS)
if err := c.initializeIfNeeded(); err != nil { if err := c.initializeIfNeeded(); err != nil {
return err return err

View File

@ -20,8 +20,6 @@ import (
"time" "time"
) )
const FPS = 60
var ( var (
frames int64 frames int64
@ -66,7 +64,7 @@ func updateFPS(now int64) {
// Update updates the inner clock state and returns an integer value // Update updates the inner clock state and returns an integer value
// indicating how many game frames the game should update. // indicating how many game frames the game should update.
func Update() int { func Update(logicFPS int) int {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
@ -92,20 +90,20 @@ func Update() int {
count := 0 count := 0
syncWithSystemClock := false syncWithSystemClock := false
if diff > 5*int64(time.Second)/FPS { if diff > 5*int64(time.Second)/int64(logicFPS) {
// The previous time is too old. // The previous time is too old.
// Let's force to sync the game time with the system clock. // Let's force to sync the game time with the system clock.
syncWithSystemClock = true syncWithSystemClock = true
} else { } else {
count = int(diff * FPS / int64(time.Second)) count = int(diff * int64(logicFPS) / int64(time.Second))
} }
// Stabilize FPS. // Stabilize FPS.
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ... // Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
if count == 0 && (int64(time.Second)/FPS/2) < diff { if count == 0 && (int64(time.Second)/int64(logicFPS)/2) < diff {
count = 1 count = 1
} }
if count == 2 && (int64(time.Second)/FPS*3/2) > diff { if count == 2 && (int64(time.Second)/int64(logicFPS)*3/2) > diff {
count = 1 count = 1
} }
@ -113,7 +111,7 @@ func Update() int {
if syncWithSystemClock { if syncWithSystemClock {
lastSystemTime = n lastSystemTime = n
} else { } else {
lastSystemTime += int64(count) * int64(time.Second) / FPS lastSystemTime += int64(count) * int64(time.Second) / int64(logicFPS)
} }
updateFPS(n) updateFPS(n)

2
run.go
View File

@ -30,7 +30,7 @@ import (
) )
// FPS represents how many times game updating happens in a second (60). // FPS represents how many times game updating happens in a second (60).
const FPS = clock.FPS const FPS = 60
// CurrentFPS returns the current number of frames per second of rendering. // CurrentFPS returns the current number of frames per second of rendering.
// //