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

FlxFramesCollection: check and trim frame sizes, closes #1937 #1966

Merged
merged 5 commits into from
Sep 30, 2016
Merged
Changes from 3 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
32 changes: 29 additions & 3 deletions flixel/graphics/frames/FlxFramesCollection.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package flixel.graphics.frames;

import flixel.FlxG;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxFrame.FlxFrameAngle;
import flixel.graphics.frames.FlxFrame.FlxFrameType;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.util.FlxDestroyUtil;
Expand Down Expand Up @@ -149,7 +151,7 @@ class FlxFramesCollection implements IFlxDestroyable
public function addSpriteSheetFrame(region:FlxRect):FlxFrame
{
var frame:FlxFrame = new FlxFrame(parent);
frame.frame = region;
frame.frame = checkFrame(region);
frame.sourceSize.set(region.width, region.height);
frame.offset.set(0, 0);
return pushFrame(frame);
Expand Down Expand Up @@ -177,14 +179,38 @@ class FlxFramesCollection implements IFlxDestroyable
texFrame.name = name;
texFrame.sourceSize.set(sourceSize.x, sourceSize.y);
texFrame.offset.set(offset.x, offset.y);
texFrame.frame = frame;
texFrame.frame = checkFrame(frame, name);

sourceSize = FlxDestroyUtil.put(sourceSize);
offset = FlxDestroyUtil.put(offset);

return pushFrame(texFrame);
}

/**
* Checks if frame's area fits into atlas image, and trims if it's out of atlas image bounds
* @param frame frame area to check.
* @param name optional frame name for debugging info.
* @return checked and trimmed frame rectangle.
*/
private inline function checkFrame(frame:FlxRect, ?name:String):FlxRect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function is a good inline candidate - too long.

{
var x:Float = FlxMath.bound(frame.x, 0, parent.width);
var y:Float = FlxMath.bound(frame.y, 0, parent.height);

var r:Float = FlxMath.bound(frame.right, 0, parent.width);
var b:Float = FlxMath.bound(frame.bottom, 0, parent.height);

frame.set(x, y, r - x, b - y);

if (frame.width <= 0 || frame.height <= 0)
{
FlxG.log.warn("The frame " + name + " have incorrect data and results in image with the size of (0; 0)");
}

return frame;
}

/**
* Helper method for adding frame into collection
*
Expand Down Expand Up @@ -247,4 +273,4 @@ enum FlxFrameCollectionType
FONT;
USER(type:String);
FILTER;
}
}