clock: Move now() from loop to clock

This commit is contained in:
Hajime Hoshi 2017-08-06 02:12:23 +09:00
parent f0f115b612
commit d1cbfdeec7
4 changed files with 10 additions and 10 deletions

View File

@ -72,20 +72,22 @@ 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 logical frames the game should update. // indicating how many logical frames the game should update.
func Update(now int64) int { func Update() int {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
n := now()
if ping != nil { if ping != nil {
ping() ping()
} }
// Initialize logicalTime if needed. // Initialize logicalTime if needed.
if logicalTime == 0 { if logicalTime == 0 {
logicalTime = now logicalTime = n
} }
t := now - logicalTime t := n - logicalTime
if t < 0 { if t < 0 {
return 0 return 0
} }
@ -134,12 +136,12 @@ func Update(now int64) int {
frames += int64(count) frames += int64(count)
if sync { if sync {
logicalTime = now logicalTime = n
} else { } else {
logicalTime += int64(count) * int64(time.Second) / FPS logicalTime += int64(count) * int64(time.Second) / FPS
} }
updateFPS(now) updateFPS(n)
return count return count
} }

View File

@ -14,7 +14,7 @@
// +build js // +build js
package loop package clock
import ( import (
"time" "time"

View File

@ -14,7 +14,7 @@
// +build !js // +build !js
package loop package clock
import ( import (
"time" "time"

View File

@ -57,9 +57,7 @@ func Update(u Updater) error {
} }
func (c *runContext) update(u Updater) error { func (c *runContext) update(u Updater) error {
n := now() count := clock.Update()
count := clock.Update(n)
if err := u.Update(count); err != nil { if err := u.Update(count); err != nil {
return err return err
} }