loop: Refactoring

This commit is contained in:
Hajime Hoshi 2017-08-05 22:07:03 +09:00
parent b85f5edee0
commit 19760be346

View File

@ -23,11 +23,13 @@ import (
) )
func CurrentFPS() float64 { func CurrentFPS() float64 {
if currentRunContext == nil {
return 0
}
return currentRunContext.getCurrentFPS() return currentRunContext.getCurrentFPS()
} }
type runContext struct { type runContext struct {
running bool
currentFPS float64 currentFPS float64
runningSlowly bool runningSlowly bool
frames int64 frames int64
@ -44,34 +46,11 @@ var (
contextInitCh = make(chan struct{}) contextInitCh = make(chan struct{})
) )
func (c *runContext) startRunning() {
c.m.Lock()
c.running = true
c.m.Unlock()
}
func (c *runContext) isRunning() bool {
c.m.RLock()
v := c.running
c.m.RUnlock()
return v
}
func (c *runContext) endRunning() {
c.m.Lock()
c.running = false
c.m.Unlock()
}
func (c *runContext) getCurrentFPS() float64 { func (c *runContext) getCurrentFPS() float64 {
c.m.RLock() c.m.RLock()
v := c.running v := c.currentFPS
c.m.RUnlock() c.m.RUnlock()
if !v { return v
// TODO: Should panic here?
return 0
}
return c.currentFPS
} }
func (c *runContext) updateFPS(fps float64) { func (c *runContext) updateFPS(fps float64) {
@ -81,11 +60,11 @@ func (c *runContext) updateFPS(fps float64) {
} }
func Start() error { func Start() error {
// TODO: Need lock here?
if currentRunContext != nil { if currentRunContext != nil {
return errors.New("loop: The game is already running") return errors.New("loop: The game is already running")
} }
currentRunContext = &runContext{} currentRunContext = &runContext{}
currentRunContext.startRunning()
n := now() n := now()
currentRunContext.lastUpdated = n currentRunContext.lastUpdated = n
@ -96,7 +75,6 @@ func Start() error {
} }
func End() { func End() {
currentRunContext.endRunning()
currentRunContext = nil currentRunContext = nil
} }