roundedRectangleRgba function

bool roundedRectangleRgba(
  1. Pointer<SdlRenderer> renderer,
  2. double x1,
  3. double y1,
  4. double x2,
  5. double y2,
  6. double rad,
  7. int r,
  8. int g,
  9. int b,
  10. int a,
)

Implementation

bool roundedRectangleRgba(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 tmp;
  double w, h;
  double xx1, xx2;
  double yy1, yy2;

  /*
	* Check renderer
	*/
  if (renderer == nullptr) {
    return false;
  }

  /*
	* Check radius vor valid range
	*/
  if (rad < 0) {
    return false;
  }

  /*
	* Special case - no rounding
	*/
  if (rad <= 1) {
    return rectangleRgba(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;
  h = y2 - y1;

  /*
	* Maybe adjust radius
	*/
  if ((rad * 2) > w) {
    rad = w / 2;
  }
  if ((rad * 2) > h) {
    rad = h / 2;
  }

  /*
	* Draw corners
	*/
  xx1 = x1 + rad;
  xx2 = x2 - rad;
  yy1 = y1 + rad;
  yy2 = y2 - rad;
  if (result) {
    result = arcRgba(renderer, xx1, yy1, rad, 180, 270, r, g, b, a);
  }
  if (result) {
    result = arcRgba(renderer, xx2, yy1, rad, 270, 360, r, g, b, a);
  }
  if (result) {
    result = arcRgba(renderer, xx1, yy2, rad, 90, 180, r, g, b, a);
  }
  if (result) {
    result = arcRgba(renderer, xx2, yy2, rad, 0, 90, r, g, b, a);
  }

  /*
	* Draw lines
	*/
  if (xx1 <= xx2) {
    if (result) {
      result = hlineRgba(renderer, xx1, xx2, y1, r, g, b, a);
    }
    if (result) {
      result = hlineRgba(renderer, xx1, xx2, y2, r, g, b, a);
    }
  }
  if (yy1 <= yy2) {
    if (result) {
      result = vlineRgba(renderer, x1, yy1, yy2, r, g, b, a);
    }
    if (result) {
      result = vlineRgba(renderer, x2, yy1, yy2, r, g, b, a);
    }
  }
  return result;
}