Add internal/hooks to replace internal/audiobinding

This commit is contained in:
Hajime Hoshi 2018-02-04 17:33:17 +09:00
parent e3023889ac
commit 392ee92aeb
3 changed files with 33 additions and 17 deletions

View File

@ -44,8 +44,8 @@ import (
"github.com/hajimehoshi/oto" "github.com/hajimehoshi/oto"
"github.com/hajimehoshi/ebiten/internal/audiobinding"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/sync" "github.com/hajimehoshi/ebiten/internal/sync"
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
) )
@ -155,6 +155,8 @@ type Context struct {
initedCh chan struct{} initedCh chan struct{}
pingCount int pingCount int
sampleRate int sampleRate int
err error
m sync.Mutex m sync.Mutex
} }
@ -163,6 +165,15 @@ var (
theContextLock sync.Mutex theContextLock sync.Mutex
) )
func init() {
hooks.AppendHookOnUpdate(func() error {
theContext.m.Lock()
err := theContext.err
theContext.m.Unlock()
return err
})
}
// NewContext creates a new audio context with the given sample rate. // NewContext creates a new audio context with the given sample rate.
// //
// The sample rate is also used for decoding MP3 with audio/mp3 package // The sample rate is also used for decoding MP3 with audio/mp3 package
@ -234,7 +245,7 @@ func (c *Context) loop() {
// but there are some known environment that is too short (e.g. Windows on Parallels, iOS). // but there are some known environment that is too short (e.g. Windows on Parallels, iOS).
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, 8192) p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, 8192)
if err != nil { if err != nil {
audiobinding.SetError(err) c.err = err
return return
} }
defer p.Close() defer p.Close()
@ -256,7 +267,7 @@ func (c *Context) loop() {
const n = 4096 const n = 4096
if _, err := io.CopyN(p, c.players, n); err != nil { if _, err := io.CopyN(p, c.players, n); err != nil {
audiobinding.SetError(err) c.err = err
return return
} }

View File

@ -17,8 +17,8 @@ package ebiten
import ( import (
"math" "math"
"github.com/hajimehoshi/ebiten/internal/audiobinding"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/restorable" "github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
@ -98,11 +98,6 @@ func drawWithFittingScale(dst *Image, src *Image) {
} }
func (c *graphicsContext) Update(afterFrameUpdate func()) error { func (c *graphicsContext) Update(afterFrameUpdate func()) error {
select {
case err := <-audiobinding.Error():
return err
default:
}
updateCount := clock.Update() updateCount := clock.Update()
if err := c.initializeIfNeeded(); err != nil { if err := c.initializeIfNeeded(); err != nil {
@ -111,6 +106,9 @@ func (c *graphicsContext) Update(afterFrameUpdate func()) error {
for i := 0; i < updateCount; i++ { for i := 0; i < updateCount; i++ {
restorable.ClearVolatileImages() restorable.ClearVolatileImages()
setRunningSlowly(i < updateCount-1) setRunningSlowly(i < updateCount-1)
if err := hooks.Run(); err != nil {
return err
}
if err := c.f(c.offscreen); err != nil { if err := c.f(c.offscreen); err != nil {
return err return err
} }

View File

@ -1,4 +1,4 @@
// Copyright 2017 The Ebiten Authors // Copyright 2018 The Ebiten Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -12,14 +12,21 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package audiobinding package hooks
var ch = make(chan error, 1) var onUpdateHooks = []func() error{}
func Error() <-chan error { // AppendHookOnUpdate appends a hook function that is run before the main update function
return ch // every frame.
func AppendHookOnUpdate(f func() error) {
onUpdateHooks = append(onUpdateHooks, f)
} }
func SetError(err error) { func Run() error {
ch <- err for _, f := range onUpdateHooks {
if err := f(); err != nil {
return err
}
}
return nil
} }