summaryrefslogtreecommitdiffhomepage
path: root/src/shapes.c
diff options
context:
space:
mode:
authorDarkElvenAngel <[email protected]>2019-06-10 16:12:06 -0400
committerGitHub <[email protected]>2019-06-10 16:12:06 -0400
commitd7f4be071579e6f00974c0940f021272f22fbc54 (patch)
tree6ee389e6617c494d272e9bc82415fbc3618e7a28 /src/shapes.c
parent8a21830b77eaa76ffe0c31df5f96aecd6bd2eecc (diff)
parentbaf7d7d19ad8d6bfbfc201169e4ed4f49a9576a6 (diff)
downloadraylib-d7f4be071579e6f00974c0940f021272f22fbc54.tar.gz
raylib-d7f4be071579e6f00974c0940f021272f22fbc54.zip
Merge pull request #1 from raysan5/master
Update
Diffstat (limited to 'src/shapes.c')
-rw-r--r--src/shapes.c892
1 files changed, 821 insertions, 71 deletions
diff --git a/src/shapes.c b/src/shapes.c
index 8976c81c..64beb8ca 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -14,7 +14,7 @@
*
* LICENSE: zlib/libpng
*
-* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
+* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
@@ -132,7 +132,7 @@ void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
rlPushMatrix();
rlTranslatef((float)startPos.x, (float)startPos.y, 0.0f);
rlRotatef(RAD2DEG*angle, 0.0f, 0.0f, 1.0f);
- rlTranslatef(0, (thick > 1.0f) ? -thick/2.0f : -1.0f, 0.0f);
+ rlTranslatef(0, (thick > 1.0f)? -thick/2.0f : -1.0f, 0.0f);
rlBegin(RL_QUADS);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -143,7 +143,7 @@ void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
rlVertex2f(0.0f, thick);
-
+
rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
rlVertex2f(d, thick);
@@ -176,6 +176,25 @@ void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
}
}
+// Draw lines sequence
+void DrawLineStrip(Vector2 *points, int pointsCount, Color color)
+{
+ if (pointsCount >= 2)
+ {
+ if (rlCheckBufferLimit(pointsCount)) rlglDraw();
+
+ rlBegin(RL_LINES);
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ for (int i = 0; i < pointsCount - 1; i++)
+ {
+ rlVertex2f(points[i].x, points[i].y);
+ rlVertex2f(points[i + 1].x, points[i + 1].y);
+ }
+ rlEnd();
+ }
+}
+
// Draw a color-filled circle
void DrawCircle(int centerX, int centerY, float radius, Color color)
{
@@ -183,18 +202,42 @@ void DrawCircle(int centerX, int centerY, float radius, Color color)
}
// Draw a piece of a circle
-// TODO: Support better angle resolution (now limited to CIRCLE_SECTOR_LENGTH)
-void DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle, Color color)
+void DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle, int segments, Color color)
{
- #define CIRCLE_SECTOR_LENGTH 10
-
+ if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero
+
+ // Function expects (endAngle > startAngle)
+ if (endAngle < startAngle)
+ {
+ // Swap values
+ int tmp = startAngle;
+ startAngle = endAngle;
+ endAngle = tmp;
+ }
+
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #define CIRCLE_ERROR_RATE 0.5f
+
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/radius, 2) - 1);
+ segments = (endAngle - startAngle)*ceilf(2*PI/th)/360;
+
+ if (segments <= 0) segments = 4;
+ }
+
+ float stepLength = (float)(endAngle - startAngle)/(float)segments;
+ float angle = startAngle;
+
#if defined(SUPPORT_QUADS_DRAW_MODE)
- if (rlCheckBufferLimit(4*((360/CIRCLE_SECTOR_LENGTH)/2))) rlglDraw();
+ if (rlCheckBufferLimit(4*segments/2)) rlglDraw();
rlEnableTexture(GetShapesTexture().id);
rlBegin(RL_QUADS);
- for (int i = startAngle; i < endAngle; i += CIRCLE_SECTOR_LENGTH*2)
+ // NOTE: Every QUAD actually represents two segments
+ for (int i = 0; i < segments/2; i++)
{
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -202,33 +245,119 @@ void DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle
rlVertex2f(center.x, center.y);
rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
- rlVertex2f(center.x + sinf(DEG2RAD*i)*radius, center.y + cosf(DEG2RAD*i)*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
- rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
- rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH*2))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH*2))*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength*2))*radius, center.y + cosf(DEG2RAD*(angle + stepLength*2))*radius);
+
+ angle += (stepLength*2);
+ }
+
+ // NOTE: In case number of segments is odd, we add one last piece to the cake
+ if (segments%2)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x, center.y);
+
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x, center.y);
}
rlEnd();
rlDisableTexture();
#else
- if (rlCheckBufferLimit(3*((360/CIRCLE_SECTOR_LENGTH)/2))) rlglDraw();
+ if (rlCheckBufferLimit(3*segments)) rlglDraw();
rlBegin(RL_TRIANGLES);
- for (int i = startAngle; i < endAngle; i += CIRCLE_SECTOR_LENGTH)
+ for (int i = 0; i < segments; i++)
{
rlColor4ub(color.r, color.g, color.b, color.a);
rlVertex2f(center.x, center.y);
- rlVertex2f(center.x + sinf(DEG2RAD*i)*radius, center.y + cosf(DEG2RAD*i)*radius);
- rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+
+ angle += stepLength;
}
rlEnd();
#endif
}
+void DrawCircleSectorLines(Vector2 center, float radius, int startAngle, int endAngle, int segments, Color color)
+{
+ if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero issue
+
+ // Function expects (endAngle > startAngle)
+ if (endAngle < startAngle)
+ {
+ // Swap values
+ int tmp = startAngle;
+ startAngle = endAngle;
+ endAngle = tmp;
+ }
+
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #ifndef CIRCLE_ERROR_RATE
+ #define CIRCLE_ERROR_RATE 0.5f
+ #endif
+
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/radius, 2) - 1);
+ segments = (endAngle - startAngle)*ceilf(2*PI/th)/360;
+
+ if (segments <= 0) segments = 4;
+ }
+
+ float stepLength = (float)(endAngle - startAngle)/(float)segments;
+ float angle = startAngle;
+
+ // Hide the cap lines when the circle is full
+ bool showCapLines = true;
+ int limit = 2*(segments + 2);
+ if ((endAngle - startAngle)%360 == 0) { limit = 2*segments; showCapLines = false; }
+
+ if (rlCheckBufferLimit(limit)) rlglDraw();
+
+ rlBegin(RL_LINES);
+ if (showCapLines)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x, center.y);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ }
+
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+
+ angle += stepLength;
+ }
+
+ if (showCapLines)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x, center.y);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ }
+ rlEnd();
+}
+
// Draw a gradient-filled circle
// NOTE: Gradient goes from center (color1) to border (color2)
void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2)
@@ -239,7 +368,7 @@ void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Co
for (int i = 0; i < 360; i += 10)
{
rlColor4ub(color1.r, color1.g, color1.b, color1.a);
- rlVertex2i(centerX, centerY);
+ rlVertex2f(centerX, centerY);
rlColor4ub(color2.r, color2.g, color2.b, color2.a);
rlVertex2f(centerX + sinf(DEG2RAD*i)*radius, centerY + cosf(DEG2RAD*i)*radius);
rlColor4ub(color2.r, color2.g, color2.b, color2.a);
@@ -252,7 +381,7 @@ void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Co
// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw)
void DrawCircleV(Vector2 center, float radius, Color color)
{
- DrawCircleSector(center, radius, 0, 360, color);
+ DrawCircleSector(center, radius, 0, 360, 36, color);
}
// Draw circle outline
@@ -272,6 +401,184 @@ void DrawCircleLines(int centerX, int centerY, float radius, Color color)
rlEnd();
}
+void DrawRing(Vector2 center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color color)
+{
+ if (startAngle == endAngle) return;
+
+ // Function expects (outerRadius > innerRadius)
+ if (outerRadius < innerRadius)
+ {
+ float tmp = outerRadius;
+ outerRadius = innerRadius;
+ innerRadius = tmp;
+
+ if (outerRadius <= 0.0f) outerRadius = 0.1f;
+ }
+
+ // Function expects (endAngle > startAngle)
+ if (endAngle < startAngle)
+ {
+ // Swap values
+ int tmp = startAngle;
+ startAngle = endAngle;
+ endAngle = tmp;
+ }
+
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #ifndef CIRCLE_ERROR_RATE
+ #define CIRCLE_ERROR_RATE 0.5f
+ #endif
+
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
+ segments = (endAngle - startAngle)*ceilf(2*PI/th)/360;
+
+ if (segments <= 0) segments = 4;
+ }
+
+ // Not a ring
+ if (innerRadius <= 0.0f)
+ {
+ DrawCircleSector(center, outerRadius, startAngle, endAngle, segments, color);
+ return;
+ }
+
+ float stepLength = (float)(endAngle - startAngle)/(float)segments;
+ float angle = startAngle;
+
+#if defined(SUPPORT_QUADS_DRAW_MODE)
+ if (rlCheckBufferLimit(4*segments)) rlglDraw();
+
+ rlEnableTexture(GetShapesTexture().id);
+
+ rlBegin(RL_QUADS);
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+
+ angle += stepLength;
+ }
+ rlEnd();
+
+ rlDisableTexture();
+#else
+ if (rlCheckBufferLimit(6*segments)) rlglDraw();
+
+ rlBegin(RL_TRIANGLES);
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+
+ angle += stepLength;
+ }
+ rlEnd();
+#endif
+}
+
+void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color color)
+{
+ if (startAngle == endAngle) return;
+
+ // Function expects (outerRadius > innerRadius)
+ if (outerRadius < innerRadius)
+ {
+ float tmp = outerRadius;
+ outerRadius = innerRadius;
+ innerRadius = tmp;
+
+ if (outerRadius <= 0.0f) outerRadius = 0.1f;
+ }
+
+ // Function expects (endAngle > startAngle)
+ if (endAngle < startAngle)
+ {
+ // Swap values
+ int tmp = startAngle;
+ startAngle = endAngle;
+ endAngle = tmp;
+ }
+
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #ifndef CIRCLE_ERROR_RATE
+ #define CIRCLE_ERROR_RATE 0.5f
+ #endif
+
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
+ segments = (endAngle - startAngle)*ceilf(2*PI/th)/360;
+
+ if (segments <= 0) segments = 4;
+ }
+
+ if (innerRadius <= 0.0f)
+ {
+ DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, color);
+ return;
+ }
+
+ float stepLength = (float)(endAngle - startAngle)/(float)segments;
+ float angle = startAngle;
+
+ bool showCapLines = true;
+ int limit = 4*(segments + 1);
+ if ((endAngle - startAngle)%360 == 0) { limit = 4*segments; showCapLines = false; }
+
+ if (rlCheckBufferLimit(limit)) rlglDraw();
+
+ rlBegin(RL_LINES);
+ if (showCapLines)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ }
+
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+
+ angle += stepLength;
+ }
+
+ if (showCapLines)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ }
+ rlEnd();
+}
+
// Draw a color-filled rectangle
void DrawRectangle(int posX, int posY, int width, int height, Color color)
{
@@ -307,10 +614,10 @@ void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color
rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
rlVertex2f(0.0f, 0.0f);
-
+
rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
rlVertex2f(0.0f, rec.height);
-
+
rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
rlVertex2f(rec.width, rec.height);
@@ -400,7 +707,7 @@ void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color)
{
if (lineThick > rec.width || lineThick > rec.height)
{
- if(rec.width > rec.height) lineThick = (int)rec.height/2;
+ if (rec.width > rec.height) lineThick = (int)rec.height/2;
else if (rec.width < rec.height) lineThick = (int)rec.width/2;
}
@@ -410,6 +717,467 @@ void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color)
DrawRectangle( (int)rec.x, (int)(rec.y + lineThick), lineThick, (int)(rec.height - lineThick*2), color);
}
+// Draw rectangle with rounded edges
+void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color)
+{
+ // Not a rounded rectangle
+ if ((roundness <= 0.0f) || (rec.width < 1) || (rec.height < 1 ))
+ {
+ DrawRectangleRec(rec, color);
+ return;
+ }
+
+ if (roundness >= 1.0f) roundness = 1.0f;
+
+ // Calculate corner radius
+ float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
+ if (radius <= 0.0f) return;
+
+ // Calculate number of segments to use for the corners
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #ifndef CIRCLE_ERROR_RATE
+ #define CIRCLE_ERROR_RATE 0.5f
+ #endif
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/radius, 2) - 1);
+ segments = ceilf(2*PI/th)/4;
+ if (segments <= 0) segments = 4;
+ }
+
+ float stepLength = 90.0f/(float)segments;
+
+ /* Quick sketch to make sense of all of this (there are 9 parts to draw, also mark the 12 points we'll use below)
+ * Not my best attempt at ASCII art, just preted it's rounded rectangle :)
+ * P0 P1
+ * ____________________
+ * /| |\
+ * /1| 2 |3\
+ *P7 /__|____________________|__\ P2
+ * | |P8 P9| |
+ * | 8 | 9 | 4 |
+ * | __|____________________|__ |
+ *P6 \ |P11 P10| / P3
+ * \7| 6 |5/
+ * \|____________________|/
+ * P5 P4
+ */
+
+ const Vector2 point[12] = { // coordinates of the 12 points that define the rounded rect (the idea here is to make things easier)
+ {(float)rec.x + radius, rec.y}, {(float)(rec.x + rec.width) - radius, rec.y}, { rec.x + rec.width, (float)rec.y + radius }, // PO, P1, P2
+ {rec.x + rec.width, (float)(rec.y + rec.height) - radius}, {(float)(rec.x + rec.width) - radius, rec.y + rec.height}, // P3, P4
+ {(float)rec.x + radius, rec.y + rec.height}, { rec.x, (float)(rec.y + rec.height) - radius}, {rec.x, (float)rec.y + radius}, // P5, P6, P7
+ {(float)rec.x + radius, (float)rec.y + radius}, {(float)(rec.x + rec.width) - radius, (float)rec.y + radius}, // P8, P9
+ {(float)(rec.x + rec.width) - radius, (float)(rec.y + rec.height) - radius}, {(float)rec.x + radius, (float)(rec.y + rec.height) - radius} // P10, P11
+ };
+
+ const Vector2 centers[4] = { point[8], point[9], point[10], point[11] };
+ const float angles[4] = { 180.0f, 90.0f, 0.0f, 270.0f };
+
+#if defined(SUPPORT_QUADS_DRAW_MODE)
+ if (rlCheckBufferLimit(16*segments/2 + 5*4)) rlglDraw();
+
+ rlEnableTexture(GetShapesTexture().id);
+
+ rlBegin(RL_QUADS);
+ // Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
+ for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
+ {
+ float angle = angles[k];
+ const Vector2 center = centers[k];
+ // NOTE: Every QUAD actually represents two segments
+ for (int i = 0; i < segments/2; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x, center.y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength*2))*radius, center.y + cosf(DEG2RAD*(angle + stepLength*2))*radius);
+ angle += (stepLength*2);
+ }
+ // NOTE: In case number of segments is odd, we add one last piece to the cake
+ if (segments%2)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x, center.y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x, center.y);
+ }
+ }
+
+ // [2] Upper Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[0].x, point[0].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[8].x, point[8].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[9].x, point[9].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[1].x, point[1].y);
+
+ // [4] Right Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[2].x, point[2].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[9].x, point[9].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[10].x, point[10].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[3].x, point[3].y);
+
+ // [6] Bottom Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[11].x, point[11].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[5].x, point[5].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[4].x, point[4].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[10].x, point[10].y);
+
+ // [8] Left Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[7].x, point[7].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[6].x, point[6].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[11].x, point[11].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[8].x, point[8].y);
+
+ // [9] Middle Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[8].x, point[8].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[11].x, point[11].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[10].x, point[10].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[9].x, point[9].y);
+
+ rlEnd();
+ rlDisableTexture();
+#else
+ if (rlCheckBufferLimit(12*segments + 5*6)) rlglDraw(); // 4 corners with 3 vertices per segment + 5 rectangles with 6 vertices each
+
+ rlBegin(RL_TRIANGLES);
+ // Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
+ for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
+ {
+ float angle = angles[k];
+ const Vector2 center = centers[k];
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x, center.y);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*radius, center.y + cosf(DEG2RAD*angle)*radius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*radius, center.y + cosf(DEG2RAD*(angle + stepLength))*radius);
+ angle += stepLength;
+ }
+ }
+
+ // [2] Upper Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[0].x, point[0].y);
+ rlVertex2f(point[8].x, point[8].y);
+ rlVertex2f(point[9].x, point[9].y);
+ rlVertex2f(point[1].x, point[1].y);
+ rlVertex2f(point[0].x, point[0].y);
+ rlVertex2f(point[9].x, point[9].y);
+
+ // [4] Right Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[9].x, point[9].y);
+ rlVertex2f(point[10].x, point[10].y);
+ rlVertex2f(point[3].x, point[3].y);
+ rlVertex2f(point[2].x, point[2].y);
+ rlVertex2f(point[9].x, point[9].y);
+ rlVertex2f(point[3].x, point[3].y);
+
+ // [6] Bottom Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[11].x, point[11].y);
+ rlVertex2f(point[5].x, point[5].y);
+ rlVertex2f(point[4].x, point[4].y);
+ rlVertex2f(point[10].x, point[10].y);
+ rlVertex2f(point[11].x, point[11].y);
+ rlVertex2f(point[4].x, point[4].y);
+
+ // [8] Left Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[7].x, point[7].y);
+ rlVertex2f(point[6].x, point[6].y);
+ rlVertex2f(point[11].x, point[11].y);
+ rlVertex2f(point[8].x, point[8].y);
+ rlVertex2f(point[7].x, point[7].y);
+ rlVertex2f(point[11].x, point[11].y);
+
+ // [9] Middle Rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[8].x, point[8].y);
+ rlVertex2f(point[11].x, point[11].y);
+ rlVertex2f(point[10].x, point[10].y);
+ rlVertex2f(point[9].x, point[9].y);
+ rlVertex2f(point[8].x, point[8].y);
+ rlVertex2f(point[10].x, point[10].y);
+ rlEnd();
+#endif
+}
+
+// Draw rectangle with rounded edges outline
+void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, int lineThick, Color color)
+{
+ if (lineThick < 0) lineThick = 0;
+
+ // Not a rounded rectangle
+ if (roundness <= 0.0f)
+ {
+ DrawRectangleLinesEx((Rectangle){rec.x-lineThick, rec.y-lineThick, rec.width+2*lineThick, rec.height+2*lineThick}, lineThick, color);
+ return;
+ }
+
+ if (roundness >= 1.0f) roundness = 1.0f;
+
+ // Calculate corner radius
+ float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
+ if (radius <= 0.0f) return;
+
+ // Calculate number of segments to use for the corners
+ if (segments < 4)
+ {
+ // Calculate how many segments we need to draw a smooth circle, taken from https://stackoverflow.com/a/2244088
+ #ifndef CIRCLE_ERROR_RATE
+ #define CIRCLE_ERROR_RATE 0.5f
+ #endif
+ // Calculate the maximum angle between segments based on the error rate.
+ float th = acosf(2*powf(1 - CIRCLE_ERROR_RATE/radius, 2) - 1);
+ segments = ceilf(2*PI/th)/2;
+ if (segments <= 0) segments = 4;
+ }
+
+ float stepLength = 90.0f/(float)segments;
+ const float outerRadius = radius + (float)lineThick, innerRadius = radius;
+
+ /* Quick sketch to make sense of all of this (mark the 16 + 4(corner centers P16-19) points we'll use below)
+ * Not my best attempt at ASCII art, just preted it's rounded rectangle :)
+ * P0 P1
+ * ====================
+ * // P8 P9 \\
+ * // \\
+ *P7 // P15 P10 \\ P2
+ * || *P16 P17* ||
+ * || ||
+ * || P14 P11 ||
+ *P6 \\ *P19 P18* // P3
+ * \\ //
+ * \\ P13 P12 //
+ * ====================
+ * P5 P4
+ */
+ const Vector2 point[16] = {
+ {(float)rec.x + innerRadius, rec.y - lineThick}, {(float)(rec.x + rec.width) - innerRadius, rec.y - lineThick}, { rec.x + rec.width + lineThick, (float)rec.y + innerRadius }, // PO, P1, P2
+ {rec.x + rec.width + lineThick, (float)(rec.y + rec.height) - innerRadius}, {(float)(rec.x + rec.width) - innerRadius, rec.y + rec.height + lineThick}, // P3, P4
+ {(float)rec.x + innerRadius, rec.y + rec.height + lineThick}, { rec.x - lineThick, (float)(rec.y + rec.height) - innerRadius}, {rec.x - lineThick, (float)rec.y + innerRadius}, // P5, P6, P7
+ {(float)rec.x + innerRadius, rec.y}, {(float)(rec.x + rec.width) - innerRadius, rec.y}, // P8, P9
+ { rec.x + rec.width, (float)rec.y + innerRadius }, {rec.x + rec.width, (float)(rec.y + rec.height) - innerRadius}, // P10, P11
+ {(float)(rec.x + rec.width) - innerRadius, rec.y + rec.height}, {(float)rec.x + innerRadius, rec.y + rec.height}, // P12, P13
+ { rec.x, (float)(rec.y + rec.height) - innerRadius}, {rec.x, (float)rec.y + innerRadius} // P14, P15
+ };
+
+ const Vector2 centers[4] = {
+ {(float)rec.x + innerRadius, (float)rec.y + innerRadius}, {(float)(rec.x + rec.width) - innerRadius, (float)rec.y + innerRadius}, // P16, P17
+ {(float)(rec.x + rec.width) - innerRadius, (float)(rec.y + rec.height) - innerRadius}, {(float)rec.x + innerRadius, (float)(rec.y + rec.height) - innerRadius} // P18, P19
+ };
+
+ const float angles[4] = { 180.0f, 90.0f, 0.0f, 270.0f };
+
+ if (lineThick > 1)
+ {
+#if defined(SUPPORT_QUADS_DRAW_MODE)
+ if (rlCheckBufferLimit(4*4*segments + 4*4)) rlglDraw(); // 4 corners with 4 vertices for each segment + 4 rectangles with 4 vertices each
+
+ rlEnableTexture(GetShapesTexture().id);
+
+ rlBegin(RL_QUADS);
+ // Draw all of the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
+ for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
+ {
+ float angle = angles[k];
+ const Vector2 center = centers[k];
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+
+ angle += stepLength;
+ }
+ }
+
+ // Upper rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[0].x, point[0].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[8].x, point[8].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[9].x, point[9].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[1].x, point[1].y);
+
+ // Right rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[2].x, point[2].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[10].x, point[10].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[11].x, point[11].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[3].x, point[3].y);
+
+ // Lower rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[13].x, point[13].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[5].x, point[5].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[4].x, point[4].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[12].x, point[12].y);
+
+ // Left rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[15].x, point[15].y);
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[7].x, point[7].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(point[6].x, point[6].y);
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(point[14].x, point[14].y);
+
+ rlEnd();
+ rlDisableTexture();
+#else
+ if (rlCheckBufferLimit(4*6*segments + 4*6)) rlglDraw(); // 4 corners with 6(2*3) vertices for each segment + 4 rectangles with 6 vertices each
+
+ rlBegin(RL_TRIANGLES);
+
+ // Draw all of the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
+ for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
+ {
+ float angle = angles[k];
+ const Vector2 center = centers[k];
+
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*innerRadius, center.y + cosf(DEG2RAD*angle)*innerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*innerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+
+ angle += stepLength;
+ }
+ }
+
+ // Upper rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[0].x, point[0].y);
+ rlVertex2f(point[8].x, point[8].y);
+ rlVertex2f(point[9].x, point[9].y);
+ rlVertex2f(point[1].x, point[1].y);
+ rlVertex2f(point[0].x, point[0].y);
+ rlVertex2f(point[9].x, point[9].y);
+
+ // Right rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[10].x, point[10].y);
+ rlVertex2f(point[11].x, point[11].y);
+ rlVertex2f(point[3].x, point[3].y);
+ rlVertex2f(point[2].x, point[2].y);
+ rlVertex2f(point[10].x, point[10].y);
+ rlVertex2f(point[3].x, point[3].y);
+
+ // Lower rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[13].x, point[13].y);
+ rlVertex2f(point[5].x, point[5].y);
+ rlVertex2f(point[4].x, point[4].y);
+ rlVertex2f(point[12].x, point[12].y);
+ rlVertex2f(point[13].x, point[13].y);
+ rlVertex2f(point[4].x, point[4].y);
+
+ // Left rectangle
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[7].x, point[7].y);
+ rlVertex2f(point[6].x, point[6].y);
+ rlVertex2f(point[14].x, point[14].y);
+ rlVertex2f(point[15].x, point[15].y);
+ rlVertex2f(point[7].x, point[7].y);
+ rlVertex2f(point[14].x, point[14].y);
+ rlEnd();
+#endif
+ }
+ else
+ {
+ // Use LINES to draw the outline
+ if (rlCheckBufferLimit(8*segments + 4*2)) rlglDraw(); // 4 corners with 2 vertices for each segment + 4 rectangles with 2 vertices each
+
+ rlBegin(RL_LINES);
+
+ // Draw all of the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
+ for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
+ {
+ float angle = angles[k];
+ const Vector2 center = centers[k];
+
+ for (int i = 0; i < segments; i++)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(center.x + sinf(DEG2RAD*angle)*outerRadius, center.y + cosf(DEG2RAD*angle)*outerRadius);
+ rlVertex2f(center.x + sinf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + cosf(DEG2RAD*(angle + stepLength))*outerRadius);
+ angle += stepLength;
+ }
+ }
+ // And now the remaining 4 lines
+ for(int i = 0; i < 8; i += 2)
+ {
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlVertex2f(point[i].x, point[i].y);
+ rlVertex2f(point[i + 1].x, point[i + 1].y);
+ }
+ rlEnd();
+ }
+}
+
// Draw a triangle
void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
{
@@ -459,6 +1227,37 @@ void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
rlEnd();
}
+// Draw a triangle fan defined by points
+// NOTE: First point provided is shared by all triangles
+void DrawTriangleFan(Vector2 *points, int pointsCount, Color color)
+{
+ if (pointsCount >= 3)
+ {
+ if (rlCheckBufferLimit((pointsCount - 2)*4)) rlglDraw();
+
+ rlEnableTexture(GetShapesTexture().id);
+ rlBegin(RL_QUADS);
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
+ for (int i = 1; i < pointsCount - 1; i++)
+ {
+ rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(points[0].x, points[0].y);
+
+ rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(points[i].x, points[i].y);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
+ rlVertex2f(points[i + 1].x, points[i + 1].y);
+
+ rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
+ rlVertex2f(points[i + 1].x, points[i + 1].y);
+ }
+ rlEnd();
+ rlDisableTexture();
+ }
+}
+
// Draw a regular polygon of n sides (Vector version)
void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color)
{
@@ -507,55 +1306,6 @@ void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color col
rlPopMatrix();
}
-// Draw a closed polygon defined by points
-void DrawPolyEx(Vector2 *points, int pointsCount, Color color)
-{
- if (pointsCount >= 3)
- {
- if (rlCheckBufferLimit((pointsCount - 2)*4)) rlglDraw();
-
- rlEnableTexture(GetShapesTexture().id);
- rlBegin(RL_QUADS);
- rlColor4ub(color.r, color.g, color.b, color.a);
-
- for (int i = 1; i < pointsCount - 1; i++)
- {
- rlTexCoord2f(recTexShapes.x/texShapes.width, recTexShapes.y/texShapes.height);
- rlVertex2f(points[0].x, points[0].y);
-
- rlTexCoord2f(recTexShapes.x/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
- rlVertex2f(points[i].x, points[i].y);
-
- rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height);
- rlVertex2f(points[i + 1].x, points[i + 1].y);
-
- rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height);
- rlVertex2f(points[i + 1].x, points[i + 1].y);
- }
- rlEnd();
- rlDisableTexture();
- }
-}
-
-// Draw polygon using lines
-void DrawPolyExLines(Vector2 *points, int pointsCount, Color color)
-{
- if (pointsCount >= 2)
- {
- if (rlCheckBufferLimit(pointsCount)) rlglDraw();
-
- rlBegin(RL_LINES);
- rlColor4ub(color.r, color.g, color.b, color.a);
-
- for (int i = 0; i < pointsCount - 1; i++)
- {
- rlVertex2f(points[i].x, points[i].y);
- rlVertex2f(points[i + 1].x, points[i + 1].y);
- }
- rlEnd();
- }
-}
-
// Define default texture used to draw shapes
void SetShapesTexture(Texture2D texture, Rectangle source)
{
@@ -644,7 +1394,7 @@ bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
if (dy <= (rec.height/2.0f)) { return true; }
float cornerDistanceSq = (dx - rec.width/2.0f)*(dx - rec.width/2.0f) +
- (dy - rec.height/2.0f)*(dy - rec.height/2.0f);
+ (dy - rec.height/2.0f)*(dy - rec.height/2.0f);
return (cornerDistanceSq <= (radius*radius));
}