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
46template<
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 Common::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)); }
145END_POINT_TYPE(int16,
Point)
168template<
typename T,
typename ConcreteRect,
typename ConcretePo
int>
185 RectBase(
const ConcretePoint &topLeft,
const ConcretePoint &bottomRight) : top(topLeft.y),
left(topLeft.x), bottom(bottomRight.y),
right(bottomRight.x) {
192 constexpr RectBase(
const ConcretePoint &topLeft, T w, T h) : top(topLeft.y),
left(topLeft.x), bottom(topLeft.y + h),
right(topLeft.x + w) {
216 ConcretePoint
origin()
const {
return ConcretePoint(
left, top); }
218 T
height()
const {
return bottom - top; }
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);
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();
317 top =
MIN(top, r.top);
318 bottom =
MAX(bottom, r.bottom);
330 bottom =
MAX(bottom, p.y);
348 void clip(
const ConcreteRect &r) {
350 assert(r.isValidRect());
352 if (top < r.top) top = r.top;
353 else if (top > r.bottom) top = r.bottom;
356 else if (
left > r.right)
left = r.right;
358 if (bottom > r.bottom) bottom = r.bottom;
359 else if (bottom < r.top) bottom = r.top;
369 clip(ConcreteRect(0, 0, maxw, maxh));
411 top += dy; bottom += dy;
442 }
else if (x >
right - w) {
447 }
else if (y > bottom - h) {
457 void debugPrint(
int debuglevel = 0,
const char *caption =
"Rect:")
const {
458 debug(debuglevel,
"%s %d, %d, %d, %d", caption,
left, top,
right, bottom);
464 void debugPrintC(
int debuglevel, uint32 debugChannel,
const char *caption =
"Rect:")
const {
465 debugC(debuglevel, debugChannel,
"%s %d, %d, %d, %d", caption,
left, top,
right, bottom);
479 static ConcreteRect
center(T cx, T cy, T w, T h) {
480 T x = cx - w / 2, y = cy - h / 2;
481 return ConcreteRect(x, y, x + w, y + h);
489 return ConcretePoint((
left +
right) / 2, (bottom + top) / 2);
500 static bool getBlitRect(ConcretePoint &dst, ConcreteRect &rect,
const ConcreteRect &
clip) {
501 if (dst.x <
clip.left) {
502 rect.left +=
clip.left - dst.x;
506 if (dst.y <
clip.top) {
507 rect.top +=
clip.top - dst.y;
511 int right = dst.x + rect.width();
515 int bottom = dst.y + rect.height();
516 if (bottom >
clip.bottom)
517 rect.bottom -= bottom -
clip.bottom;
518 return !rect.isEmpty();
525#define BEGIN_RECT_TYPE(T, Rect, Point) \
526 struct Rect : public Common::RectBase<T, Rect, Point> {
528#define END_RECT_TYPE(T, Rect, Point) \
529 constexpr Rect() : RectBase() {} \
530 constexpr Rect(T w, T h) : RectBase(w, h) {} \
531 Rect(const Point &topLeft, const Point &bottomRight) : RectBase(topLeft, bottomRight) {} \
532 constexpr Rect(const Point &topLeft, T w, T h) : RectBase(topLeft, w, h) {} \
533 Rect(T x1, T y1, T x2, T y2) : RectBase(x1, y1, x2, y2) {} \
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
void void void void void debugC(int level, uint32 debugChannel, MSVC_PRINTF const char *s,...) GCC_PRINTF(3
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
#define BEGIN_RECT_TYPE(T, Rect, Point)
Definition: rect.h:525
#define BEGIN_POINT_TYPE(T, Point)
Definition: rect.h:135
T ABS(T x)
Definition: util.h:60
T MIN(T a, T b)
Definition: util.h:63
T MAX(T a, T b)
Definition: util.h:66
Definition: algorithm.h:29
ConcretePoint operator*(double multiplier) const
Definition: rect.h:88
void operator-=(const ConcretePoint &delta)
Definition: rect.h:101
uint sqrDist(const ConcretePoint &p) const
Definition: rect.h:112
String toString() const
Definition: rect.h:127
ConcretePoint operator-(const ConcretePoint &delta) const
Definition: rect.h:72
ConcretePoint operator+(const ConcretePoint &delta) const
Definition: rect.h:68
ConcretePoint operator/(double divisor) const
Definition: rect.h:84
ConcretePoint operator/(int divisor) const
Definition: rect.h:76
bool operator==(const ConcretePoint &p) const
Definition: rect.h:60
ConcretePoint operator*(int multiplier) const
Definition: rect.h:80
constexpr PointBase(T x1, T y1)
Definition: rect.h:56
void operator+=(const ConcretePoint &delta)
Definition: rect.h:93
bool operator!=(const ConcretePoint &p) const
Definition: rect.h:64
bool constrain(T &x, T &y, T w, T h) const
Definition: rect.h:435
bool constrain(const ConcreteRect &o)
Definition: rect.h:426
bool equals(const ConcreteRect &r) const
Definition: rect.h:279
ConcreteRect findIntersectingRect(const ConcreteRect &r) const
Definition: rect.h:302
void setHeight(T aHeight)
Definition: rect.h:224
T right
Definition: rect.h:171
bool contains(const ConcretePoint &p) const
Definition: rect.h:257
constexpr RectBase(const ConcretePoint &topLeft, T w, T h)
Definition: rect.h:192
bool operator!=(const ConcreteRect &rhs) const
Definition: rect.h:214
bool contains(const ConcreteRect &r) const
Definition: rect.h:268
static ConcreteRect center(T cx, T cy, T w, T h)
Definition: rect.h:479
void moveTo(T x, T y)
Definition: rect.h:399
ConcretePoint origin() const
Definition: rect.h:216
void debugPrint(int debuglevel=0, const char *caption="Rect:") const
Definition: rect.h:457
bool intersects(const ConcreteRect &r) const
Definition: rect.h:291
void extend(const ConcreteRect &r)
Definition: rect.h:314
void translate(T dx, T dy)
Definition: rect.h:409
void debugPrintC(int debuglevel, uint32 debugChannel, const char *caption="Rect:") const
Definition: rect.h:464
bool operator==(const ConcreteRect &rhs) const
Definition: rect.h:208
bool isEmpty() const
Definition: rect.h:385
void moveTo(const ConcretePoint &p)
Definition: rect.h:417
void setEmpty()
Definition: rect.h:375
T height() const
Definition: rect.h:218
bool isValidRect() const
Definition: rect.h:392
constexpr RectBase(T w, T h)
Definition: rect.h:177
T left
Definition: rect.h:170
RectBase(const ConcretePoint &topLeft, const ConcretePoint &bottomRight)
Definition: rect.h:185
void grow(T offset)
Definition: rect.h:338
void clip(T maxw, T maxh)
Definition: rect.h:368
bool contains(T x, T y) const
Definition: rect.h:246
static bool getBlitRect(ConcretePoint &dst, ConcreteRect &rect, const ConcreteRect &clip)
Definition: rect.h:500
RectBase(T x1, T y1, T x2, T y2)
Definition: rect.h:200
void clip(const ConcreteRect &r)
Definition: rect.h:348
T width() const
Definition: rect.h:217
void setRect(T newLeft, T newTop, T newRight, T newBottom)
Definition: rect.h:231
void setWidth(T aWidth)
Definition: rect.h:220
String toString() const
Definition: rect.h:471
ConcretePoint center() const
Definition: rect.h:488
void extend(const ConcretePoint &p)
Definition: rect.h:326