loop: Refactoring

This commit is contained in:
Hajime Hoshi 2016-06-14 00:49:43 +09:00
parent 6640f60c88
commit 218df19ead

View File

@ -23,7 +23,7 @@ import (
)
func CurrentFPS() float64 {
return currentRunContext.currentFPS()
return currentRunContext.getCurrentFPS()
}
func IsRunning() bool {
@ -36,8 +36,12 @@ func IsRunningSlowly() bool {
type runContext struct {
running bool
fps float64
fps int
currentFPS float64
runningSlowly bool
frames int
beforeForUpdate int64
beforeForFPS int64
m sync.RWMutex
}
@ -61,20 +65,20 @@ func (c *runContext) endRunning() {
c.running = false
}
func (c *runContext) currentFPS() float64 {
func (c *runContext) getCurrentFPS() float64 {
c.m.RLock()
defer c.m.RUnlock()
if !c.running {
// TODO: Should panic here?
return 0
}
return c.fps
return c.currentFPS
}
func (c *runContext) updateFPS(fps float64) {
c.m.Lock()
defer c.m.Unlock()
c.fps = fps
c.currentFPS = fps
}
func (c *runContext) isRunningSlowly() bool {
@ -103,7 +107,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
currentRunContext = &runContext{}
currentRunContext = &runContext{
fps: fps,
}
currentRunContext.startRunning()
defer currentRunContext.endRunning()
@ -113,10 +119,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
// TODO: Use the error value
defer ui.CurrentUI().Terminate()
frames := 0
n := now()
beforeForUpdate := n
beforeForFPS := n
currentRunContext.beforeForUpdate = n
currentRunContext.beforeForFPS = n
for {
e, err := ui.CurrentUI().Update()
if err != nil {
@ -130,14 +135,26 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
case ui.CloseEvent:
return nil
case ui.RenderEvent:
n2 := now()
if err := currentRunContext.render(g); err != nil {
return err
}
e.Done <- struct{}{}
default:
panic("not reach")
}
}
}
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) < n2-beforeForUpdate {
currentRunContext.setRunningSlowly(false)
beforeForUpdate = n2
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 := n2 - beforeForUpdate
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].
@ -147,7 +164,7 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
if 1 <= tt {
for i := 0; i < tt; i++ {
slow := i < tt-1
currentRunContext.setRunningSlowly(slow)
c.setRunningSlowly(slow)
if err := g.UpdateAndDraw(); err != nil {
return err
}
@ -160,19 +177,15 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
if err := ui.CurrentUI().SwapBuffers(); err != nil {
return err
}
beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
frames++
c.beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
c.frames++
}
// Calc the current FPS.
if time.Second <= time.Duration(n2-beforeForFPS) {
fps := float64(frames) * float64(time.Second) / float64(n2-beforeForFPS)
currentRunContext.updateFPS(fps)
beforeForFPS = n2
frames = 0
}
e.Done <- struct{}{}
default:
panic("not reach")
}
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
}
return nil
}