internal/ui: catch the error at At correctly

This commit is contained in:
Hajime Hoshi 2022-03-05 01:44:59 +09:00
parent df2133186d
commit 9c6b7aaca3
2 changed files with 15 additions and 2 deletions

View File

@ -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) { 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() { if i.isDisposed() {
return 0, 0, 0, 0 return 0, 0, 0, 0
} }

View File

@ -66,7 +66,7 @@ func (c *contextImpl) forceUpdateFrame(outsideWidth, outsideHeight float64, devi
} }
func (c *contextImpl) updateFrameImpl(updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error { 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 return err
} }
@ -102,6 +102,10 @@ func (c *contextImpl) updateFrameImpl(updateCount int, outsideWidth, outsideHeig
if err := c.game.Update(); err != nil { if err := c.game.Update(); err != nil {
return err return err
} }
// Catch the error that happened at (*Image).At.
if err := theGlobalState.error(); err != nil {
return err
}
Get().resetForTick() Get().resetForTick()
} }
@ -177,7 +181,7 @@ type globalState struct {
screenFilterEnabled_ int32 screenFilterEnabled_ int32
} }
func (g *globalState) err() error { func (g *globalState) error() error {
err, ok := g.err_.Load().(error) err, ok := g.err_.Load().(error)
if !ok { if !ok {
return nil return nil
@ -238,6 +242,10 @@ func (g *globalState) setScreenFilterEnabled(enabled bool) {
atomic.StoreInt32(&g.screenFilterEnabled_, v) atomic.StoreInt32(&g.screenFilterEnabled_, v)
} }
func HasError() bool {
return theGlobalState.error() != nil
}
func SetError(err error) { func SetError(err error) {
theGlobalState.setError(err) theGlobalState.setError(err)
} }