diff --git a/examples/contextlost/main.go b/examples/contextlost/main.go index 2d06d84a0..e8b799542 100644 --- a/examples/contextlost/main.go +++ b/examples/contextlost/main.go @@ -93,6 +93,10 @@ func (g *Game) Update(screen *ebiten.Image) error { return nil } + if inpututil.IsKeyJustPressed(ebiten.KeyS) { + ebiten.SetClearingScreenSkipped(!ebiten.IsClearingScreenSkipped()) + } + g.count++ return nil } @@ -107,7 +111,12 @@ func (g *Game) Draw(screen *ebiten.Image) { op.GeoM.Translate(screenWidth/2, screenHeight/2) screen.DrawImage(gophersImage, op) - ebitenutil.DebugPrint(screen, "Press Space to force to lose/restore the GL context!\n(Browser only)") + msg := `Press Space to force to lose/restore the GL context! +(Browser only) + +Press S to switch clearing the screen +at the beginning of each frame.` + ebitenutil.DebugPrint(screen, msg) } func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { diff --git a/run.go b/run.go index b8698050a..f64eb5751 100644 --- a/run.go +++ b/run.go @@ -112,6 +112,7 @@ func SetClearingScreenSkipped(skipped bool) { v = 1 } atomic.StoreInt32(&isClearingScreenSkipped, v) + theUIContext.setClearingScreenSkipped(skipped) } // IsClearingScreenSkipped returns true if the frame isn't cleared at the beginning. diff --git a/uicontext.go b/uicontext.go index 57986100b..ede097e8c 100644 --- a/uicontext.go +++ b/uicontext.go @@ -176,7 +176,7 @@ func (c *uiContext) updateOffscreen() { } if c.offscreen == nil { c.offscreen = newImage(sw, sh, FilterDefault) - c.offscreen.mipmap.SetVolatile(true) + c.offscreen.mipmap.SetVolatile(!IsClearingScreenSkipped()) } // The window size is automatically adjusted when Run is used. @@ -193,6 +193,15 @@ func (c *uiContext) updateOffscreen() { // scale. This is fine since ebiten.ScreenScale will be deprecated. } +func (c *uiContext) setClearingScreenSkipped(skipped bool) { + c.m.Lock() + defer c.m.Unlock() + + if c.offscreen != nil { + c.offscreen.mipmap.SetVolatile(!skipped) + } +} + func (c *uiContext) setWindowResizable(resizable bool) { c.m.Lock() defer c.m.Unlock()