From e6088379434395f6978a99400a0b2cad31fbfcce Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 9 Apr 2016 18:56:07 +0900 Subject: [PATCH] ui: Make FPS more stable (#203) --- run.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/run.go b/run.go index 53929cdc9..80511832d 100644 --- a/run.go +++ b/run.go @@ -199,9 +199,16 @@ func Run(f func(*Image) error, width, height, scale int, title string) error { currentRunContext.setRunningSlowly(false) beforeForUpdate = now } else { + // Note that generally t is a little more than 1/60[sec]. t := now - 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 + // (generally, errors are positive values). + // To make the FPS stable, set tt 1 if t is a little less than 1/60[sec]. + if tt == 0 && (int64(time.Second)/FPS-int64(10*time.Millisecond)) < t { + tt = 1 + } for i := 0; i < tt; i++ { if err := ui.CurrentUI().DoEvents(); err != nil { return err @@ -214,7 +221,8 @@ func Run(f func(*Image) error, width, height, scale int, title string) error { } } ui.CurrentUI().SwapBuffers() - beforeForUpdate += int64(tt) * int64(time.Second) / FPS + d := int64(tt) * int64(time.Second) / FPS + beforeForUpdate += d frames++ }