mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
ui: More precise clock
This commit is contained in:
parent
c5fc7ea0ab
commit
ba3feaf52f
@ -23,6 +23,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Now() int64 {
|
||||||
|
return time.Now().UnixNano()
|
||||||
|
}
|
||||||
|
|
||||||
var currentUI *userInterface
|
var currentUI *userInterface
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
|
@ -19,8 +19,14 @@ package ui
|
|||||||
import (
|
import (
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Now() int64 {
|
||||||
|
// time.Now() is not reliable until GopherJS suports performance.now().
|
||||||
|
return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond))
|
||||||
|
}
|
||||||
|
|
||||||
func ExecOnUIThread(f func()) {
|
func ExecOnUIThread(f func()) {
|
||||||
f()
|
f()
|
||||||
}
|
}
|
||||||
|
4
run.go
4
run.go
@ -60,7 +60,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
frames := 0
|
frames := 0
|
||||||
t := time.Now().UnixNano()
|
t := ui.Now()
|
||||||
for {
|
for {
|
||||||
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale {
|
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale {
|
||||||
changed := false
|
changed := false
|
||||||
@ -110,7 +110,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calc the current FPS.
|
// Calc the current FPS.
|
||||||
now := time.Now().UnixNano()
|
now := ui.Now()
|
||||||
frames++
|
frames++
|
||||||
if time.Second <= time.Duration(now-t) {
|
if time.Second <= time.Duration(now-t) {
|
||||||
runContext.fps = float64(frames) * float64(time.Second) / float64(now-t)
|
runContext.fps = float64(frames) * float64(time.Second) / float64(now-t)
|
||||||
|
Loading…
Reference in New Issue
Block a user