clock: Rename function names; loop: Rename variables

This commit is contained in:
Hajime Hoshi 2017-08-05 22:25:21 +09:00
parent 94843fbe73
commit a92f77c207
3 changed files with 19 additions and 19 deletions

View File

@ -262,7 +262,7 @@ func (c *Context) loop() {
c.pingCount-- c.pingCount--
c.m.Unlock() c.m.Unlock()
c.frames++ c.frames++
clock.Inc() clock.Tick()
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / loop.FPS bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / loop.FPS
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes
l &= mask l &= mask

View File

@ -21,7 +21,7 @@ import (
var ( var (
m sync.Mutex m sync.Mutex
valid bool valid bool
frame int64 now int64
) )
func IsValid() bool { func IsValid() bool {
@ -31,16 +31,16 @@ func IsValid() bool {
return v return v
} }
func Inc() { func Tick() {
m.Lock() m.Lock()
valid = true valid = true
frame++ now++
m.Unlock() m.Unlock()
} }
func Frame() int64 { func Now() int64 {
m.Lock() m.Lock()
n := frame n := now
m.Unlock() m.Unlock()
return n return n
} }

View File

@ -25,10 +25,10 @@ import (
const FPS = 60 const FPS = 60
func CurrentFPS() float64 { func CurrentFPS() float64 {
if currentRunContext == nil { if theRunContext == nil {
return 0 return 0
} }
return currentRunContext.getCurrentFPS() return theRunContext.getCurrentFPS()
} }
type runContext struct { type runContext struct {
@ -44,7 +44,7 @@ type runContext struct {
} }
var ( var (
currentRunContext *runContext theRunContext *runContext
contextInitCh = make(chan struct{}) contextInitCh = make(chan struct{})
) )
@ -63,21 +63,21 @@ func (c *runContext) updateFPS(fps float64) {
func Start() error { func Start() error {
// TODO: Need lock here? // TODO: Need lock here?
if currentRunContext != nil { if theRunContext != nil {
return errors.New("loop: The game is already running") return errors.New("loop: The game is already running")
} }
currentRunContext = &runContext{} theRunContext = &runContext{}
n := now() n := now()
currentRunContext.lastUpdated = n theRunContext.lastUpdated = n
currentRunContext.lastFPSUpdated = n theRunContext.lastFPSUpdated = n
close(contextInitCh) close(contextInitCh)
return nil return nil
} }
func End() { func End() {
currentRunContext = nil theRunContext = nil
} }
func (c *runContext) updateCount(now int64) int { func (c *runContext) updateCount(now int64) int {
@ -89,9 +89,9 @@ func (c *runContext) updateCount(now int64) int {
return 0 return 0
} }
if clock.IsValid() && c.lastClockFrame != clock.Frame() { if clock.IsValid() && c.lastClockFrame != clock.Now() {
sync = true sync = true
f := clock.Frame() f := clock.Now()
if c.frames < f { if c.frames < f {
count = int(f - c.frames) count = int(f - c.frames)
} }
@ -130,7 +130,7 @@ func (c *runContext) updateCount(now int64) int {
func RegisterPing(ping func()) { func RegisterPing(ping func()) {
<-contextInitCh <-contextInitCh
currentRunContext.registerPing(ping) theRunContext.registerPing(ping)
} }
func (c *runContext) registerPing(ping func()) { func (c *runContext) registerPing(ping func()) {
@ -145,7 +145,7 @@ type Updater interface {
func Update(u Updater) error { func Update(u Updater) error {
<-contextInitCh <-contextInitCh
return currentRunContext.update(u) return theRunContext.update(u)
} }
func (c *runContext) update(u Updater) error { func (c *runContext) update(u Updater) error {