Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graphic.isDestroyed, prevent render crashes #2974

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.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;
}
}

/**
Expand Down
19 changes: 15 additions & 4 deletions flixel/graphics/FlxGraphic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<FlxFrameCollectionType, Array<Dynamic>>();
frameCollectionTypes = new Array<FlxFrameCollectionType>();
bitmap = Bitmap;
this.bitmap = bitmap;

shader = new FlxShader();
}
Expand Down Expand Up @@ -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
{
Expand Down
Loading