mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
examples/contextlost: Enable to build on non-JS environments
This commit is contained in:
parent
48b46345d7
commit
552fbb7ed9
63
examples/contextlost/context_js.go
Normal file
63
examples/contextlost/context_js.go
Normal file
@ -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
|
||||
}()
|
||||
}
|
26
examples/contextlost/context_notjs.go
Normal file
26
examples/contextlost/context_notjs.go
Normal file
@ -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.")
|
||||
}
|
@ -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)
|
Loading…
Reference in New Issue
Block a user