diff --git a/image.go b/image.go index 4e4c9364c..b12a54a54 100644 --- a/image.go +++ b/image.go @@ -717,6 +717,11 @@ func (i *Image) RGBA64At(x, y int) color.RGBA64 { } func (i *Image) at(x, y int) (r, g, b, a uint8) { + // Check the error existence and avoid unnecessary calls. + if ui.HasError() { + return 0, 0, 0, 0 + } + if i.isDisposed() { return 0, 0, 0, 0 } diff --git a/internal/ui/context.go b/internal/ui/context.go index 66a886a12..ff6084d89 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -66,7 +66,7 @@ func (c *contextImpl) forceUpdateFrame(outsideWidth, outsideHeight float64, devi } func (c *contextImpl) updateFrameImpl(updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error { - if err := theGlobalState.err(); err != nil { + if err := theGlobalState.error(); err != nil { return err } @@ -102,6 +102,10 @@ func (c *contextImpl) updateFrameImpl(updateCount int, outsideWidth, outsideHeig if err := c.game.Update(); err != nil { return err } + // Catch the error that happened at (*Image).At. + if err := theGlobalState.error(); err != nil { + return err + } Get().resetForTick() } @@ -177,7 +181,7 @@ type globalState struct { screenFilterEnabled_ int32 } -func (g *globalState) err() error { +func (g *globalState) error() error { err, ok := g.err_.Load().(error) if !ok { return nil @@ -238,6 +242,10 @@ func (g *globalState) setScreenFilterEnabled(enabled bool) { atomic.StoreInt32(&g.screenFilterEnabled_, v) } +func HasError() bool { + return theGlobalState.error() != nil +} + func SetError(err error) { theGlobalState.setError(err) }