From 552fbb7ed921a00c5cc777cfef08d3c85a289759 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 5 Nov 2020 00:34:51 +0900 Subject: [PATCH] examples/contextlost: Enable to build on non-JS environments --- examples/contextlost/context_js.go | 63 ++++++++++++++++++++ examples/contextlost/context_notjs.go | 26 ++++++++ examples/contextlost/{main_js.go => main.go} | 50 ++-------------- 3 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 examples/contextlost/context_js.go create mode 100644 examples/contextlost/context_notjs.go rename examples/contextlost/{main_js.go => main.go} (67%) diff --git a/examples/contextlost/context_js.go b/examples/contextlost/context_js.go new file mode 100644 index 000000000..14c5c9452 --- /dev/null +++ b/examples/contextlost/context_js.go @@ -0,0 +1,63 @@ +// Copyright 2020 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build example + +package main + +import ( + "fmt" + "syscall/js" + "time" +) + +func (g *Game) loseAndRestoreContext() { + doc := js.Global().Get("document") + canvas := doc.Call("getElementsByTagName", "canvas").Index(0) + context := canvas.Call("getContext", "webgl2") + if !context.Truthy() { + context = canvas.Call("getContext", "webgl") + if !context.Truthy() { + context = canvas.Call("getContext", "experimental-webgl") + } + } + + if g.lost { + return + } + + // Edge might not support the extension. See + // https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_lose_context + ext := context.Call("getExtension", "WEBGL_lose_context") + if !ext.Truthy() { + fmt.Println("Fail to force context lost. Edge might not support the extension yet.") + return + } + + ext.Call("loseContext") + fmt.Println("Lost the context!") + fmt.Println("The context is automatically restored after 3 seconds.") + g.lost = true + + // If and only if the context is lost by loseContext, you need to call restoreContext. Note that in usual + // case of context lost, you cannot call restoreContext but the context should be restored automatically. + // + // After the context is lost, update will not be called. Instead, fire the goroutine to restore the context. + go func() { + time.Sleep(3 * time.Second) + ext.Call("restoreContext") + fmt.Println("Restored the context!") + g.lost = false + }() +} diff --git a/examples/contextlost/context_notjs.go b/examples/contextlost/context_notjs.go new file mode 100644 index 000000000..d40b9c54c --- /dev/null +++ b/examples/contextlost/context_notjs.go @@ -0,0 +1,26 @@ +// Copyright 2020 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build example +// +build !js + +package main + +import ( + "fmt" +) + +func (g *Game) loseAndRestoreContext() { + fmt.Println("Losing context lost works only on browsers.") +} diff --git a/examples/contextlost/main_js.go b/examples/contextlost/main.go similarity index 67% rename from examples/contextlost/main_js.go rename to examples/contextlost/main.go index fedb9c38d..0d91e7dd7 100644 --- a/examples/contextlost/main_js.go +++ b/examples/contextlost/main.go @@ -18,13 +18,10 @@ package main import ( "bytes" - "fmt" "image" _ "image/jpeg" "log" "math" - "syscall/js" - "time" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" @@ -33,8 +30,8 @@ import ( ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 640 + screenHeight = 480 ) var ( @@ -47,48 +44,9 @@ type Game struct { lost bool } -func (g *Game) loseAndRestoreContext(context js.Value) { - if g.lost { - return - } - - // Edge might not support the extension. See - // https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_lose_context - ext := context.Call("getExtension", "WEBGL_lose_context") - if !ext.Truthy() { - fmt.Println("Fail to force context lost. Edge might not support the extension yet.") - return - } - - ext.Call("loseContext") - fmt.Println("Lost the context!") - fmt.Println("The context is automatically restored after 3 seconds.") - g.lost = true - - // If and only if the context is lost by loseContext, you need to call restoreContext. Note that in usual - // case of context lost, you cannot call restoreContext but the context should be restored automatically. - // - // After the context is lost, update will not be called. Instead, fire the goroutine to restore the context. - go func() { - time.Sleep(3 * time.Second) - ext.Call("restoreContext") - fmt.Println("Restored the context!") - g.lost = false - }() -} - func (g *Game) Update() error { if inpututil.IsKeyJustPressed(ebiten.KeySpace) { - doc := js.Global().Get("document") - canvas := doc.Call("getElementsByTagName", "canvas").Index(0) - context := canvas.Call("getContext", "webgl2") - if !context.Truthy() { - context = canvas.Call("getContext", "webgl") - if !context.Truthy() { - context = canvas.Call("getContext", "experimental-webgl") - } - } - g.loseAndRestoreContext(context) + g.loseAndRestoreContext() return nil } @@ -151,7 +109,7 @@ func main() { extraImages = append(extraImages, eimg) } - ebiten.SetWindowSize(screenWidth*2, screenHeight*2) + ebiten.SetWindowSize(screenWidth, screenHeight) ebiten.SetWindowTitle("Context Lost (Ebiten Demo)") if err := ebiten.RunGame(&Game{}); err != nil { log.Fatal(err)