From 18ec1d82655ce529e234b0e75eec3bb71e234b4c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 15 Jul 2018 22:43:14 +0900 Subject: [PATCH] internal/clock: Update takes FPS --- graphicscontext.go | 2 +- internal/clock/clock.go | 14 ++++++-------- run.go | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index fe5b5d26d..2cd5e378e 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -82,7 +82,7 @@ func (c *graphicsContext) initializeIfNeeded() error { } func (c *graphicsContext) Update(afterFrameUpdate func()) error { - updateCount := clock.Update() + updateCount := clock.Update(FPS) if err := c.initializeIfNeeded(); err != nil { return err diff --git a/internal/clock/clock.go b/internal/clock/clock.go index 5f98c5f4c..52f9592c1 100644 --- a/internal/clock/clock.go +++ b/internal/clock/clock.go @@ -20,8 +20,6 @@ import ( "time" ) -const FPS = 60 - var ( frames int64 @@ -66,7 +64,7 @@ func updateFPS(now int64) { // Update updates the inner clock state and returns an integer value // indicating how many game frames the game should update. -func Update() int { +func Update(logicFPS int) int { m.Lock() defer m.Unlock() @@ -92,20 +90,20 @@ func Update() int { count := 0 syncWithSystemClock := false - if diff > 5*int64(time.Second)/FPS { + if diff > 5*int64(time.Second)/int64(logicFPS) { // The previous time is too old. // Let's force to sync the game time with the system clock. syncWithSystemClock = true } else { - count = int(diff * FPS / int64(time.Second)) + count = int(diff * int64(logicFPS) / int64(time.Second)) } // Stabilize FPS. // 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 } - if count == 2 && (int64(time.Second)/FPS*3/2) > diff { + if count == 2 && (int64(time.Second)/int64(logicFPS)*3/2) > diff { count = 1 } @@ -113,7 +111,7 @@ func Update() int { if syncWithSystemClock { lastSystemTime = n } else { - lastSystemTime += int64(count) * int64(time.Second) / FPS + lastSystemTime += int64(count) * int64(time.Second) / int64(logicFPS) } updateFPS(n) diff --git a/run.go b/run.go index 2138227f9..d5b704546 100644 --- a/run.go +++ b/run.go @@ -30,7 +30,7 @@ import ( ) // 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. //