[Scummvm-git-logs] scummvm master -> 7a546462fd5eff0792a47bcf51b2235f11e8c591
sev-
noreply at scummvm.org
Sun Aug 31 19:43:24 UTC 2025
This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .
Summary:
15e30d360f SAGA2: Remove unused class Rect32
7a546462fd COMMON: Switch Common::Rect and Common::Point to templates
Commit: 15e30d360f561ecfc468fe11036a5fa3cf27b1ce
https://github.com/scummvm/scummvm/commit/15e30d360f561ecfc468fe11036a5fa3cf27b1ce
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2025-08-31T21:43:20+02:00
Commit Message:
SAGA2: Remove unused class Rect32
Changed paths:
engines/saga2/rect.cpp
engines/saga2/rect.h
diff --git a/engines/saga2/rect.cpp b/engines/saga2/rect.cpp
index 4bd41396287..2ec6e6804a7 100644
--- a/engines/saga2/rect.cpp
+++ b/engines/saga2/rect.cpp
@@ -107,59 +107,4 @@ bool Rect16::overlap(const Rect16 &r) const {
&& r.y < y + height && y < r.y + r.height);
}
-Rect32 bound(const Rect32 a, const Rect32 b) {
- int32 x1, x2, y1, y2;
-
- x1 = MIN(a.x, b.x);
- x2 = MAX(a.x + a.width, b.x + b.width) - x1;
- y1 = MIN(a.y, b.y);
- y2 = MAX(a.y + a.height, b.y + b.height) - y1;
-
- return Rect32(x1, y1, x2, y2);
-}
-
-void Rect32::expand(int32 dx, int32 dy) {
- x -= dx;
- width += dx + dx;
- y -= dy;
- height += dy + dy;
-}
-
-void Rect32::expand(int32 left, int32 top, int32 right, int32 bottom) {
- x -= left;
- width += left + right;
- y -= top;
- height += top + bottom;
-}
-
-Rect32 intersect(const Rect32 a, const Rect32 b) {
- int32 x1, width, y1, height;
-
- x1 = MAX(a.x, b.x);
- width = MIN(a.x + a.width, b.x + b.width) - x1;
- y1 = MAX(a.y, b.y);
- height = MIN(a.y + a.height, b.y + b.height) - y1;
-
-// if ( ( width <= 0 ) || ( height <= 0 ) )
-// return Rect32( 0, 0, 0, 0 );
-// else
- return Rect32(x1, y1, width, height);
-}
-
-void Rect32::normalize() {
- if (width < 0) {
- x += width;
- width = -width;
- }
- if (height < 0) {
- y += height;
- height = - height;
- }
-}
-
-bool Rect32::overlap(const Rect32 &r) const {
- return (r.x < x + width && x < r.x + r.width
- && r.y < y + height && y < r.y + r.height);
-}
-
} // end if namespace Saga2
diff --git a/engines/saga2/rect.h b/engines/saga2/rect.h
index b447bc5f28f..353a1d7e261 100644
--- a/engines/saga2/rect.h
+++ b/engines/saga2/rect.h
@@ -383,136 +383,6 @@ public:
Rect16 intersect(const Rect16, const Rect16);
Rect16 bound(const Rect16, const Rect16);
-/* ===================================================================== *
- Rect32: 16-bit 2-D rectangle
- * ===================================================================== */
-
-class Rect32 {
-public:
- int32 x, y,
- width, height;
-
- // constructors
- Rect32() { x = y = width = height = 0; }
- Rect32(int32 nx, int32 ny, int32 nw, int32 nh) {
- x = nx;
- y = ny;
- width = nw;
- height = nh;
- }
- Rect32(Point16 a, int32 nw, int32 nh) {
- x = a.x;
- y = a.y;
- width = nw;
- height = nh;
- }
- Rect32(Point16 a, Point16 b) {
- x = a.x;
- y = a.y;
- width = (int32)(b.x - a.x);
- height = (int32)(b.y - a.y);
- }
- Rect32(Point32 a, int32 nw, int32 nh) {
- x = a.x;
- y = a.y;
- width = nw;
- height = nh;
- }
- Rect32(Point32 a, Point32 b) {
- x = a.x;
- y = a.y;
- width = (int32)(b.x - a.x);
- height = (int32)(b.y - a.y);
- }
- // Conversion operators
- Rect32(Rect16 r) {
- x = r.x;
- y = r.y;
- width = r.width;
- height = r.height;
- }
- operator Rect16() {
- return Rect16((int16)x, (int16)y, (int16)width, (int16)height);
- }
-
- // Rect32 operators
-
- friend int operator==(Rect32 a, Rect32 b) {
- return a.x == b.x
- && a.y == b.y
- && a.width == b.width
- && a.height == b.height;
- }
-
- friend int operator!=(Rect32 a, Rect32 b) {
- return a.x != b.x
- || a.y != b.y
- || a.width != b.width
- || a.height != b.height;
- }
-
- friend Rect32 operator+ (Rect32 a, Point16 b) {
- return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
- }
-
- friend Rect32 operator+ (Point16 b, Rect32 a) {
- return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
- }
-
- friend Rect32 operator- (Rect32 a, Point16 b) {
- return Rect32(a.x - b.x, a.y - b.y, a.width, a.height);
- }
-
- friend Rect32 operator+ (Point32 b, Rect32 a) {
- return Rect32(a.x + b.x, a.y + b.y, a.width, a.height);
- }
-
- friend Rect32 operator- (Rect32 a, Point32 b) {
- return Rect32(a.x - b.x, a.y - b.y, a.width, a.height);
- }
-
- void operator+= (Point16 a) {
- x += a.x;
- y += a.y;
- }
- void operator-= (Point16 a) {
- x -= a.x;
- y -= a.y;
- }
-
- void operator+= (Point32 a) {
- x += a.x;
- y += a.y;
- }
- void operator-= (Point32 a) {
- x -= a.x;
- y -= a.y;
- }
- // functions
- void normalize(); // make rect right-side out
-
- void expand(int32 dx, int32 dy); // grow or shrink the rect
- void expand(int32 left, int32 top, int32 right, int32 bottom);
-
- int empty() const {
- return width <= 0 || height <= 0;
- }
-
- bool ptInside(int32 px, int32 py) const { // true if point inside
- return px >= x && px < (x + width) && py >= y && py < (y + height);
- }
- bool ptInside(const Point16 p) const {
- return ptInside(p.x, p.y);
- }
-
- bool overlap(const Rect32 &r) const;
-};
-
-// Intersect two rectangles
-
-Rect32 intersect(const Rect32, const Rect32);
-Rect32 bound(const Rect32, const Rect32);
-
} // end of namespace Saga2
#endif
Commit: 7a546462fd5eff0792a47bcf51b2235f11e8c591
https://github.com/scummvm/scummvm/commit/7a546462fd5eff0792a47bcf51b2235f11e8c591
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2025-08-31T21:43:20+02:00
Commit Message:
COMMON: Switch Common::Rect and Common::Point to templates
Also, ontroduced Common::Rect32 and Common::Point32.
And adds two new methods, setRect() and setEmpty()
It uses Curiously recurring template pattern.
See https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
Courtesy of lephilosouphe
Changed paths:
common/rect.h
diff --git a/common/rect.h b/common/rect.h
index e8a25892d09..3592dc5b2b4 100644
--- a/common/rect.h
+++ b/common/rect.h
@@ -42,53 +42,54 @@ namespace Common {
/**
* Simple class for handling both 2D position and size.
*/
-struct Point {
- int16 x; /*!< The horizontal position of the point. */
- int16 y; /*!< The vertical position of the point. */
+template<typename T, typename ConcretePoint>
+struct PointBase {
+ T x; /*!< The horizontal position of the point. */
+ T y; /*!< The vertical position of the point. */
- constexpr Point() : x(0), y(0) {}
+ constexpr PointBase() : x(0), y(0) {}
/**
* Create a point with position defined by @p x1 and @p y1.
*/
- constexpr Point(int16 x1, int16 y1) : x(x1), y(y1) {}
+ constexpr PointBase(T x1, T y1) : x(x1), y(y1) {}
/**
* Determine whether the position of two points is the same.
*/
- bool operator==(const Point &p) const { return x == p.x && y == p.y; }
+ bool operator==(const ConcretePoint &p) const { return x == p.x && y == p.y; }
/**
* Determine whether the position of two points is not the same.
*/
- bool operator!=(const Point &p) const { return x != p.x || y != p.y; }
+ bool operator!=(const ConcretePoint &p) const { return x != p.x || y != p.y; }
/**
* Create a point by adding the @p delta value to a point.
*/
- Point operator+(const Point &delta) const { return Point(x + delta.x, y + delta.y); }
+ ConcretePoint operator+(const ConcretePoint &delta) const { return ConcretePoint(x + delta.x, y + delta.y); }
/**
* Create a point by subtracting the @p delta value from a point.
*/
- Point operator-(const Point &delta) const { return Point(x - delta.x, y - delta.y); }
+ ConcretePoint operator-(const ConcretePoint &delta) const { return ConcretePoint(x - delta.x, y - delta.y); }
/**
* Create a point by dividing a point by the (int) @p divisor value.
*/
- Point operator/(int divisor) const { return Point(x / divisor, y / divisor); }
+ ConcretePoint operator/(int divisor) const { return ConcretePoint(x / divisor, y / divisor); }
/**
* Create a point by multiplying a point by the (int) @p multiplier value.
*/
- Point operator*(int multiplier) const { return Point(x * multiplier, y * multiplier); }
+ ConcretePoint operator*(int multiplier) const { return ConcretePoint(x * multiplier, y * multiplier); }
/**
* Create a point by dividing a point by the (double) @p divisor value.
*/
- Point operator/(double divisor) const { return Point((int16)(x / divisor), (int16)(y / divisor)); }
+ ConcretePoint operator/(double divisor) const { return ConcretePoint((int16)(x / divisor), (int16)(y / divisor)); }
/**
* Create a point by multiplying a point by the (double) @p multiplier value.
*/
- Point operator*(double multiplier) const { return Point((int16)(x * multiplier), (int16)(y * multiplier)); }
+ ConcretePoint operator*(double multiplier) const { return ConcretePoint((int16)(x * multiplier), (int16)(y * multiplier)); }
/**
* Change a point's position by adding @p delta to its x and y coordinates.
*/
- void operator+=(const Point &delta) {
+ void operator+=(const ConcretePoint &delta) {
x += delta.x;
y += delta.y;
}
@@ -96,7 +97,7 @@ struct Point {
/**
* Change a point's position by subtracting @p delta from its x and y arguments.
*/
- void operator-=(const Point &delta) {
+ void operator-=(const ConcretePoint &delta) {
x -= delta.x;
y -= delta.y;
}
@@ -107,7 +108,7 @@ struct Point {
* @param p The other point.
* @return The distance between this and @p p.
*/
- uint sqrDist(const Point &p) const {
+ uint sqrDist(const ConcretePoint &p) const {
int diffx = ABS(p.x - x);
if (diffx >= 0x1000)
return 0xFFFFFF;
@@ -120,6 +121,13 @@ struct Point {
}
};
+struct Point : public PointBase<int16, Point > {
+ using PointBase::PointBase;
+};
+struct Point32 : public PointBase<int32, Point32> {
+ using PointBase::PointBase;
+};
+
static inline Point operator*(int multiplier, const Point &p) { return Point(p.x * multiplier, p.y * multiplier); }
static inline Point operator*(double multiplier, const Point &p) { return Point((int16)(p.x * multiplier), (int16)(p.y * multiplier)); }
@@ -141,15 +149,16 @@ static inline Point operator*(double multiplier, const Point &p) { return Point(
*
* When writing code using our Rect class, always keep this principle in mind!
*/
-struct Rect {
- int16 top, left; /*!< The point at the top left of the rectangle (part of the Rect). */
- int16 bottom, right; /*!< The point at the bottom right of the rectangle (not part of the Rect). */
+template<typename T, typename ConcreteRect, typename ConcretePoint>
+struct RectBase {
+ T top, left; /*!< The point at the top left of the rectangle (part of the Rect). */
+ T bottom, right; /*!< The point at the bottom right of the rectangle (not part of the Rect). */
- constexpr Rect() : top(0), left(0), bottom(0), right(0) {}
+ constexpr RectBase() : top(0), left(0), bottom(0), right(0) {}
/**
* Create a rectangle with the top-left corner at position (0, 0) and the given width @p w and height @p h.
*/
- constexpr Rect(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
+ constexpr RectBase(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
/**
* Create a rectangle with the top-left corner at the position @p topLeft
* and the bottom-right corner at the position @p bottomRight.
@@ -157,14 +166,14 @@ struct Rect {
* The @p topLeft x value must be greater or equal @p bottomRight x and
* @p topLeft y must be greater or equal @p bottomRight y.
*/
- Rect(const Point &topLeft, const Point &bottomRight) : top(topLeft.y), left(topLeft.x), bottom(bottomRight.y), right(bottomRight.x) {
+ RectBase(const ConcretePoint &topLeft, const ConcretePoint &bottomRight) : top(topLeft.y), left(topLeft.x), bottom(bottomRight.y), right(bottomRight.x) {
assert(isValidRect());
}
/**
* Create a rectangle with the top-left corner at the position @p topLeft
* and the given width @p w and height @p h.
*/
- constexpr Rect(const Point &topLeft, int16 w, int16 h) : top(topLeft.y), left(topLeft.x), bottom(topLeft.y + h), right(topLeft.x + w) {
+ constexpr RectBase(const ConcretePoint &topLeft, int16 w, int16 h) : top(topLeft.y), left(topLeft.x), bottom(topLeft.y + h), right(topLeft.x + w) {
}
/**
* Create a rectangle with the top-left corner at the given position (x1, y1)
@@ -172,7 +181,7 @@ struct Rect {
*
* The @p x2 value must be greater or equal @p x1 and @p y2 must be greater or equal @p y1.
*/
- Rect(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
+ RectBase(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
assert(isValidRect());
}
/**
@@ -180,26 +189,36 @@ struct Rect {
*
* @return True if the rectangles are identical, false otherwise.
*/
- bool operator==(const Rect &rhs) const { return equals(rhs); }
+ bool operator==(const ConcreteRect &rhs) const { return equals(rhs); }
/**
* Check if two rectangles are different.
*
* @return True if the rectangles are different, false otherwise.
*/
- bool operator!=(const Rect &rhs) const { return !equals(rhs); }
+ bool operator!=(const ConcreteRect &rhs) const { return !equals(rhs); }
- Common::Point origin() const { return Common::Point(left, top); } /*!< Return the origin of a rectangle. */
- int16 width() const { return right - left; } /*!< Return the width of a rectangle. */
- int16 height() const { return bottom - top; } /*!< Return the height of a rectangle. */
+ ConcretePoint origin() const { return ConcretePoint(left, top); } /*!< Return the origin of a rectangle. */
+ T width() const { return right - left; } /*!< Return the width of a rectangle. */
+ T height() const { return bottom - top; } /*!< Return the height of a rectangle. */
- void setWidth(int16 aWidth) { /*!< Set the width to @p aWidth value. */
+ void setWidth(T aWidth) { /*!< Set the width to @p aWidth value. */
right = left + aWidth;
}
- void setHeight(int16 aHeight) { /*!< Set the height to @p aHeight value. */
+ void setHeight(T aHeight) { /*!< Set the height to @p aHeight value. */
bottom = top + aHeight;
}
+ /**
+ * Set the rectangle to a new position and size.
+ */
+ void setRect(T newLeft, T newTop, T newRight, T newBottom) {
+ left = newLeft;
+ top = newTop;
+ right = newRight;
+ bottom = newBottom;
+ }
+
/**
* Check if the given position is inside this rectangle.
*
@@ -208,7 +227,7 @@ struct Rect {
*
* @return True if the given position is inside this rectangle, false otherwise.
*/
- bool contains(int16 x, int16 y) const {
+ bool contains(T x, T y) const {
return (left <= x) && (x < right) && (top <= y) && (y < bottom);
}
@@ -219,7 +238,7 @@ struct Rect {
*
* @return True if the given point is inside this rectangle, false otherwise.
*/
- bool contains(const Point &p) const {
+ bool contains(const ConcretePoint &p) const {
return contains(p.x, p.y);
}
@@ -230,7 +249,7 @@ struct Rect {
*
* @return True if the given Rect is inside, false otherwise.
*/
- bool contains(const Rect &r) const {
+ bool contains(const ConcreteRect &r) const {
return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
}
@@ -241,7 +260,7 @@ struct Rect {
*
* @return true If the given Rect is equal, false otherwise.
*/
- bool equals(const Rect &r) const {
+ bool equals(const ConcreteRect &r) const {
return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
}
@@ -253,7 +272,7 @@ struct Rect {
* @return True if the given rectangle has a non-empty intersection with
* this rectangle, false otherwise.
*/
- bool intersects(const Rect &r) const {
+ bool intersects(const ConcreteRect &r) const {
return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
}
@@ -264,11 +283,11 @@ struct Rect {
*
* @return The intersection of the rectangles or an empty rectangle if not intersecting.
*/
- Rect findIntersectingRect(const Rect &r) const {
+ ConcreteRect findIntersectingRect(const ConcreteRect &r) const {
if (!intersects(r))
- return Rect();
+ return ConcreteRect();
- return Rect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
+ return ConcreteRect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
}
/**
@@ -276,7 +295,7 @@ struct Rect {
*
* @param r The rectangle to extend by.
*/
- void extend(const Rect &r) {
+ void extend(const ConcreteRect &r) {
left = MIN(left, r.left);
right = MAX(right, r.right);
top = MIN(top, r.top);
@@ -288,7 +307,7 @@ struct Rect {
*
* @param offset The size to grow by.
*/
- void grow(int16 offset) {
+ void grow(T offset) {
top -= offset;
left -= offset;
bottom += offset;
@@ -298,7 +317,7 @@ struct Rect {
/**
* Clip this rectangle to within the bounds of another rectangle @p r.
*/
- void clip(const Rect &r) {
+ void clip(const ConcreteRect &r) {
assert(isValidRect());
assert(r.isValidRect());
@@ -318,8 +337,15 @@ struct Rect {
/**
* Reduce the dimensions of this rectangle by setting max width and max height.
*/
- void clip(int16 maxw, int16 maxh) {
- clip(Rect(0, 0, maxw, maxh));
+ void clip(T maxw, T maxh) {
+ clip(ConcreteRect(0, 0, maxw, maxh));
+ }
+
+ /**
+ * Reset the rectangle to an empty state.
+ */
+ void setEmpty() {
+ left = right = top = bottom = 0;
}
/**
@@ -342,7 +368,7 @@ struct Rect {
/**
* Move this rectangle to the position defined by @p x, @p y.
*/
- void moveTo(int16 x, int16 y) {
+ void moveTo(T x, T y) {
bottom += y - top;
right += x - left;
top = y;
@@ -352,7 +378,7 @@ struct Rect {
/**
* Move the rectangle by the given delta x and y values.
*/
- void translate(int16 dx, int16 dy) {
+ void translate(T dx, T dy) {
left += dx; right += dx;
top += dy; bottom += dy;
}
@@ -360,7 +386,7 @@ struct Rect {
/**
* Move this rectangle to the position of the point @p p.
*/
- void moveTo(const Point &p) {
+ void moveTo(const ConcretePoint &p) {
moveTo(p.x, p.y);
}
@@ -369,7 +395,7 @@ struct Rect {
*
* @return True if the rectangle could be constrained
*/
- bool constrain(const Rect &o) {
+ bool constrain(const ConcreteRect &o) {
return o.constrain(left, top, width(), height());
}
@@ -378,7 +404,7 @@ struct Rect {
*
* @return True if the rectangle could be constrained
*/
- bool constrain(int16 &x, int16 &y, int16 w, int16 h) const {
+ bool constrain(T &x, T &y, T w, T h) const {
if (w > width() || h > height()) {
return false;
}
@@ -415,17 +441,17 @@ struct Rect {
* Create a rectangle around the given center.
* @note The center point is rounded up and left when given an odd width and height.
*/
- static Rect center(int16 cx, int16 cy, int16 w, int16 h) {
- int x = cx - w / 2, y = cy - h / 2;
- return Rect(x, y, x + w, y + h);
+ static ConcreteRect center(T cx, T cy, T w, T h) {
+ T x = cx - w / 2, y = cy - h / 2;
+ return ConcreteRect(x, y, x + w, y + h);
}
/**
* Return a Point indicating the centroid of the rectangle
* @note The center point is rounded up and left when width and/or height are odd
*/
- Point center() const {
- return Point((left + right) / 2, (bottom + top) / 2);
+ ConcretePoint center() const {
+ return ConcretePoint((left + right) / 2, (bottom + top) / 2);
}
/**
@@ -436,7 +462,7 @@ struct Rect {
* @param rect Blit source rectangle.
* @param clip Clip rectangle (size of destination surface).
*/
- static bool getBlitRect(Point &dst, Rect &rect, const Rect &clip) {
+ static bool getBlitRect(ConcretePoint &dst, ConcreteRect &rect, const ConcreteRect &clip) {
if (dst.x < clip.left) {
rect.left += clip.left - dst.x;
dst.x = clip.left;
@@ -458,6 +484,12 @@ struct Rect {
}
};
+struct Rect : public RectBase<int16, Rect, Point > {
+ using RectBase::RectBase;
+};
+struct Rect32 : public RectBase<int32, Rect32, Point32> {
+ using RectBase::RectBase;
+};
/** @} */
} // End of namespace Common
More information about the Scummvm-git-logs
mailing list