Sprite: Clamp src_rect to bitmap bounds

This commit is contained in:
Jonas Kulla 2017-02-17 19:29:38 +01:00
parent 541e24f678
commit 55cec53911
1 changed files with 16 additions and 5 deletions

View File

@ -134,12 +134,23 @@ struct SpritePrivate
void onSrcRectChange()
{
if (mirrored)
quad.setTexRect(srcRect->toFloatRect().hFlipped());
else
quad.setTexRect(srcRect->toFloatRect());
FloatRect rect = srcRect->toFloatRect();
Vec2i bmSize;
quad.setPosRect(IntRect(0, 0, srcRect->width, srcRect->height));
if (bitmap)
bmSize = Vec2i(bitmap->width(), bitmap->height());
if (mirrored)
rect = rect.hFlipped();
/* Clamp the rectangle so it doesn't reach outside
* the bitmap bounds */
rect.w = clamp<int>(rect.w, 0, bmSize.x-rect.x);
rect.h = clamp<int>(rect.h, 0, bmSize.y-rect.y);
quad.setTexRect(rect);
quad.setPosRect(FloatRect(0, 0, rect.w, rect.h));
recomputeBushDepth();
wave.dirty = true;