From 297dd641e8b80f962c3db05915b7033d5b2508c6 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 13 Feb 2019 00:06:06 +0100 Subject: [PATCH] ADDED: DrawCircleSector() --- src/raylib.h | 1 + src/shapes.c | 67 +++++++++++++++++++++++++++++----------------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index f09a2a24..17a6efc6 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1033,6 +1033,7 @@ RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line defining thickness RLAPI void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line using cubic-bezier curves in-out RLAPI void DrawCircle(int centerX, int centerY, float radius, Color color); // Draw a color-filled circle +RLAPI void DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle, Color color); // Draw a piece of a circle RLAPI void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2); // Draw a gradient-filled circle RLAPI void DrawCircleV(Vector2 center, float radius, Color color); // Draw a color-filled circle (Vector version) RLAPI void DrawCircleLines(int centerX, int centerY, float radius, Color color); // Draw circle outline diff --git a/src/shapes.c b/src/shapes.c index 83cdbe98..454e176a 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -182,36 +182,19 @@ void DrawCircle(int centerX, int centerY, float radius, Color color) DrawCircleV((Vector2){ (float)centerX, (float)centerY }, radius, color); } -// 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) -{ - if (rlCheckBufferLimit(3*36)) rlglDraw(); - - rlBegin(RL_TRIANGLES); - for (int i = 0; i < 360; i += 10) - { - rlColor4ub(color1.r, color1.g, color1.b, color1.a); - rlVertex2i(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); - rlVertex2f(centerX + sinf(DEG2RAD*(i + 10))*radius, centerY + cosf(DEG2RAD*(i + 10))*radius); - } - rlEnd(); -} - -// Draw a color-filled circle (Vector version) -// 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) +// 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) { + #define CIRCLE_SECTOR_LENGTH 10 + #if defined(SUPPORT_QUADS_DRAW_MODE) - if (rlCheckBufferLimit(4*(36/2))) rlglDraw(); + if (rlCheckBufferLimit(4*((360/CIRCLE_SECTOR_LENGTH)/2))) rlglDraw(); rlEnableTexture(GetShapesTexture().id); rlBegin(RL_QUADS); - for (int i = 0; i < 360; i += 20) + for (int i = startAngle; i < endAngle; i += CIRCLE_SECTOR_LENGTH*2) { rlColor4ub(color.r, color.g, color.b, color.a); @@ -222,30 +205,56 @@ void DrawCircleV(Vector2 center, float radius, Color color) rlVertex2f(center.x + sinf(DEG2RAD*i)*radius, center.y + cosf(DEG2RAD*i)*radius); rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, (recTexShapes.y + recTexShapes.height)/texShapes.height); - rlVertex2f(center.x + sinf(DEG2RAD*(i + 10))*radius, center.y + cosf(DEG2RAD*(i + 10))*radius); + rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius); rlTexCoord2f((recTexShapes.x + recTexShapes.width)/texShapes.width, recTexShapes.y/texShapes.height); - rlVertex2f(center.x + sinf(DEG2RAD*(i + 20))*radius, center.y + cosf(DEG2RAD*(i + 20))*radius); + rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH*2))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH*2))*radius); } rlEnd(); rlDisableTexture(); #else - if (rlCheckBufferLimit(3*(36/2))) rlglDraw(); + if (rlCheckBufferLimit(3*((360/CIRCLE_SECTOR_LENGTH)/2))) rlglDraw(); rlBegin(RL_TRIANGLES); - for (int i = 0; i < 360; i += 10) + for (int i = startAngle; i < endAngle; i += CIRCLE_SECTOR_LENGTH) { 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 + 10))*radius, center.y + cosf(DEG2RAD*(i + 10))*radius); + rlVertex2f(center.x + sinf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius, center.y + cosf(DEG2RAD*(i + CIRCLE_SECTOR_LENGTH))*radius); } rlEnd(); #endif } +// 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) +{ + if (rlCheckBufferLimit(3*36)) rlglDraw(); + + rlBegin(RL_TRIANGLES); + for (int i = 0; i < 360; i += 10) + { + rlColor4ub(color1.r, color1.g, color1.b, color1.a); + rlVertex2i(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); + rlVertex2f(centerX + sinf(DEG2RAD*(i + 10))*radius, centerY + cosf(DEG2RAD*(i + 10))*radius); + } + rlEnd(); +} + +// Draw a color-filled circle (Vector version) +// 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); +} + // Draw circle outline void DrawCircleLines(int centerX, int centerY, float radius, Color color) {