loop: Refactoring

This commit is contained in:
Hajime Hoshi 2016-06-14 01:33:35 +09:00
parent b852f5a0f7
commit ceea24ab95

View File

@ -35,14 +35,14 @@ func IsRunningSlowly() bool {
} }
type runContext struct { type runContext struct {
running bool running bool
fps int fps int
currentFPS float64 currentFPS float64
runningSlowly bool runningSlowly bool
frames int frames int
beforeForUpdate int64 lastUpdated int64
beforeForFPS int64 lastFPSUpdated int64
m sync.RWMutex m sync.RWMutex
} }
var currentRunContext *runContext var currentRunContext *runContext
@ -120,8 +120,8 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
defer ui.CurrentUI().Terminate() defer ui.CurrentUI().Terminate()
n := now() n := now()
currentRunContext.beforeForUpdate = n currentRunContext.lastUpdated = n
currentRunContext.beforeForFPS = n currentRunContext.lastFPSUpdated = n
for { for {
e, err := ui.CurrentUI().Update() e, err := ui.CurrentUI().Update()
if err != nil { 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 { func (c *runContext) render(g GraphicsContext) error {
fps := c.fps fps := c.fps
n := now() n := now()
// If beforeForUpdate is too old, we assume that screen is not shown. defer func() {
if 5*int64(time.Second)/int64(fps) < n-c.beforeForUpdate { // Calc the current FPS.
c.setRunningSlowly(false) if time.Second > time.Duration(n-c.lastFPSUpdated) {
c.beforeForUpdate = n return
} 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
} }
if 1 <= tt { currentFPS := float64(c.frames) * float64(time.Second) / float64(n-c.lastFPSUpdated)
for i := 0; i < tt; i++ { c.updateFPS(currentFPS)
slow := i < tt-1 c.lastFPSUpdated = n
c.setRunningSlowly(slow) c.frames = 0
if err := g.UpdateAndDraw(); err != nil { }()
return err
} // If lastUpdated is too old, we assume that screen is not shown.
} if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated {
} else { c.setRunningSlowly(false)
if err := g.Draw(); err != nil { 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 return err
} }
} }
if err := ui.CurrentUI().SwapBuffers(); err != nil { } else {
if err := g.Draw(); err != nil {
return err return err
} }
c.beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
c.frames++
} }
// Calc the current FPS. if err := ui.CurrentUI().SwapBuffers(); err != nil {
if time.Second <= time.Duration(n-c.beforeForFPS) { return err
fps := float64(c.frames) * float64(time.Second) / float64(n-c.beforeForFPS)
c.updateFPS(fps)
c.beforeForFPS = n
c.frames = 0
} }
c.lastUpdated += int64(tt) * int64(time.Second) / int64(fps)
c.frames++
return nil return nil
} }