mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
loop: Remove dependency from audio to loop
This commit is contained in:
parent
2e7a477f9d
commit
95a061df7f
@ -39,7 +39,6 @@ import (
|
|||||||
"github.com/hajimehoshi/oto"
|
"github.com/hajimehoshi/oto"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/clock"
|
"github.com/hajimehoshi/ebiten/internal/clock"
|
||||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type players struct {
|
type players struct {
|
||||||
@ -233,7 +232,7 @@ func (c *Context) ping() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) loop() {
|
func (c *Context) loop() {
|
||||||
loop.RegisterPing(c.ping)
|
clock.RegisterPing(c.ping)
|
||||||
|
|
||||||
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
|
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
|
||||||
// Accessing oto.Player functions requires the environment to be already initialized,
|
// Accessing oto.Player functions requires the environment to be already initialized,
|
||||||
@ -263,7 +262,7 @@ func (c *Context) loop() {
|
|||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
c.frames++
|
c.frames++
|
||||||
clock.ProceedPrimaryTimer()
|
clock.ProceedPrimaryTimer()
|
||||||
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / loop.FPS
|
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / clock.FPS
|
||||||
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes
|
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes
|
||||||
l &= mask
|
l &= mask
|
||||||
c.writtenBytes += l
|
c.writtenBytes += l
|
||||||
|
@ -20,14 +20,23 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const FPS = 60
|
||||||
|
|
||||||
var (
|
var (
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
primaryTime int64
|
primaryTime int64
|
||||||
lastPrimaryTime int64
|
lastPrimaryTime int64
|
||||||
frames int64
|
frames int64
|
||||||
logicalTime int64
|
logicalTime int64
|
||||||
|
ping func()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func RegisterPing(pingFunc func()) {
|
||||||
|
m.Lock()
|
||||||
|
ping = pingFunc
|
||||||
|
m.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// ProceedPrimaryTimer increments the primary time by a frame.
|
// ProceedPrimaryTimer increments the primary time by a frame.
|
||||||
func ProceedPrimaryTimer() {
|
func ProceedPrimaryTimer() {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
@ -35,13 +44,16 @@ func ProceedPrimaryTimer() {
|
|||||||
m.Unlock()
|
m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Frames returns an integer value indicating how many logical frames the game should update.
|
// Update updates the inner clock state and returns an integer value
|
||||||
//
|
// indicating how many logical frames the game should update.
|
||||||
// Frames also updates the inner timer states.
|
func Update(now int64) int {
|
||||||
func Frames(now int64, fps int) int {
|
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
|
if ping != nil {
|
||||||
|
ping()
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize logicalTime if needed.
|
// Initialize logicalTime if needed.
|
||||||
if logicalTime == 0 {
|
if logicalTime == 0 {
|
||||||
logicalTime = now
|
logicalTime = now
|
||||||
@ -74,20 +86,20 @@ func Frames(now int64, fps int) int {
|
|||||||
// As the primary time can be updated discountinuously,
|
// As the primary time can be updated discountinuously,
|
||||||
// the system clock is still needed.
|
// the system clock is still needed.
|
||||||
|
|
||||||
if t > 5*int64(time.Second)/int64(fps) {
|
if t > 5*int64(time.Second)/FPS {
|
||||||
// The previous time is too old.
|
// The previous time is too old.
|
||||||
// Let's force to sync the logical time with the OS clock.
|
// Let's force to sync the logical time with the OS clock.
|
||||||
sync = true
|
sync = true
|
||||||
} else {
|
} else {
|
||||||
count = int(t * int64(fps) / int64(time.Second))
|
count = int(t * FPS / int64(time.Second))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stabilize FPS.
|
// Stabilize FPS.
|
||||||
if count == 0 && (int64(time.Second)/int64(fps)/2) < t {
|
if count == 0 && (int64(time.Second)/FPS/2) < t {
|
||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
if count == 2 && (int64(time.Second)/int64(fps)*3/2) > t {
|
if count == 2 && (int64(time.Second)/FPS*3/2) > t {
|
||||||
count = 1
|
count = 1
|
||||||
}
|
}
|
||||||
if count > 3 {
|
if count > 3 {
|
||||||
@ -98,7 +110,7 @@ func Frames(now int64, fps int) int {
|
|||||||
if sync {
|
if sync {
|
||||||
logicalTime = now
|
logicalTime = now
|
||||||
} else {
|
} else {
|
||||||
logicalTime += int64(count) * int64(time.Second) / int64(fps)
|
logicalTime += int64(count) * int64(time.Second) / FPS
|
||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FPS = 60
|
const FPS = clock.FPS
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
if theRunContext == nil {
|
if theRunContext == nil {
|
||||||
@ -76,17 +76,6 @@ func End() {
|
|||||||
theRunContext = nil
|
theRunContext = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterPing(ping func()) {
|
|
||||||
<-contextInitCh
|
|
||||||
theRunContext.registerPing(ping)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *runContext) registerPing(ping func()) {
|
|
||||||
c.m.Lock()
|
|
||||||
c.ping = ping
|
|
||||||
c.m.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Updater interface {
|
type Updater interface {
|
||||||
Update(updateCount int) error
|
Update(updateCount int) error
|
||||||
}
|
}
|
||||||
@ -99,13 +88,7 @@ func Update(u Updater) error {
|
|||||||
func (c *runContext) update(u Updater) error {
|
func (c *runContext) update(u Updater) error {
|
||||||
n := now()
|
n := now()
|
||||||
|
|
||||||
c.m.Lock()
|
count := clock.Update(n)
|
||||||
if c.ping != nil {
|
|
||||||
c.ping()
|
|
||||||
}
|
|
||||||
c.m.Unlock()
|
|
||||||
|
|
||||||
count := clock.Frames(n, FPS)
|
|
||||||
if err := u.Update(count); err != nil {
|
if err := u.Update(count); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user