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

@ -26,9 +26,10 @@ import (
var canvas *js.Object
type userInterface struct {
scale float64
deviceScale float64
sizeChanged bool
scale float64
deviceScale float64
sizeChanged bool
contextRestored chan struct{}
}
var currentUI = &userInterface{
@ -80,6 +81,9 @@ func (u *userInterface) ActualScreenScale() float64 {
}
func (u *userInterface) Update() (interface{}, error) {
if u.contextRestored != nil {
<-u.contextRestored
}
currentInput.updateGamepads()
if u.sizeChanged {
u.sizeChanged = false
@ -229,6 +233,15 @@ func initialize() (*opengl.Context, error) {
// 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()
if err != nil {
return nil, err