From 673556d03f256cb64bbe273634dde9d7253bfb09 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 20 Mar 2022 16:26:26 +0900 Subject: [PATCH] internal/ui: move the error handlings to the ui package --- export_test.go | 4 ---- image.go | 34 ++++----------------------------- image_test.go | 3 ++- internal/ui/context.go | 8 -------- internal/ui/image.go | 43 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/export_test.go b/export_test.go index 8ac230b77..affa4ce57 100644 --- a/export_test.go +++ b/export_test.go @@ -17,7 +17,3 @@ package ebiten var ( ImageToBytes = imageToBytes ) - -func PanicOnErrorAtImageAt() { - panicOnErrorAtImageAt = true -} diff --git a/image.go b/image.go index e45855b28..17b4f7205 100644 --- a/image.go +++ b/image.go @@ -25,10 +25,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/ui" ) -// panicOnErrorAtImageAt indicates whether (*Image).At panics on an error or not. -// This value is set only on testing. -var panicOnErrorAtImageAt bool - // Image represents a rectangle set of pixels. // The pixel format is alpha-premultiplied RGBA. // Image implements image.Image and draw.Image. @@ -716,24 +712,14 @@ 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. - // TODO: The package ui should have an image struct that treats errors correctly. - if ui.HasError() { - return 0, 0, 0, 0 - } - if i.isDisposed() { return 0, 0, 0, 0 } if !image.Pt(x, y).In(i.Bounds()) { return 0, 0, 0, 0 } - pix, err := i.image.Pixels(x, y, 1, 1) - if err != nil { - if panicOnErrorAtImageAt { - panic(err) - } - ui.SetError(err) + pix := i.image.Pixels(x, y, 1, 1) + if len(pix) == 0 { return 0, 0, 0, 0 } return pix[0], pix[1], pix[2], pix[3] @@ -747,10 +733,6 @@ func (i *Image) at(x, y int) (r, g, b, a uint8) { // // If the image is disposed, Set does nothing. func (i *Image) Set(x, y int, clr color.Color) { - if ui.HasError() { - return - } - i.copyCheck() if i.isDisposed() { return @@ -764,9 +746,7 @@ func (i *Image) Set(x, y int, clr color.Color) { r, g, b, a := clr.RGBA() pix := []byte{byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)} - if err := i.image.ReplaceSmallRegionPixels(pix, x, y, 1, 1); err != nil { - ui.SetError(err) - } + i.image.ReplaceSmallRegionPixels(pix, x, y, 1, 1) } // Dispose disposes the image data. @@ -802,10 +782,6 @@ func (i *Image) Dispose() { // // When the image is disposed, ReplacePixels does nothing. func (i *Image) ReplacePixels(pixels []byte) { - if ui.HasError() { - return - } - i.copyCheck() if i.isDisposed() { @@ -816,9 +792,7 @@ func (i *Image) ReplacePixels(pixels []byte) { // Do not need to copy pixels here. // * In internal/mipmap, pixels are copied when necessary. // * In internal/atlas, pixels are copied to make its paddings. - if err := i.image.ReplaceLargeRegionPixels(pixels, r.Min.X, r.Min.Y, r.Dx(), r.Dy()); err != nil { - ui.SetError(err) - } + i.image.ReplaceLargeRegionPixels(pixels, r.Min.X, r.Min.Y, r.Dx(), r.Dy()) } // NewImage returns an empty image. diff --git a/image_test.go b/image_test.go index fad58081a..cf5e95c43 100644 --- a/image_test.go +++ b/image_test.go @@ -31,6 +31,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/examples/resources/images" "github.com/hajimehoshi/ebiten/v2/internal/graphics" t "github.com/hajimehoshi/ebiten/v2/internal/testing" + "github.com/hajimehoshi/ebiten/v2/internal/ui" ) // maxImageSize is a maximum image size that should work in almost every environment. @@ -53,7 +54,7 @@ func skipTooSlowTests(t *testing.T) bool { } func TestMain(m *testing.M) { - ebiten.PanicOnErrorAtImageAt() + ui.SetPanicOnErrorAtImageAtForTesting(true) t.MainWithRunLoop(m) } diff --git a/internal/ui/context.go b/internal/ui/context.go index f609d793e..c3132144d 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -242,14 +242,6 @@ 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) -} - func FPSMode() FPSModeType { return theGlobalState.fpsMode() } diff --git a/internal/ui/image.go b/internal/ui/image.go index 4686dbb37..019590a7f 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -22,6 +22,14 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/mipmap" ) +// panicOnErrorAtImageAt indicates whether (*Image).At panics on an error or not. +// This value is set only on testing. +var panicOnErrorAtImageAt bool + +func SetPanicOnErrorAtImageAtForTesting(value bool) { + panicOnErrorAtImageAt = value +} + type Image struct { mipmap *mipmap.Mipmap } @@ -60,16 +68,39 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd, canSkipMipmap) } -func (i *Image) ReplaceLargeRegionPixels(pix []byte, x, y, width, height int) error { - return i.mipmap.ReplaceLargeRegionPixels(pix, x, y, width, height) +func (i *Image) ReplaceLargeRegionPixels(pix []byte, x, y, width, height int) { + if theGlobalState.error() != nil { + return + } + if err := i.mipmap.ReplaceLargeRegionPixels(pix, x, y, width, height); err != nil { + theGlobalState.setError(err) + } } -func (i *Image) ReplaceSmallRegionPixels(pix []byte, x, y, width, height int) error { - return i.mipmap.ReplaceSmallRegionPixels(graphicsDriver(), pix, x, y, width, height) +func (i *Image) ReplaceSmallRegionPixels(pix []byte, x, y, width, height int) { + if theGlobalState.error() != nil { + return + } + if err := i.mipmap.ReplaceSmallRegionPixels(graphicsDriver(), pix, x, y, width, height); err != nil { + theGlobalState.setError(err) + } } -func (i *Image) Pixels(x, y, width, height int) ([]byte, error) { - return i.mipmap.Pixels(graphicsDriver(), x, y, width, height) +func (i *Image) Pixels(x, y, width, height int) []byte { + // Check the error existence and avoid unnecessary calls. + if theGlobalState.error() != nil { + return nil + } + + pix, err := i.mipmap.Pixels(graphicsDriver(), x, y, width, height) + if err != nil { + if panicOnErrorAtImageAt { + panic(err) + } + theGlobalState.setError(err) + return nil + } + return pix } func (i *Image) DumpScreenshot(name string, blackbg bool) error {