mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Add internal/clock
This commit is contained in:
parent
fe3f0b2f1f
commit
5d1d0844e1
@ -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
|
||||
|
46
internal/clock/clock.go
Normal file
46
internal/clock/clock.go
Normal file
@ -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
|
||||
}
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user