SDL_rect.h File Reference
`#include "SDL_stdinc.h"`
`#include "SDL_error.h"`
`#include "SDL_pixels.h"`
`#include "SDL_rwops.h"`
`#include "begin_code.h"`
`#include "close_code.h"`
Include dependency graph for SDL_rect.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | SDL_Point
| The structure that defines a point. More...
struct | SDL_Rect
| A rectangle, with the origin at the upper left. More...

## Functions

SDL_FORCE_INLINE SDL_bool | SDL_PointInRect (const SDL_Point *p, const SDL_Rect *r)
| Returns true if point resides inside a rectangle. More...
SDL_FORCE_INLINE SDL_bool | SDL_RectEmpty (const SDL_Rect *r)
| Returns true if the rectangle has no area. More...
SDL_FORCE_INLINE SDL_bool | SDL_RectEquals (const SDL_Rect *a, const SDL_Rect *b)
| Returns true if the two rectangles are equal. More...
SDL_bool | SDL_HasIntersection (const SDL_Rect *A, const SDL_Rect *B)
| Determine whether two rectangles intersect. More...
SDL_bool | SDL_IntersectRect (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
| Calculate the intersection of two rectangles. More...
void | SDL_UnionRect (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
| Calculate the union of two rectangles. More...
SDL_bool | SDL_EnclosePoints (const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result)
| Calculate a minimal rectangle enclosing a set of points. More...
SDL_bool | SDL_IntersectRectAndLine (const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
| Calculate the intersection of a rectangle and line segment. More...

## Detailed Description

Header file for SDL_rect definition and management functions.

Definition in file SDL_rect.h.

## Function Documentation

SDL_bool SDL_EnclosePoints | ( | const SDL_Point * | points,
|  | int | count,
|  | const SDL_Rect * | clip,
|  | SDL_Rect * | result
| ) |  |

Calculate a minimal rectangle enclosing a set of points.

Returns
  SDL_TRUE if any points were within the clipping rect

Definition at line 192 of file SDL_rect.c.

References SDL_Rect::h, i, NULL, SDL_FALSE, SDL_InvalidParamError, SDL_RectEmpty(), SDL_TRUE, SDL_Rect::w, SDL_Point::x, SDL_Rect::x, SDL_Point::y, and SDL_Rect::y.

Referenced by SDL_RectEquals().

{
int minx = 0;
int miny = 0;
int maxx = 0;
int maxy = 0;
int x, y, i;
if (!points) {
SDL_InvalidParamError("points");
return SDL_FALSE;
}
if (count < 1) {
SDL_InvalidParamError("count");
return SDL_FALSE;
}
if (clip) {
SDL_bool added = SDL_FALSE;
const int clip_minx = clip->x;
const int clip_miny = clip->y;
const int clip_maxx = clip->x+clip->w-1;
const int clip_maxy = clip->y+clip->h-1;
/* Special case for empty rectangle */
if (SDL_RectEmpty(clip)) {
return SDL_FALSE;
}
for (i = 0; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < clip_minx || x > clip_maxx ||
y < clip_miny || y > clip_maxy) {
continue;
}
if (!added) {
/* Special case: if no result was requested, we are done */
if (result == NULL) {
return SDL_TRUE;
}
/* First point added */
minx = maxx = x;
miny = maxy = y;
added = SDL_TRUE;
continue;
}
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
if (!added) {
return SDL_FALSE;
}
} else {
/* Special case: if no result was requested, we are done */
if (result == NULL) {
return SDL_TRUE;
}
/* No clipping, always add the first point */
minx = maxx = points[0].x;
miny = maxy = points[0].y;
for (i = 1; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
}
if (result) {
result->x = minx;
result->y = miny;
result->w = (maxx-minx)+1;
result->h = (maxy-miny)+1;
}
return SDL_TRUE;
}
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1564
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDL_Point::x
int x
Definition: SDL_rect.h:50
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_Point::y
int y
Definition: SDL_rect.h:51
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
NULL
#define NULL
Definition: begin_code.h:143
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

SDL_bool SDL_HasIntersection | ( | const SDL_Rect * | A,
|  | const SDL_Rect * | B
| ) |  |

Determine whether two rectangles intersect.

Returns
  SDL_TRUE if there is an intersection, SDL_FALSE otherwise.

Definition at line 28 of file SDL_rect.c.

References SDL_Rect::h, SDL_FALSE, SDL_InvalidParamError, SDL_RectEmpty(), SDL_TRUE, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_RectEquals().

{
int Amin, Amax, Bmin, Bmax;
if (!A) {
SDL_InvalidParamError("A");
return SDL_FALSE;
}
if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE;
}
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
return SDL_FALSE;
}
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
return SDL_TRUE;
}
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

SDL_bool SDL_IntersectRect | ( | const SDL_Rect * | A,
|  | const SDL_Rect * | B,
|  | SDL_Rect * | result
| ) |  |

Calculate the intersection of two rectangles.

Returns
  SDL_TRUE if there is an intersection, SDL_FALSE otherwise.

Definition at line 75 of file SDL_rect.c.

References SDL_Rect::h, SDL_FALSE, SDL_InvalidParamError, SDL_RectEmpty(), SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_RectEquals().

{
int Amin, Amax, Bmin, Bmax;
if (!A) {
SDL_InvalidParamError("A");
return SDL_FALSE;
}
if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE;
}
if (!result) {
SDL_InvalidParamError("result");
return SDL_FALSE;
}
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
result->w = 0;
result->h = 0;
return SDL_FALSE;
}
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->h = Amax - Amin;
return !SDL_RectEmpty(result);
}
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

