mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
ui: Move Now() to internal/loop and hide it
This commit is contained in:
parent
cd5436712b
commit
faff188574
25
internal/loop/now.go
Normal file
25
internal/loop/now.go
Normal 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
28
internal/loop/now_js.go
Normal 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))
|
||||
}
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user