From 6bbdf7e183343c23f49e35b80af405e68d8762b7 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Wed, 9 Jul 2014 02:56:35 +0200 Subject: [PATCH] Bitmap#blt: Clamp source rect to source bitmap bounds RGSS allows the source rectangle in both `blt` and `stretch_blt` to lie outside the source bitmap bounds (treating the missing data as (0, 0, 0, 0)) and to be inverted (in which case the blitted image is also inverted). This commit only hanldes a corner case that arises in the game "Last Scenario"; emulating the full RGSS behavior is however desirable. --- src/bitmap.cpp | 14 +++++++++++++- src/bitmap.h | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/bitmap.cpp b/src/bitmap.cpp index b951f52..d023f55 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -278,9 +278,21 @@ IntRect Bitmap::rect() const } void Bitmap::blt(int x, int y, - const Bitmap &source, const IntRect &rect, + const Bitmap &source, IntRect rect, int opacity) { + // FIXME: RGSS allows the source rect to both lie outside + // the bitmap rect and be inverted in both directions; + // clamping only covers a subset of these cases (and + // doesn't fix anything for a direct stretch_blt call). + + /* Clamp rect to source bitmap size */ + if (rect.x + rect.w > source.width()) + rect.w = source.width() - rect.x; + + if (rect.y + rect.h > source.height()) + rect.h = source.height() - rect.y; + stretchBlt(IntRect(x, y, rect.w, rect.h), source, rect, opacity); } diff --git a/src/bitmap.h b/src/bitmap.h index cb491bb..fd4d8c6 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -49,7 +49,7 @@ public: IntRect rect() const; void blt(int x, int y, - const Bitmap &source, const IntRect &rect, + const Bitmap &source, IntRect rect, int opacity = 255); void stretchBlt(const IntRect &destRect,