From ceea24ab95c86e3e6062bc91594213ffd16ca0a0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 14 Jun 2016 01:33:35 +0900 Subject: [PATCH] loop: Refactoring --- internal/loop/run.go | 88 +++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/internal/loop/run.go b/internal/loop/run.go index ff89376ff..f9d5bff99 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -35,14 +35,14 @@ func IsRunningSlowly() bool { } type runContext struct { - running bool - fps int - currentFPS float64 - runningSlowly bool - frames int - beforeForUpdate int64 - beforeForFPS int64 - m sync.RWMutex + running bool + fps int + currentFPS float64 + runningSlowly bool + frames int + lastUpdated int64 + lastFPSUpdated int64 + m sync.RWMutex } var currentRunContext *runContext @@ -120,8 +120,8 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err defer ui.CurrentUI().Terminate() n := now() - currentRunContext.beforeForUpdate = n - currentRunContext.beforeForFPS = n + currentRunContext.lastUpdated = n + currentRunContext.lastFPSUpdated = n for { e, err := ui.CurrentUI().Update() if err != nil { @@ -148,44 +148,50 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err func (c *runContext) render(g GraphicsContext) error { fps := c.fps n := now() - // If beforeForUpdate is too old, we assume that screen is not shown. - if 5*int64(time.Second)/int64(fps) < n-c.beforeForUpdate { - c.setRunningSlowly(false) - c.beforeForUpdate = n - } else { - // Note that generally t is a little different from 1/60[sec]. - t := n - c.beforeForUpdate - tt := int(t * int64(fps) / int64(time.Second)) - // As t is not accurate 1/60[sec], errors are accumulated. - // To make the FPS stable, set tt 1 if t is a little less than 1/60[sec]. - if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t { - tt = 1 + defer func() { + // Calc the current FPS. + if time.Second > time.Duration(n-c.lastFPSUpdated) { + return } - if 1 <= tt { - for i := 0; i < tt; i++ { - slow := i < tt-1 - c.setRunningSlowly(slow) - if err := g.UpdateAndDraw(); err != nil { - return err - } - } - } else { - if err := g.Draw(); err != nil { + currentFPS := float64(c.frames) * float64(time.Second) / float64(n-c.lastFPSUpdated) + c.updateFPS(currentFPS) + c.lastFPSUpdated = n + c.frames = 0 + }() + + // If lastUpdated is too old, we assume that screen is not shown. + if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated { + c.setRunningSlowly(false) + c.lastUpdated = n + return nil + } + + // Note that generally t is a little different from 1/60[sec]. + t := n - c.lastUpdated + tt := int(t * int64(fps) / int64(time.Second)) + + // As t is not accurate 1/60[sec], errors are accumulated. + // To make the FPS stable, set tt 1 if t is a little less than 1/60[sec]. + if tt == 0 && (int64(time.Second)/int64(fps)-int64(5*time.Millisecond)) < t { + tt = 1 + } + if 1 <= tt { + for i := 0; i < tt; i++ { + slow := i < tt-1 + c.setRunningSlowly(slow) + if err := g.UpdateAndDraw(); err != nil { return err } } - if err := ui.CurrentUI().SwapBuffers(); err != nil { + } else { + if err := g.Draw(); err != nil { return err } - c.beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps) - c.frames++ } - // Calc the current FPS. - if time.Second <= time.Duration(n-c.beforeForFPS) { - fps := float64(c.frames) * float64(time.Second) / float64(n-c.beforeForFPS) - c.updateFPS(fps) - c.beforeForFPS = n - c.frames = 0 + if err := ui.CurrentUI().SwapBuffers(); err != nil { + return err } + c.lastUpdated += int64(tt) * int64(time.Second) / int64(fps) + c.frames++ return nil }