uidriver: Remove dependency on hooks package

This commit is contained in:
Hajime Hoshi 2019-04-08 23:03:56 -04:00
parent 099c7bd5c9
commit aecfd6b13d
5 changed files with 33 additions and 23 deletions

View File

@ -171,3 +171,11 @@ func (c *graphicsContext) restoreIfNeeded() error {
} }
return nil return nil
} }
func (c *graphicsContext) SuspendAudio() {
hooks.SuspendAudio()
}
func (c *graphicsContext) ResumeAudio() {
hooks.ResumeAudio()
}

View File

@ -22,6 +22,8 @@ import (
type GraphicsContext interface { type GraphicsContext interface {
SetSize(width, height int, scale float64) SetSize(width, height int, scale float64)
Update(afterFrameUpdate func()) error Update(afterFrameUpdate func()) error
SuspendAudio()
ResumeAudio()
} }
// RegularTermination represents a regular termination. // RegularTermination represents a regular termination.

View File

@ -29,7 +29,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/glfw" "github.com/hajimehoshi/ebiten/internal/glfw"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/mainthread" "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()) u.input.update(u.window, u.getScale()*u.glfwScale())
defer hooks.ResumeAudio() defer g.ResumeAudio()
for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 { for !u.isRunnableInBackground() && u.window.GetAttrib(glfw.Focused) == 0 {
hooks.SuspendAudio() g.SuspendAudio()
// Wait for an arbitrary period to avoid busy loop. // Wait for an arbitrary period to avoid busy loop.
time.Sleep(time.Second / 60) time.Sleep(time.Second / 60)
glfw.PollEvents() glfw.PollEvents()

View File

@ -26,7 +26,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/hooks"
) )
var canvas js.Value var canvas js.Value
@ -46,6 +45,7 @@ type UserInterface struct {
lastActualScale float64 lastActualScale float64
context driver.GraphicsContext
input Input input Input
} }
@ -188,7 +188,7 @@ func (u *UserInterface) actualScreenScale() float64 {
return u.getScale() * devicescale.GetAt(0, 0) return u.getScale() * devicescale.GetAt(0, 0)
} }
func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) { func (u *UserInterface) updateGraphicsContext() {
a := u.actualScreenScale() a := u.actualScreenScale()
if u.lastActualScale != a { if u.lastActualScale != a {
u.updateScreenSize() u.updateScreenSize()
@ -197,7 +197,7 @@ func (u *UserInterface) updateGraphicsContext(g driver.GraphicsContext) {
if u.sizeChanged { if u.sizeChanged {
u.sizeChanged = false 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) return !u.runnableInBackground && (!u.windowFocus || !u.pageVisible)
} }
func (u *UserInterface) update(g driver.GraphicsContext) error { func (u *UserInterface) update() error {
if u.suspended() { if u.suspended() {
hooks.SuspendAudio() u.context.SuspendAudio()
return nil return nil
} }
hooks.ResumeAudio() u.context.ResumeAudio()
u.input.UpdateGamepads() u.input.UpdateGamepads()
u.updateGraphicsContext(g) u.updateGraphicsContext()
if err := g.Update(func() { if err := u.context.Update(func() {
u.updateGraphicsContext(g) u.updateGraphicsContext()
}); err != nil { }); err != nil {
return err return err
} }
@ -223,6 +223,8 @@ func (u *UserInterface) update(g driver.GraphicsContext) error {
} }
func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error { func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error {
u.context = g
ch := make(chan error) ch := make(chan error)
var cf js.Callback var cf js.Callback
f := func([]js.Value) { f := func([]js.Value) {
@ -231,7 +233,7 @@ func (u *UserInterface) loop(g driver.GraphicsContext) <-chan error {
return return
} }
if err := u.update(g); err != nil { if err := u.update(); err != nil {
ch <- err ch <- err
close(ch) close(ch)
return return
@ -262,25 +264,25 @@ func init() {
window.Call("addEventListener", "focus", js.NewCallback(func([]js.Value) { window.Call("addEventListener", "focus", js.NewCallback(func([]js.Value) {
theUI.windowFocus = true theUI.windowFocus = true
if theUI.suspended() { if theUI.suspended() {
hooks.SuspendAudio() theUI.context.SuspendAudio()
} else { } else {
hooks.ResumeAudio() theUI.context.ResumeAudio()
} }
})) }))
window.Call("addEventListener", "blur", js.NewCallback(func([]js.Value) { window.Call("addEventListener", "blur", js.NewCallback(func([]js.Value) {
theUI.windowFocus = false theUI.windowFocus = false
if theUI.suspended() { if theUI.suspended() {
hooks.SuspendAudio() theUI.context.SuspendAudio()
} else { } else {
hooks.ResumeAudio() theUI.context.ResumeAudio()
} }
})) }))
document.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) { document.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) {
theUI.pageVisible = !document.Get("hidden").Bool() theUI.pageVisible = !document.Get("hidden").Bool()
if theUI.suspended() { if theUI.suspended() {
hooks.SuspendAudio() theUI.context.SuspendAudio()
} else { } else {
hooks.ResumeAudio() theUI.context.ResumeAudio()
} }
})) }))
window.Call("addEventListener", "resize", js.NewCallback(func([]js.Value) { window.Call("addEventListener", "resize", js.NewCallback(func([]js.Value) {

View File

@ -33,7 +33,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/devicescale" "github.com/hajimehoshi/ebiten/internal/devicescale"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/internal/hooks"
) )
var ( var (
@ -236,11 +235,11 @@ render:
case <-renderCh: case <-renderCh:
break render break render
case <-time.After(500 * time.Millisecond): case <-time.After(500 * time.Millisecond):
hooks.SuspendAudio() g.SuspendAudio()
continue continue
} }
} }
hooks.ResumeAudio() g.ResumeAudio()
defer func() { defer func() {
renderChEnd <- struct{}{} renderChEnd <- struct{}{}