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
}
func (c *graphicsContext) SuspendAudio() {
hooks.SuspendAudio()
}
func (c *graphicsContext) ResumeAudio() {
hooks.ResumeAudio()
}

View File

@ -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.

View File

@ -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()

View File

@ -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) {

View File

@ -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{}{}