SDL_Rect: minor code cleanups.

main
Ryan C. Gordon 2022-03-19 09:44:09 -04:00
parent c573ebe143
commit 4d9bef604a
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 57 additions and 93 deletions

View File

@ -31,16 +31,11 @@ SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
if (!A) { if (!A) {
SDL_InvalidParamError("A"); SDL_InvalidParamError("A");
return SDL_FALSE; return SDL_FALSE;
} } else if (!B) {
if (!B) {
SDL_InvalidParamError("B"); SDL_InvalidParamError("B");
return SDL_FALSE; return SDL_FALSE;
} } else if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
return SDL_FALSE; /* Special cases for empty rects */
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
return SDL_FALSE;
} }
/* Horizontal intersection */ /* Horizontal intersection */
@ -48,25 +43,29 @@ SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
Amax = Amin + A->w; Amax = Amin + A->w;
Bmin = B->x; Bmin = B->x;
Bmax = Bmin + B->w; Bmax = Bmin + B->w;
if (Bmin > Amin) if (Bmin > Amin) {
Amin = Bmin; Amin = Bmin;
if (Bmax < Amax) }
if (Bmax < Amax) {
Amax = Bmax; Amax = Bmax;
if (Amax <= Amin) }
if (Amax <= Amin) {
return SDL_FALSE; return SDL_FALSE;
}
/* Vertical intersection */ /* Vertical intersection */
Amin = A->y; Amin = A->y;
Amax = Amin + A->h; Amax = Amin + A->h;
Bmin = B->y; Bmin = B->y;
Bmax = Bmin + B->h; Bmax = Bmin + B->h;
if (Bmin > Amin) if (Bmin > Amin) {
Amin = Bmin; Amin = Bmin;
if (Bmax < Amax) }
if (Bmax < Amax) {
Amax = Bmax; Amax = Bmax;
if (Amax <= Amin) }
if (Amax <= Amin) {
return SDL_FALSE; return SDL_FALSE;
}
return SDL_TRUE; return SDL_TRUE;
} }
@ -78,20 +77,13 @@ SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
if (!A) { if (!A) {
SDL_InvalidParamError("A"); SDL_InvalidParamError("A");
return SDL_FALSE; return SDL_FALSE;
} } else if (!B) {
if (!B) {
SDL_InvalidParamError("B"); SDL_InvalidParamError("B");
return SDL_FALSE; return SDL_FALSE;
} } else if (!result) {
if (!result) {
SDL_InvalidParamError("result"); SDL_InvalidParamError("result");
return SDL_FALSE; return SDL_FALSE;
} } else if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) { /* Special cases for empty rects */
/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
result->w = 0; result->w = 0;
result->h = 0; result->h = 0;
return SDL_FALSE; return SDL_FALSE;
@ -102,11 +94,13 @@ SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
Amax = Amin + A->w; Amax = Amin + A->w;
Bmin = B->x; Bmin = B->x;
Bmax = Bmin + B->w; Bmax = Bmin + B->w;
if (Bmin > Amin) if (Bmin > Amin) {
Amin = Bmin; Amin = Bmin;
}
result->x = Amin; result->x = Amin;
if (Bmax < Amax) if (Bmax < Amax) {
Amax = Bmax; Amax = Bmax;
}
result->w = Amax - Amin; result->w = Amax - Amin;
/* Vertical intersection */ /* Vertical intersection */
@ -114,11 +108,13 @@ SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
Amax = Amin + A->h; Amax = Amin + A->h;
Bmin = B->y; Bmin = B->y;
Bmax = Bmin + B->h; Bmax = Bmin + B->h;
if (Bmin > Amin) if (Bmin > Amin) {
Amin = Bmin; Amin = Bmin;
}
result->y = Amin; result->y = Amin;
if (Bmax < Amax) if (Bmax < Amax) {
Amax = Bmax; Amax = Bmax;
}
result->h = Amax - Amin; result->h = Amax - Amin;
return !SDL_RectEmpty(result); return !SDL_RectEmpty(result);
@ -132,47 +128,36 @@ SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
if (!A) { if (!A) {
SDL_InvalidParamError("A"); SDL_InvalidParamError("A");
return; return;
} } else if (!B) {
if (!B) {
SDL_InvalidParamError("B"); SDL_InvalidParamError("B");
return; return;
} } else if (!result) {
if (!result) {
SDL_InvalidParamError("result"); SDL_InvalidParamError("result");
return; return;
} } else if (SDL_RectEmpty(A)) { /* Special cases for empty Rects */
if (SDL_RectEmpty(B)) { /* A and B empty */
/* Special cases for empty Rects */
if (SDL_RectEmpty(A)) {
if (SDL_RectEmpty(B)) {
/* A and B empty */
SDL_zerop(result); SDL_zerop(result);
return; } else { /* A empty, B not empty */
} else {
/* A empty, B not empty */
*result = *B; *result = *B;
return;
} }
} else { return;
if (SDL_RectEmpty(B)) { } else if (SDL_RectEmpty(B)) { /* A not empty, B empty */
/* A not empty, B empty */
*result = *A; *result = *A;
return; return;
} }
}
/* Horizontal union */ /* Horizontal union */
Amin = A->x; Amin = A->x;
Amax = Amin + A->w; Amax = Amin + A->w;
Bmin = B->x; Bmin = B->x;
Bmax = Bmin + B->w; Bmax = Bmin + B->w;
if (Bmin < Amin) if (Bmin < Amin) {
Amin = Bmin; Amin = Bmin;
}
result->x = Amin; result->x = Amin;
if (Bmax > Amax) if (Bmax > Amax) {
Amax = Bmax; Amax = Bmax;
}
result->w = Amax - Amin; result->w = Amax - Amin;
/* Vertical union */ /* Vertical union */
@ -180,11 +165,13 @@ SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
Amax = Amin + A->h; Amax = Amin + A->h;
Bmin = B->y; Bmin = B->y;
Bmax = Bmin + B->h; Bmax = Bmin + B->h;
if (Bmin < Amin) if (Bmin < Amin) {
Amin = Bmin; Amin = Bmin;
}
result->y = Amin; result->y = Amin;
if (Bmax > Amax) if (Bmax > Amax) {
Amax = Bmax; Amax = Bmax;
}
result->h = Amax - Amin; result->h = Amax - Amin;
} }
@ -201,9 +188,7 @@ SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
if (!points) { if (!points) {
SDL_InvalidParamError("points"); SDL_InvalidParamError("points");
return SDL_FALSE; return SDL_FALSE;
} } else if (count < 1) {
if (count < 1) {
SDL_InvalidParamError("count"); SDL_InvalidParamError("count");
return SDL_FALSE; return SDL_FALSE;
} }
@ -330,31 +315,20 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
if (!rect) { if (!rect) {
SDL_InvalidParamError("rect"); SDL_InvalidParamError("rect");
return SDL_FALSE; return SDL_FALSE;
} } else if (!X1) {
if (!X1) {
SDL_InvalidParamError("X1"); SDL_InvalidParamError("X1");
return SDL_FALSE; return SDL_FALSE;
} } else if (!Y1) {
if (!Y1) {
SDL_InvalidParamError("Y1"); SDL_InvalidParamError("Y1");
return SDL_FALSE; return SDL_FALSE;
} } else if (!X2) {
if (!X2) {
SDL_InvalidParamError("X2"); SDL_InvalidParamError("X2");
return SDL_FALSE; return SDL_FALSE;
} } else if (!Y2) {
if (!Y2) {
SDL_InvalidParamError("Y2"); SDL_InvalidParamError("Y2");
return SDL_FALSE; return SDL_FALSE;
} } else if (SDL_RectEmpty(rect)) {
return SDL_FALSE; /* Special case for empty rect */
/* Special case for empty rect */
if (SDL_RectEmpty(rect)) {
return SDL_FALSE;
} }
x1 = *X1; x1 = *X1;
@ -378,8 +352,7 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
return SDL_FALSE; return SDL_FALSE;
} }
if (y1 == y2) { if (y1 == y2) { /* Horizontal line, easy to clip */
/* Horizontal line, easy to clip */
if (x1 < rectx1) { if (x1 < rectx1) {
*X1 = rectx1; *X1 = rectx1;
} else if (x1 > rectx2) { } else if (x1 > rectx2) {
@ -393,8 +366,7 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
return SDL_TRUE; return SDL_TRUE;
} }
if (x1 == x2) { if (x1 == x2) { /* Vertical line, easy to clip */
/* Vertical line, easy to clip */
if (y1 < recty1) { if (y1 < recty1) {
*Y1 = recty1; *Y1 = recty1;
} else if (y1 > recty2) { } else if (y1 > recty2) {
@ -476,24 +448,16 @@ SDL_GetSpanEnclosingRect(int width, int height,
if (width < 1) { if (width < 1) {
SDL_InvalidParamError("width"); SDL_InvalidParamError("width");
return SDL_FALSE; return SDL_FALSE;
} } else if (height < 1) {
if (height < 1) {
SDL_InvalidParamError("height"); SDL_InvalidParamError("height");
return SDL_FALSE; return SDL_FALSE;
} } else if (!rects) {
if (!rects) {
SDL_InvalidParamError("rects"); SDL_InvalidParamError("rects");
return SDL_FALSE; return SDL_FALSE;
} } else if (!span) {
if (!span) {
SDL_InvalidParamError("span"); SDL_InvalidParamError("span");
return SDL_FALSE; return SDL_FALSE;
} } else if (numrects < 1) {
if (numrects < 1) {
SDL_InvalidParamError("numrects"); SDL_InvalidParamError("numrects");
return SDL_FALSE; return SDL_FALSE;
} }