From 5d1d0844e1edf748e0b034df4967017614ad99ba Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 14 Jul 2017 00:28:28 +0900 Subject: [PATCH] Add internal/clock --- audio/audio.go | 12 +++-------- internal/clock/clock.go | 46 +++++++++++++++++++++++++++++++++++++++++ internal/loop/run.go | 9 ++++---- 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 internal/clock/clock.go diff --git a/audio/audio.go b/audio/audio.go index 569a128f0..ecfee624f 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -35,6 +35,8 @@ import ( "time" "github.com/hajimehoshi/oto" + + "github.com/hajimehoshi/ebiten/internal/clock" ) const FPS = 60 @@ -222,14 +224,6 @@ func CurrentContext() *Context { return c } -// Internal Only? -func (c *Context) Frame() int64 { - c.m.Lock() - n := c.framesReadOnly - c.m.Unlock() - return n -} - // Internal Only? func (c *Context) Ping() { if c.initCh != nil { @@ -262,7 +256,6 @@ func (c *Context) loop() { for { c.m.Lock() - c.framesReadOnly = c.frames if c.pingCount == 0 { c.m.Unlock() time.Sleep(10 * time.Millisecond) @@ -271,6 +264,7 @@ func (c *Context) loop() { c.pingCount-- c.m.Unlock() c.frames++ + clock.Inc() bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / FPS l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes l &= mask diff --git a/internal/clock/clock.go b/internal/clock/clock.go new file mode 100644 index 000000000..eee86ed52 --- /dev/null +++ b/internal/clock/clock.go @@ -0,0 +1,46 @@ +// Copyright 2017 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package clock + +import ( + "github.com/hajimehoshi/ebiten/internal/sync" +) + +var ( + m sync.Mutex + valid bool + frame int64 +) + +func IsValid() bool { + m.Lock() + v := valid + m.Unlock() + return v +} + +func Inc() { + m.Lock() + valid = true + frame++ + m.Unlock() +} + +func Frame() int64 { + m.Lock() + n := frame + m.Unlock() + return n +} diff --git a/internal/loop/run.go b/internal/loop/run.go index 43e43518f..06f1d31ed 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -19,6 +19,7 @@ import ( "time" "github.com/hajimehoshi/ebiten/audio" + "github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/sync" "github.com/hajimehoshi/ebiten/internal/ui" ) @@ -36,7 +37,7 @@ type runContext struct { framesForFPS int64 lastUpdated int64 lastFPSUpdated int64 - lastAudioFrame int64 + lastClockFrame int64 m sync.RWMutex } @@ -132,13 +133,13 @@ func (c *runContext) updateCount(now int64) int { return 0 } - if audio.CurrentContext() != nil && c.lastAudioFrame != audio.CurrentContext().Frame() { + if clock.IsValid() && c.lastClockFrame != clock.Frame() { sync = true - f := audio.CurrentContext().Frame() + f := clock.Frame() if c.frames < f { count = int(f - c.frames) } - c.lastAudioFrame = f + c.lastClockFrame = f } else { count = int(t * int64(c.fps) / int64(time.Second)) }