From 18e495bca0acf0ac58f3e5204c4921639670b5a3 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Thu, 16 Nov 2023 17:02:46 -0500 Subject: [PATCH] Implement `wasDestroyed` check on FlxGraphic --- flixel/FlxCamera.hx | 5 +++++ flixel/graphics/FlxGraphic.hx | 10 ++++++++++ flixel/graphics/tile/FlxDrawQuadsItem.hx | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/flixel/FlxCamera.hx b/flixel/FlxCamera.hx index acf179f8a4..d171881585 100644 --- a/flixel/FlxCamera.hx +++ b/flixel/FlxCamera.hx @@ -635,6 +635,11 @@ class FlxCamera extends FlxBasic itemToReturn = new FlxDrawItem(); } + if (graphic.wasDestroyed) { + // Throw an exception here, because throwing in `FlxDrawQuadsItem.render()` is not helpful. + throw 'Attempted to queue invalid FlxDrawItem, did you dispose of a cached sprite?'; + } + itemToReturn.graphics = graphic; itemToReturn.antialiasing = smooth; itemToReturn.colored = colored; diff --git a/flixel/graphics/FlxGraphic.hx b/flixel/graphics/FlxGraphic.hx index f496ba7e2b..618861b060 100644 --- a/flixel/graphics/FlxGraphic.hx +++ b/flixel/graphics/FlxGraphic.hx @@ -343,6 +343,16 @@ class FlxGraphic implements IFlxDestroyable */ public var atlasFrames(get, never):FlxAtlasFrames; + /** + * This value is `true` if this graphic has been disposed. + * Attempting to render a disposed graphic will cause an error. + */ + public var wasDestroyed(get, never):Bool; + + function get_wasDestroyed():Bool { + return shader == null; + } + /** * Storage for all available frame collection of all types for this graphic object. */ diff --git a/flixel/graphics/tile/FlxDrawQuadsItem.hx b/flixel/graphics/tile/FlxDrawQuadsItem.hx index cccf2aa6e0..797d2c8d31 100644 --- a/flixel/graphics/tile/FlxDrawQuadsItem.hx +++ b/flixel/graphics/tile/FlxDrawQuadsItem.hx @@ -115,6 +115,12 @@ class FlxDrawQuadsItem extends FlxDrawBaseItem return; var shader = shader != null ? shader : graphics.shader; + + if (shader == null) { + // If this gets called, the check for `graphic.wasDestroyed` in FlxCamera failed. + throw 'Attempted to render invalid FlxDrawItem, did you dispose of a cached sprite?'; + } + shader.bitmap.input = graphics.bitmap; shader.bitmap.filter = (camera.antialiasing || antialiasing) ? LINEAR : NEAREST; shader.alpha.value = alphas;