25 #include "common/scummsys.h" 26 #include "common/util.h" 27 #include "common/debug.h" 28 #include "common/str.h" 30 #define PRINT_RECT(x) (x).left,(x).top,(x).right,(x).bottom 46 template<
typename T,
typename ConcretePo
int>
60 bool operator==(
const ConcretePoint &p)
const {
return x == p.x && y == p.y; }
64 bool operator!=(
const ConcretePoint &p)
const {
return x != p.x || y != p.y; }
68 ConcretePoint
operator+(
const ConcretePoint &delta)
const {
return ConcretePoint(x + delta.x, y + delta.y); }
72 ConcretePoint
operator-(
const ConcretePoint &delta)
const {
return ConcretePoint(x - delta.x, y - delta.y); }
76 ConcretePoint
operator/(
int divisor)
const {
return ConcretePoint(x / divisor, y / divisor); }
80 ConcretePoint
operator*(
int multiplier)
const {
return ConcretePoint(x * multiplier, y * multiplier); }
84 ConcretePoint
operator/(
double divisor)
const {
return ConcretePoint((T)(x / divisor), (T)(y / divisor)); }
88 ConcretePoint
operator*(
double multiplier)
const {
return ConcretePoint((T)(x * multiplier), (T)(y * multiplier)); }
113 int diffx =
ABS(p.x - x);
117 int diffy =
ABS(p.y - y);
121 return uint(diffx * diffx + diffy * diffy);
135 #define BEGIN_POINT_TYPE(T, Point) \ 136 struct Point : public PointBase<T, Point> { 137 #define END_POINT_TYPE(T, Point) \ 138 constexpr Point() : PointBase() {} \ 139 constexpr Point(T x1, T y1) : PointBase(x1, y1) {} \ 141 static inline Point operator*(int multiplier, const Point &p) { return Point(p.x * multiplier, p.y * multiplier); } \ 142 static inline Point operator*(double multiplier, const Point &p) { return Point((T)(p.x * multiplier), (T)(p.y * multiplier)); } 145 END_POINT_TYPE(int16,
Point)
168 template<
typename T,
typename ConcreteRect,
typename ConcretePo
int>
173 constexpr
RectBase() : top(0), left(0), bottom(0), right(0) {}
177 constexpr
RectBase(T w, T h) : top(0), left(0), bottom(h), right(w) {}
185 RectBase(
const ConcretePoint &topLeft,
const ConcretePoint &bottomRight) : top(topLeft.
y), left(topLeft.
x), bottom(bottomRight.
y), right(bottomRight.
x) {
186 assert(isValidRect());
192 constexpr
RectBase(
const ConcretePoint &topLeft, T w, T h) : top(topLeft.
y), left(topLeft.
x), bottom(topLeft.
y + h), right(topLeft.
x + w) {
200 RectBase(T x1, T y1, T x2, T y2) : top(y1), left(x1), bottom(y2), right(x2) {
201 assert(isValidRect());
208 bool operator==(
const ConcreteRect &rhs)
const {
return equals(rhs); }
214 bool operator!=(
const ConcreteRect &rhs)
const {
return !equals(rhs); }
216 ConcretePoint
origin()
const {
return ConcretePoint(left, top); }
217 T
width()
const {
return right - left; }
218 T
height()
const {
return bottom - top; }
221 right = left + aWidth;
225 bottom = top + aHeight;
231 void setRect(T newLeft, T newTop, T newRight, T newBottom) {
247 return (left <= x) && (x < right) && (top <= y) && (y < bottom);
258 return contains(p.x, p.y);
269 return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
279 bool equals(
const ConcreteRect &r)
const {
280 return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
292 return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
304 return ConcreteRect();
306 return ConcreteRect(
MAX(r.left, left),
MAX(r.top, top),
MIN(r.right, right),
MIN(r.bottom, bottom));
315 left =
MIN(left, r.left);
316 right =
MAX(right, r.right);
317 top =
MIN(top, r.top);
318 bottom =
MAX(bottom, r.bottom);
336 void clip(
const ConcreteRect &r) {
337 assert(isValidRect());
338 assert(r.isValidRect());
340 if (top < r.top) top = r.top;
341 else if (top > r.bottom) top = r.bottom;
343 if (left < r.left) left = r.left;
344 else if (left > r.right) left = r.right;
346 if (bottom > r.bottom) bottom = r.bottom;
347 else if (bottom < r.top) bottom = r.top;
349 if (right > r.right) right = r.right;
350 else if (right < r.left) right = r.left;
357 clip(ConcreteRect(0, 0, maxw, maxh));
364 left = right = top = bottom = 0;
374 return (left >= right || top >= bottom);
381 return (left <= right && top <= bottom);
398 left += dx; right += dx;
399 top += dy; bottom += dy;
415 return o.constrain(left, top, width(), height());
424 if (w > width() || h > height()) {
430 }
else if (x > right - w) {
435 }
else if (y > bottom - h) {
445 void debugPrint(
int debuglevel = 0,
const char *caption =
"Rect:")
const {
446 debug(debuglevel,
"%s %d, %d, %d, %d", caption, left, top, right, bottom);
452 void debugPrintC(
int debuglevel, uint32 debugChannel,
const char *caption =
"Rect:")
const {
453 debugC(debuglevel, debugChannel,
"%s %d, %d, %d, %d", caption, left, top, right, bottom);
460 return String::format(
"%d, %d, %d, %d", left, top, right, bottom);
467 static ConcreteRect
center(T cx, T cy, T w, T h) {
468 T
x = cx - w / 2,
y = cy - h / 2;
469 return ConcreteRect(x,
y, x + w,
y + h);
477 return ConcretePoint((left + right) / 2, (bottom + top) / 2);
488 static bool getBlitRect(ConcretePoint &dst, ConcreteRect &rect,
const ConcreteRect &clip) {
489 if (dst.x < clip.left) {
490 rect.left += clip.left - dst.x;
494 if (dst.y < clip.top) {
495 rect.top += clip.top - dst.y;
499 int right = dst.x + rect.right;
500 if (right > clip.right)
501 rect.right -= right - clip.right;
503 int bottom = dst.y + rect.bottom;
504 if (bottom > clip.bottom)
505 rect.bottom -= bottom - clip.bottom;
506 return !rect.isEmpty();
513 #define BEGIN_RECT_TYPE(T, Rect, Point) \ 514 struct Rect : public RectBase<T, Rect, Point> { 516 #define END_RECT_TYPE(T, Rect, Point) \ 517 constexpr Rect() : RectBase() {} \ 518 constexpr Rect(T w, T h) : RectBase(w, h) {} \ 519 Rect(const Point &topLeft, const Point &bottomRight) : RectBase(topLeft, bottomRight) {} \ 520 constexpr Rect(const Point &topLeft, T w, T h) : RectBase(topLeft, w, h) {} \ 521 Rect(T x1, T y1, T x2, T y2) : RectBase(x1, y1, x2, y2) {} \ 527 END_RECT_TYPE(int32, Rect32,
Point32)
ConcretePoint operator*(int multiplier) const
Definition: rect.h:80
constexpr RectBase(const ConcretePoint &topLeft, T w, T h)
Definition: rect.h:192
void debugPrint(int debuglevel=0, const char *caption="Rect:") const
Definition: rect.h:445
ConcretePoint origin() const
Definition: rect.h:216
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
#define BEGIN_RECT_TYPE(T, Rect, Point)
Definition: rect.h:513
T left
Definition: rect.h:170
bool operator==(const ConcretePoint &p) const
Definition: rect.h:60
ConcretePoint center() const
Definition: rect.h:476
String toString() const
Definition: rect.h:459
void grow(T offset)
Definition: rect.h:326
void translate(T dx, T dy)
Definition: rect.h:397
ConcreteRect findIntersectingRect(const ConcreteRect &r) const
Definition: rect.h:302
static bool getBlitRect(ConcretePoint &dst, ConcreteRect &rect, const ConcreteRect &clip)
Definition: rect.h:488
void setHeight(T aHeight)
Definition: rect.h:224
void setWidth(T aWidth)
Definition: rect.h:220
bool intersects(const ConcreteRect &r) const
Definition: rect.h:291
T width() const
Definition: rect.h:217
void void void void void debugC(int level, uint32 debugChannel, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
void moveTo(const ConcretePoint &p)
Definition: rect.h:405
ConcretePoint operator/(int divisor) const
Definition: rect.h:76
void operator+=(const ConcretePoint &delta)
Definition: rect.h:93
bool operator==(const ConcreteRect &rhs) const
Definition: rect.h:208
constexpr PointBase(T x1, T y1)
Definition: rect.h:56
String toString() const
Definition: rect.h:127
RectBase(T x1, T y1, T x2, T y2)
Definition: rect.h:200
bool operator!=(const ConcreteRect &rhs) const
Definition: rect.h:214
T height() const
Definition: rect.h:218
ConcretePoint operator+(const ConcretePoint &delta) const
Definition: rect.h:68
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
T right
Definition: rect.h:171
void clip(const ConcreteRect &r)
Definition: rect.h:336
uint sqrDist(const ConcretePoint &p) const
Definition: rect.h:112
ConcretePoint operator-(const ConcretePoint &delta) const
Definition: rect.h:72
Definition: algorithm.h:29
bool isEmpty() const
Definition: rect.h:373
bool equals(const ConcreteRect &r) const
Definition: rect.h:279
void setRect(T newLeft, T newTop, T newRight, T newBottom)
Definition: rect.h:231
bool contains(const ConcreteRect &r) const
Definition: rect.h:268
bool contains(T x, T y) const
Definition: rect.h:246
#define BEGIN_POINT_TYPE(T, Point)
Definition: rect.h:135
RectBase(const ConcretePoint &topLeft, const ConcretePoint &bottomRight)
Definition: rect.h:185
ConcretePoint operator*(double multiplier) const
Definition: rect.h:88
bool contains(const ConcretePoint &p) const
Definition: rect.h:257
T MIN(T a, T b)
Definition: util.h:61
T MAX(T a, T b)
Definition: util.h:64
bool constrain(const ConcreteRect &o)
Definition: rect.h:414
void debugPrintC(int debuglevel, uint32 debugChannel, const char *caption="Rect:") const
Definition: rect.h:452
void extend(const ConcreteRect &r)
Definition: rect.h:314
T ABS(T x)
Definition: util.h:58
bool operator!=(const ConcretePoint &p) const
Definition: rect.h:64
bool constrain(T &x, T &y, T w, T h) const
Definition: rect.h:423
ConcretePoint operator/(double divisor) const
Definition: rect.h:84
constexpr RectBase(T w, T h)
Definition: rect.h:177
bool isValidRect() const
Definition: rect.h:380
static ConcreteRect center(T cx, T cy, T w, T h)
Definition: rect.h:467
void operator-=(const ConcretePoint &delta)
Definition: rect.h:101
void setEmpty()
Definition: rect.h:363
void moveTo(T x, T y)
Definition: rect.h:387
void clip(T maxw, T maxh)
Definition: rect.h:356