From faff188574aca826b21acdf34cf6b4c88e0993ce Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 18 May 2016 11:24:17 +0900 Subject: [PATCH] ui: Move Now() to internal/loop and hide it --- internal/loop/now.go | 25 +++++++++++++++++++++++++ internal/loop/now_js.go | 28 ++++++++++++++++++++++++++++ internal/loop/run.go | 16 ++++++++-------- internal/ui/ui_glfw.go | 4 ---- internal/ui/ui_js.go | 6 ------ 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 internal/loop/now.go create mode 100644 internal/loop/now_js.go diff --git a/internal/loop/now.go b/internal/loop/now.go new file mode 100644 index 000000000..3a200fd9e --- /dev/null +++ b/internal/loop/now.go @@ -0,0 +1,25 @@ +// Copyright 2016 Hajime Hoshi +// +// 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. + +// +build !js + +package loop + +import ( + "time" +) + +func now() int64 { + return time.Now().UnixNano() +} diff --git a/internal/loop/now_js.go b/internal/loop/now_js.go new file mode 100644 index 000000000..a117dc768 --- /dev/null +++ b/internal/loop/now_js.go @@ -0,0 +1,28 @@ +// Copyright 2016 Hajime Hoshi +// +// 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. + +// +build js + +package loop + +import ( + "time" + + "github.com/gopherjs/gopherjs/js" +) + +func now() int64 { + // time.Now() is not reliable until GopherJS supports performance.now(). + return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) +} diff --git a/internal/loop/run.go b/internal/loop/run.go index 7f8b07101..ac49717f4 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -187,7 +187,7 @@ func Run(g GraphicsContext, width, height, scale int, title string) error { } frames := 0 - n := ui.Now() + n := now() beforeForUpdate := n beforeForFPS := n for { @@ -202,14 +202,14 @@ func Run(g GraphicsContext, width, height, scale int, title string) error { case ui.CloseEvent: return nil case ui.RenderEvent: - now := ui.Now() + n2 := now() // If beforeForUpdate is too old, we assume that screen is not shown. - if int64(5*time.Second/FPS) < now-beforeForUpdate { + if int64(5*time.Second/FPS) < n2-beforeForUpdate { currentRunContext.setRunningSlowly(false) - beforeForUpdate = now + beforeForUpdate = n2 } else { // Note that generally t is a little different from 1/60[sec]. - t := now - beforeForUpdate + t := n2 - beforeForUpdate currentRunContext.setRunningSlowly(t*FPS >= int64(time.Second*5/2)) tt := int(t * FPS / int64(time.Second)) // As t is not accurate 1/60[sec], errors are accumulated. @@ -228,9 +228,9 @@ func Run(g GraphicsContext, width, height, scale int, title string) error { } // Calc the current FPS. - if time.Second <= time.Duration(now-beforeForFPS) { - currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(now-beforeForFPS)) - beforeForFPS = now + if time.Second <= time.Duration(n2-beforeForFPS) { + currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(n2-beforeForFPS)) + beforeForFPS = n2 frames = 0 } default: diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 12dd479a7..5aa2b7d64 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -26,10 +26,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -func Now() int64 { - return time.Now().UnixNano() -} - type UserInterface struct { window *glfw.Window width int diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 8baa00bf0..96892ce2a 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -18,18 +18,12 @@ package ui import ( "strconv" - "time" "github.com/gopherjs/gopherjs/js" "github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) -func Now() int64 { - // time.Now() is not reliable until GopherJS supports performance.now(). - return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) -} - func (u *UserInterface) SetScreenSize(width, height int) bool { return u.setScreenSize(width, height, u.scale) }