mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
audio: At most one audio context can exist (#271)
This commit is contained in:
parent
2eac8dca8c
commit
61676616a7
@ -186,12 +186,22 @@ type Context struct {
|
|||||||
writtenBytes int
|
writtenBytes int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
theContext *Context
|
||||||
|
theContextLock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
// NewContext creates a new audio context with the given sample rate (e.g. 44100).
|
// NewContext creates a new audio context with the given sample rate (e.g. 44100).
|
||||||
func NewContext(sampleRate int) (*Context, error) {
|
func NewContext(sampleRate int) (*Context, error) {
|
||||||
// TODO: Panic if one context exists.
|
theContextLock.Lock()
|
||||||
|
defer theContextLock.Unlock()
|
||||||
|
if theContext != nil {
|
||||||
|
return nil, errors.New("audio: context is already created")
|
||||||
|
}
|
||||||
c := &Context{
|
c := &Context{
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
}
|
}
|
||||||
|
theContext = c
|
||||||
c.players = &players{
|
c.players = &players{
|
||||||
players: map[*Player]struct{}{},
|
players: map[*Player]struct{}{},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user