From d089b0d83cdf7ff3b69af0f6fcbf1dd3e5eb8759 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Mon, 27 Nov 2023 13:50:52 -0600 Subject: [PATCH 1/2] add graphic.isDestroyed, prevent render crashes --- flixel/FlxSprite.hx | 10 ++++++++++ flixel/graphics/FlxGraphic.hx | 19 +++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 9eac31aa4f..61e1aa84b1 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -774,6 +774,16 @@ class FlxSprite extends FlxObject { if (_frame == null) loadGraphic("flixel/images/logo/default.png"); + else if (graphic != null && graphic.isDestroyed) + { + // switch graphic but log and preserve size + final width = this.width; + final height = this.height; + FlxG.log.warn('Cannot render a destroyed graphic, the placeholder image will be used instead'); + loadGraphic("flixel/images/logo/default.png"); + this.width = width; + this.height = height; + } } /** diff --git a/flixel/graphics/FlxGraphic.hx b/flixel/graphics/FlxGraphic.hx index f496ba7e2b..b3140b93e2 100644 --- a/flixel/graphics/FlxGraphic.hx +++ b/flixel/graphics/FlxGraphic.hx @@ -313,6 +313,12 @@ class FlxGraphic implements IFlxDestroyable */ public var isLoaded(get, never):Bool; + /** + * Whether `destroy` was called on this graphic + * @since 5.6.0 + */ + public var isDestroyed(get, never):Bool; + /** * Whether the `BitmapData` of this graphic object can be dumped for decreased memory usage, * but may cause some issues (when you need direct access to pixels of this graphic. @@ -388,14 +394,14 @@ class FlxGraphic implements IFlxDestroyable * @param Persist Whether or not this graphic stay in the cache after resetting it. * Default value is `false`, which means that this graphic will be destroyed at the cache reset. */ - function new(Key:String, Bitmap:BitmapData, ?Persist:Bool) + function new(key:String, bitmap:BitmapData, ?persist:Bool) { - key = Key; - persist = (Persist != null) ? Persist : defaultPersist; + this.key = key; + this.persist = (persist != null) ? persist : defaultPersist; frameCollections = new Map>(); frameCollectionTypes = new Array(); - bitmap = Bitmap; + this.bitmap = bitmap; shader = new FlxShader(); } @@ -552,6 +558,11 @@ class FlxGraphic implements IFlxDestroyable { return bitmap != null && !bitmap.rect.isEmpty(); } + + inline function get_isDestroyed() + { + return shader == null; + } inline function get_canBeDumped():Bool { From a12be1386eae94b1ab456e0b4e0c4c82c9d432ec Mon Sep 17 00:00:00 2001 From: George FunBook Date: Mon, 27 Nov 2023 15:21:34 -0600 Subject: [PATCH 2/2] error instead of warning --- flixel/FlxSprite.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 61e1aa84b1..feac4eb8de 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -779,7 +779,7 @@ class FlxSprite extends FlxObject // switch graphic but log and preserve size final width = this.width; final height = this.height; - FlxG.log.warn('Cannot render a destroyed graphic, the placeholder image will be used instead'); + FlxG.log.error('Cannot render a destroyed graphic, the placeholder image will be used instead'); loadGraphic("flixel/images/logo/default.png"); this.width = width; this.height = height;