SDL_bool SDL_IntersectRectAndLine | ( | const SDL_Rect * | rect,
|  | int * | X1,
|  | int * | Y1,
|  | int * | X2,
|  | int * | Y2
| ) |  |

Calculate the intersection of a rectangle and line segment.

Returns
  SDL_TRUE if there is an intersection, SDL_FALSE otherwise.

Definition at line 317 of file SDL_rect.c.

References CODE_BOTTOM, CODE_LEFT, CODE_RIGHT, CODE_TOP, ComputeOutCode(), SDL_Rect::h, SDL_FALSE, SDL_InvalidParamError, SDL_RectEmpty(), SDL_TRUE, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_RectEquals().

{
int x = 0;
int y = 0;
int x1, y1;
int x2, y2;
int rectx1;
int recty1;
int rectx2;
int recty2;
int outcode1, outcode2;
if (!rect) {
SDL_InvalidParamError("rect");
return SDL_FALSE;
}
if (!X1) {
SDL_InvalidParamError("X1");
return SDL_FALSE;
}
if (!Y1) {
SDL_InvalidParamError("Y1");
return SDL_FALSE;
}
if (!X2) {
SDL_InvalidParamError("X2");
return SDL_FALSE;
}
if (!Y2) {
SDL_InvalidParamError("Y2");
return SDL_FALSE;
}
/* Special case for empty rect */
if (SDL_RectEmpty(rect)) {
return SDL_FALSE;
}
x1 = *X1;
y1 = *Y1;
x2 = *X2;
y2 = *Y2;
rectx1 = rect->x;
recty1 = rect->y;
rectx2 = rect->x + rect->w - 1;
recty2 = rect->y + rect->h - 1;
/* Check to see if entire line is inside rect */
if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
return SDL_TRUE;
}
/* Check to see if entire line is to one side of rect */
if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
(y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
return SDL_FALSE;
}
if (y1 == y2) {
/* Horizontal line, easy to clip */
if (x1 < rectx1) {
*X1 = rectx1;
} else if (x1 > rectx2) {
*X1 = rectx2;
}
if (x2 < rectx1) {
*X2 = rectx1;
} else if (x2 > rectx2) {
*X2 = rectx2;
}
return SDL_TRUE;
}
if (x1 == x2) {
/* Vertical line, easy to clip */
if (y1 < recty1) {
*Y1 = recty1;
} else if (y1 > recty2) {
*Y1 = recty2;
}
if (y2 < recty1) {
*Y2 = recty1;
} else if (y2 > recty2) {
*Y2 = recty2;
}
return SDL_TRUE;
}
/* More complicated Cohen-Sutherland algorithm */
outcode1 = ComputeOutCode(rect, x1, y1);
outcode2 = ComputeOutCode(rect, x2, y2);
while (outcode1 || outcode2) {
if (outcode1 & outcode2) {
return SDL_FALSE;
}
if (outcode1) {
if (outcode1 & CODE_TOP) {
y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode1 & CODE_BOTTOM) {
y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode1 & CODE_LEFT) {
x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} else if (outcode1 & CODE_RIGHT) {
x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
}
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(rect, x, y);
} else {
if (outcode2 & CODE_TOP) {
y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode2 & CODE_BOTTOM) {
y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
} else if (outcode2 & CODE_LEFT) {
x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
} else if (outcode2 & CODE_RIGHT) {
x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
}
x2 = x;
y2 = y;
outcode2 = ComputeOutCode(rect, x, y);
}
}
*X1 = x1;
*Y1 = y1;
*X2 = x2;
*Y2 = y2;
return SDL_TRUE;
}
CODE_TOP
#define CODE_TOP
Definition: SDL_rect.c:295
x1
GLuint GLfloat GLfloat GLfloat x1
Definition: SDL_opengl_glext.h:8583
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
CODE_RIGHT
#define CODE_RIGHT
Definition: SDL_rect.c:297
y2
GLfixed GLfixed GLfixed y2
Definition: SDL_opengl_glext.h:4583
CODE_BOTTOM
#define CODE_BOTTOM
Definition: SDL_rect.c:294
ComputeOutCode
static int ComputeOutCode(const SDL_Rect *rect, int x, int y)
Definition: SDL_rect.c:300
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4583
y1
GLfixed y1
Definition: SDL_opengl_glext.h:4583
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
CODE_LEFT
#define CODE_LEFT
Definition: SDL_rect.c:296
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

