mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-04 15:04:28 +01:00
audio: Use a boolean value to inidicate inited state intead of channels
This is a preparation to use oto.Players for Players. By using a boolean value, suspending state can be detected from multiple oto.Players.
This commit is contained in:
parent
fe55e55b7e
commit
14b6e95217
@ -49,12 +49,12 @@ import (
|
||||
//
|
||||
// For a typical usage example, see examples/wav/main.go.
|
||||
type Context struct {
|
||||
c context
|
||||
initCh chan struct{}
|
||||
c context
|
||||
|
||||
mux *mux
|
||||
sampleRate int
|
||||
err error
|
||||
inited bool
|
||||
suspended bool
|
||||
ready bool
|
||||
|
||||
@ -88,7 +88,6 @@ func NewContext(sampleRate int) (*Context, error) {
|
||||
c := &Context{
|
||||
sampleRate: sampleRate,
|
||||
c: newContext(sampleRate),
|
||||
initCh: make(chan struct{}),
|
||||
}
|
||||
theContext = c
|
||||
c.mux = newMux()
|
||||
@ -105,11 +104,10 @@ func NewContext(sampleRate int) (*Context, error) {
|
||||
c.m.Unlock()
|
||||
})
|
||||
|
||||
var once sync.Once
|
||||
h.AppendHookOnBeforeUpdate(func() error {
|
||||
once.Do(func() {
|
||||
close(c.initCh)
|
||||
})
|
||||
c.m.Lock()
|
||||
c.inited = true
|
||||
c.m.Unlock()
|
||||
|
||||
var err error
|
||||
theContextLock.Lock()
|
||||
@ -135,19 +133,22 @@ func CurrentContext() *Context {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Context) loop() {
|
||||
<-c.initCh
|
||||
func (c *Context) playable() bool {
|
||||
c.m.Lock()
|
||||
i := c.inited
|
||||
s := c.suspended
|
||||
c.m.Unlock()
|
||||
return i && !s
|
||||
}
|
||||
|
||||
func (c *Context) loop() {
|
||||
defer c.c.Close()
|
||||
|
||||
p := c.c.NewPlayer()
|
||||
defer p.Close()
|
||||
|
||||
for {
|
||||
c.m.Lock()
|
||||
s := c.suspended
|
||||
c.m.Unlock()
|
||||
if s {
|
||||
if !c.playable() {
|
||||
runtime.Gosched()
|
||||
continue
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user