ui: More precise clock

This commit is contained in:
Hajime Hoshi 2015-02-15 19:30:29 +09:00
parent c5fc7ea0ab
commit ba3feaf52f
3 changed files with 12 additions and 2 deletions

View File

@ -23,6 +23,10 @@ import (
"time" "time"
) )
func Now() int64 {
return time.Now().UnixNano()
}
var currentUI *userInterface var currentUI *userInterface
func Init() { func Init() {

View File

@ -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
View File

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