SDL_FORCE_INLINE SDL_bool SDL_PointInRect | ( | const SDL_Point * | p,
|  | const SDL_Rect * | r
| ) |  |

Returns true if point resides inside a rectangle.

Definition at line 73 of file SDL_rect.h.

References SDL_Rect::h, SDL_FALSE, SDL_TRUE, SDL_Rect::w, SDL_Point::x, SDL_Rect::x, SDL_Point::y, and SDL_Rect::y.

Referenced by hitTest().

{
return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
(p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
}
SDL_Point::x
int x
Definition: SDL_rect.h:50
SDL_Point::y
int y
Definition: SDL_rect.h:51
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

SDL_FORCE_INLINE SDL_bool SDL_RectEmpty | ( | const SDL_Rect * | r | ) |

Returns true if the rectangle has no area.

Definition at line 82 of file SDL_rect.h.

References SDL_Rect::h, SDL_FALSE, SDL_TRUE, and SDL_Rect::w.

Referenced by rect_testIntersectRectEmpty(), rect_testRectEmpty(), rect_testRectEmptyParam(), SDL_EnclosePoints(), SDL_FillRect(), SDL_HasIntersection(), SDL_IntersectRect(), SDL_IntersectRectAndLine(), SDL_SoftBlit(), SDL_UnionRect(), and SDLTest_CommonEvent().

{
return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
}
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128

SDL_FORCE_INLINE SDL_bool SDL_RectEquals | ( | const SDL_Rect * | a,
|  | const SDL_Rect * | b
| ) |  |

Returns true if the two rectangles are equal.

Definition at line 90 of file SDL_rect.h.

References DECLSPEC, SDL_Rect::h, rect, SDL_EnclosePoints(), SDL_FALSE, SDL_HasIntersection(), SDL_IntersectRect(), SDL_IntersectRectAndLine(), SDL_TRUE, SDL_UnionRect(), SDLCALL, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by rect_testRectEquals(), and rect_testRectEqualsParam().

{
return (a && b && (a->x == b->x) && (a->y == b->y) &&
(a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
}
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Rect::y
int y
Definition: SDL_rect.h:66

void SDL_UnionRect | ( | const SDL_Rect * | A,
|  | const SDL_Rect * | B,
|  | SDL_Rect * | result
| ) |  |

Calculate the union of two rectangles.

Definition at line 129 of file SDL_rect.c.

References SDL_Rect::h, SDL_InvalidParamError, SDL_RectEmpty(), SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_RectEquals().

{
int Amin, Amax, Bmin, Bmax;
if (!A) {
SDL_InvalidParamError("A");
return;
}
if (!B) {
SDL_InvalidParamError("B");
return;
}
if (!result) {
SDL_InvalidParamError("result");
return;
}
/* Special cases for empty Rects */
if (SDL_RectEmpty(A)) {
if (SDL_RectEmpty(B)) {
/* A and B empty */
return;
} else {
/* A empty, B not empty */
*result = *B;
return;
}
} else {
if (SDL_RectEmpty(B)) {
/* A not empty, B empty */
*result = *A;
return;
}
}
/* Horizontal union */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin < Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical union */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin < Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->h = Amax - Amin;
}
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_Rect::y
int y
Definition: SDL_rect.h:66