opengl: Prepare Context Losing for JavaScript (#217)

This commit is contained in:
Hajime Hoshi 2016-06-29 23:16:21 +09:00
parent 9453f9e7b5
commit 328ef85606

View File

@ -29,6 +29,7 @@ type userInterface struct {
scale float64 scale float64
deviceScale float64 deviceScale float64
sizeChanged bool sizeChanged bool
contextRestored chan struct{}
} }
var currentUI = &userInterface{ var currentUI = &userInterface{
@ -80,6 +81,9 @@ func (u *userInterface) ActualScreenScale() float64 {
} }
func (u *userInterface) Update() (interface{}, error) { func (u *userInterface) Update() (interface{}, error) {
if u.contextRestored != nil {
<-u.contextRestored
}
currentInput.updateGamepads() currentInput.updateGamepads()
if u.sizeChanged { if u.sizeChanged {
u.sizeChanged = false u.sizeChanged = false
@ -229,6 +233,15 @@ func initialize() (*opengl.Context, error) {
// Do nothing. // Do nothing.
}) })
canvas.Call("addEventListener", "webglcontextlost", func(e *js.Object) {
e.Call("preventDefault")
currentUI.contextRestored = make(chan struct{})
})
canvas.Call("addEventListener", "webglcontextrestored", func(e *js.Object) {
close(currentUI.contextRestored)
currentUI.contextRestored = nil
})
c, err := opengl.NewContext() c, err := opengl.NewContext()
if err != nil { if err != nil {
return nil, err return nil, err