From aecfd6b13d4584cd10bc6e7b5b60d4b9bf5f1a2c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 8 Apr 2019 23:03:56 -0400 Subject: [PATCH] uidriver: Remove dependency on hooks package --- graphicscontext.go | 8 ++++++++ internal/driver/ui.go | 2 ++ internal/uidriver/glfw/ui.go | 5 ++--- internal/uidriver/js/ui.go | 36 ++++++++++++++++++---------------- internal/uidriver/mobile/ui.go | 5 ++--- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index ffc0c44e2..0cc3b1cec 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -171,3 +171,11 @@ func (c *graphicsContext) restoreIfNeeded() error { } return nil } + +func (c *graphicsContext) SuspendAudio() { + hooks.SuspendAudio() +} + +func (c *graphicsContext) ResumeAudio() { + hooks.ResumeAudio() +} diff --git a/internal/driver/ui.go b/internal/driver/ui.go index fa98c307d..f09e8b9f9 100644 --- a/internal/driver/ui.go +++ b/internal/driver/ui.go @@ -22,6 +22,8 @@ import ( type GraphicsContext interface { SetSize(width, height int, scale float64) Update(afterFrameUpdate func()) error + SuspendAudio() + ResumeAudio() } // RegularTermination represents a regular termination. diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 291f00169..a2ad73daf 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -29,7 +29,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/glfw" - "github.com/hajimehoshi/ebiten/internal/hooks" "github.com/hajimehoshi/ebiten/internal/mainthread" ) @@ -750,10 +749,10 @@ func (u *UserInterface) update(g driver.GraphicsContext) error { u.input.update(u.window, u.getScale()*u.glfwScale()) - defer hooks.ResumeAudio() + defer g.ResumeAudio() for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 { - hooks.SuspendAudio() + g.SuspendAudio() // Wait for an arbitrary period to avoid busy loop. time.Sleep(time.Second / 60) glfw.PollEvents() diff --git a/internal/uidriver/js/ui.go b/internal/uidriver/js/ui.go index 20cb366b7..683b758bd 100644 --- a/internal/uidriver/js/ui.go +++ b/internal/uidriver/js/ui.go @@ -26,7 +26,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/driver" - "github.com/hajimehoshi/ebiten/internal/hooks" ) var canvas js.Value @@ -46,7 +45,8 @@ type UserInterface struct { lastActualScale float64 - input Input + context driver.GraphicsContext + input Input } var theUI = &UserInterface{ @@ -188,7 +188,7 @@ func (u *UserInterface) actualScreenScale() float64 { return u.getScale() * devicescale.GetAt(0, 0) } -func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) { +func (u *UserInterface) updateGraphicsContext() { a := u.actualScreenScale() if u.lastActualScale != a { u.updateScreenSize() @@ -197,7 +197,7 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) { if u.sizeChanged { u.sizeChanged = false - g.SetSize(u.width, u.height, a) + u.context.SetSize(u.width, u.height, a) } } @@ -205,17 +205,17 @@ func (u *UserInterface) suspended() bool { return !u.runnableInBackground && (!u.windowFocus || !u.pageVisible) } -func (u *UserInterface) update(g driver.GraphicsContext) error { +func (u *UserInterface) update() error { if u.suspended() { - hooks.SuspendAudio() + u.context.SuspendAudio() return nil } - hooks.ResumeAudio() + u.context.ResumeAudio() u.input.UpdateGamepads() - u.updateGraphicsContext(g) - if err := g.Update(func() { - u.updateGraphicsContext(g) + u.updateGraphicsContext() + if err := u.context.Update(func() { + u.updateGraphicsContext() }); err != nil { return err } @@ -223,6 +223,8 @@ func (u *UserInterface) update(g driver.GraphicsContext) error { } func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error { + u.context = g + ch := make(chan error) var cf js.Callback f := func([]js.Value) { @@ -231,7 +233,7 @@ func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error { return } - if err := u.update(g); err != nil { + if err := u.update(); err != nil { ch <- err close(ch) return @@ -262,25 +264,25 @@ func init() { window.Call("addEventListener", "focus", js.NewCallback(func([]js.Value) { theUI.windowFocus = true if theUI.suspended() { - hooks.SuspendAudio() + theUI.context.SuspendAudio() } else { - hooks.ResumeAudio() + theUI.context.ResumeAudio() } })) window.Call("addEventListener", "blur", js.NewCallback(func([]js.Value) { theUI.windowFocus = false if theUI.suspended() { - hooks.SuspendAudio() + theUI.context.SuspendAudio() } else { - hooks.ResumeAudio() + theUI.context.ResumeAudio() } })) document.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) { theUI.pageVisible = !document.Get("hidden").Bool() if theUI.suspended() { - hooks.SuspendAudio() + theUI.context.SuspendAudio() } else { - hooks.ResumeAudio() + theUI.context.ResumeAudio() } })) window.Call("addEventListener", "resize", js.NewCallback(func([]js.Value) { diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index 4ca72bd81..c204ee0e4 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -33,7 +33,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" - "github.com/hajimehoshi/ebiten/internal/hooks" ) var ( @@ -236,11 +235,11 @@ render: case <-renderCh: break render case <-time.After(500 * time.Millisecond): - hooks.SuspendAudio() + g.SuspendAudio() continue } } - hooks.ResumeAudio() + g.ResumeAudio() defer func() { renderChEnd <- struct{}{}