roundedBoxRgba function
bool
roundedBoxRgba(
- Pointer<SdlRenderer> renderer,
- double x1,
- double y1,
- double x2,
- double y2,
- double rad,
- int r,
- int g,
- int b,
- int a,
)
Implementation
bool roundedBoxRgba(Pointer<SdlRenderer> renderer, double x1, double y1,
double x2, double y2, double rad, int r, int g, int b, int a) {
bool result = true;
double w, h, r2, tmp;
double cx = 0;
double cy = rad;
double ocx = 0xffff;
double ocy = 0xffff;
double df = 1 - rad;
double dE = 3;
double dSe = -2 * rad + 5;
double xpcx, xmcx, xpcy, xmcy;
double ypcy, ymcy, ypcx, ymcx;
double x, y, dx, dy;
/*
* Check destination renderer
*/
if (renderer == nullptr) {
return false;
}
/*
* Check radius vor valid range
*/
if (rad < 0) {
return false;
}
/*
* Special case - no rounding
*/
if (rad <= 1) {
return boxRgba(renderer, x1, y1, x2, y2, r, g, b, a);
}
/*
* Test for special cases of straight lines or single point
*/
if (x1 == x2) {
if (y1 == y2) {
return pixelRgba(renderer, x1, y1, r, g, b, a);
} else {
return vlineRgba(renderer, x1, y1, y2, r, g, b, a);
}
} else {
if (y1 == y2) {
return hlineRgba(renderer, x1, x2, y1, r, g, b, a);
}
}
/*
* Swap x1, x2 if required
*/
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
/*
* Swap y1, y2 if required
*/
if (y1 > y2) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
/*
* Calculate width&height
*/
w = x2 - x1 + 1;
h = y2 - y1 + 1;
/*
* Maybe adjust radius
*/
r2 = rad + rad;
if (r2 > w) {
rad = w / 2;
r2 = rad + rad;
}
if (r2 > h) {
rad = h / 2;
}
/* Setup filled circle drawing for corners */
x = x1 + rad;
y = y1 + rad;
dx = x2 - x1 - rad - rad;
dy = y2 - y1 - rad - rad;
/*
* Set color
*/
result = true;
if (result) {
result = sdlSetRenderDrawBlendMode(
renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
}
if (result) {
result = sdlSetRenderDrawColor(renderer, r, g, b, a);
}
/*
* Draw corners
*/
do {
xpcx = x + cx;
xmcx = x - cx;
xpcy = x + cy;
xmcy = x - cy;
if (ocy != cy) {
if (cy > 0) {
ypcy = y + cy;
ymcy = y - cy;
if (result) {
result = hline(renderer, xmcx, xpcx + dx, ypcy + dy);
}
if (result) {
result = hline(renderer, xmcx, xpcx + dx, ymcy);
}
} else {
if (result) {
result = hline(renderer, xmcx, xpcx + dx, y);
}
}
ocy = cy;
}
if (ocx != cx) {
if (cx != cy) {
if (cx > 0) {
ypcx = y + cx;
ymcx = y - cx;
if (result) {
result = hline(renderer, xmcy, xpcy + dx, ymcx);
}
if (result) {
result = hline(renderer, xmcy, xpcy + dx, ypcx + dy);
}
} else {
if (result) {
result = hline(renderer, xmcy, xpcy + dx, y);
}
}
}
ocx = cx;
}
/*
* Update
*/
if (df < 0) {
df += dE;
dE += 2;
dSe += 2;
} else {
df += dSe;
dE += 2;
dSe += 4;
cy--;
}
cx++;
} while (cx <= cy);
/* Inside */
if (dx > 0 && dy > 0) {
if (result) {
result = boxRgba(renderer, x1, y1 + rad + 1, x2, y2 - rad, r, g, b, a);
}
}
return (result);
}