ui: Move Now() to internal/loop and hide it

This commit is contained in:
Hajime Hoshi 2016-05-18 11:24:17 +09:00
parent cd5436712b
commit faff188574
5 changed files with 61 additions and 18 deletions

25
internal/loop/now.go Normal file
View File

@ -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()
}

28
internal/loop/now_js.go Normal file
View File

@ -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))
}

View File

@ -187,7 +187,7 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
} }
frames := 0 frames := 0
n := ui.Now() n := now()
beforeForUpdate := n beforeForUpdate := n
beforeForFPS := n beforeForFPS := n
for { for {
@ -202,14 +202,14 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
case ui.CloseEvent: case ui.CloseEvent:
return nil return nil
case ui.RenderEvent: case ui.RenderEvent:
now := ui.Now() n2 := now()
// If beforeForUpdate is too old, we assume that screen is not shown. // 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) currentRunContext.setRunningSlowly(false)
beforeForUpdate = now beforeForUpdate = n2
} else { } else {
// Note that generally t is a little different from 1/60[sec]. // 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)) currentRunContext.setRunningSlowly(t*FPS >= int64(time.Second*5/2))
tt := int(t * FPS / int64(time.Second)) tt := int(t * FPS / int64(time.Second))
// As t is not accurate 1/60[sec], errors are accumulated. // 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. // Calc the current FPS.
if time.Second <= time.Duration(now-beforeForFPS) { if time.Second <= time.Duration(n2-beforeForFPS) {
currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(now-beforeForFPS)) currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(n2-beforeForFPS))
beforeForFPS = now beforeForFPS = n2
frames = 0 frames = 0
} }
default: default:

View File

@ -26,10 +26,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
) )
func Now() int64 {
return time.Now().UnixNano()
}
type UserInterface struct { type UserInterface struct {
window *glfw.Window window *glfw.Window
width int width int

View File

@ -18,18 +18,12 @@ package ui
import ( import (
"strconv" "strconv"
"time"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "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 { func (u *UserInterface) SetScreenSize(width, height int) bool {
return u.setScreenSize(width, height, u.scale) return u.setScreenSize(width, height, u.scale)
} }