summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--BINDINGS.md2
-rw-r--r--src/raylib.h2
-rw-r--r--src/rlgl.h59
-rw-r--r--src/shapes.c74
4 files changed, 108 insertions, 29 deletions
diff --git a/BINDINGS.md b/BINDINGS.md
index dec13511..ac8276b9 100644
--- a/BINDINGS.md
+++ b/BINDINGS.md
@@ -23,6 +23,8 @@ Some people ported raylib to other languages in form of bindings or wrappers to
- *[raylib flat-assembler Usage example]()*
- *[raylib COBOL Usage example](https://github.com/Martinfx/Cobol/tree/master/OpenCobol/Games/raylib)*
+Missing some language? Check the [bindings not yet in this list](https://gist.github.com/raysan5/5764cc5b885183f523fce47f098f3d9b#bindings-not-yet-in-the-official-list) or create a new binding! :)
+
Usually, raylib bindings follow the convention: `raylib-{language}`
Let me know if you're writing a new binding for raylib, I will list it here and I usually
diff --git a/src/raylib.h b/src/raylib.h
index c72f0682..5db50c04 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -1043,7 +1043,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 DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle, int segments, 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/rlgl.h b/src/rlgl.h
index 00658fa1..d4faec18 100644
--- a/src/rlgl.h
+++ b/src/rlgl.h
@@ -728,6 +728,7 @@ typedef struct DynamicBuffer {
typedef struct DrawCall {
int mode; // Drawing mode: LINES, TRIANGLES, QUADS
int vertexCount; // Number of vertex of the draw
+ int vertexAlignment; // Number of vertex required for index alignment (LINES, TRIANGLES)
//unsigned int vaoId; // Vertex Array id to be used on the draw
//unsigned int shaderId; // Shader id to be used on the draw
unsigned int textureId; // Texture id to be used on the draw
@@ -1123,7 +1124,27 @@ void rlBegin(int mode)
// NOTE: In all three cases, vertex are accumulated over default internal vertex buffer
if (draws[drawsCounter - 1].mode != mode)
{
- if (draws[drawsCounter - 1].vertexCount > 0) drawsCounter++;
+ if (draws[drawsCounter - 1].vertexCount > 0)
+ {
+ // Make sure current draws[i].vertexCount is aligned a multiple of 4,
+ // that way, following QUADS drawing will keep aligned with index processing
+ // It implies adding some extra alignment vertex at the end of the draw,
+ // those vertex are not processed but they are considered as an additional offset
+ // for the next set of vertex to be drawn
+ if (draws[drawsCounter - 1].mode == RL_LINES) draws[drawsCounter - 1].vertexAlignment = ((draws[drawsCounter - 1].vertexCount < 4)? draws[drawsCounter - 1].vertexCount : draws[drawsCounter - 1].vertexCount%4);
+ else if (draws[drawsCounter - 1].mode == RL_TRIANGLES) draws[drawsCounter - 1].vertexAlignment = ((draws[drawsCounter - 1].vertexCount < 4)? 1 : (4 - (draws[drawsCounter - 1].vertexCount%4)));
+
+ if (rlCheckBufferLimit(draws[drawsCounter - 1].vertexAlignment)) rlglDraw();
+ else
+ {
+ vertexData[currentBuffer].vCounter += draws[drawsCounter - 1].vertexAlignment;
+ vertexData[currentBuffer].cCounter += draws[drawsCounter - 1].vertexAlignment;
+ vertexData[currentBuffer].tcCounter += draws[drawsCounter - 1].vertexAlignment;
+
+ drawsCounter++;
+ }
+ }
+
if (drawsCounter >= MAX_DRAWCALL_REGISTERED) rlglDraw();
draws[drawsCounter - 1].mode = mode;
@@ -1135,15 +1156,6 @@ void rlBegin(int mode)
// Finish vertex providing
void rlEnd(void)
{
- // Make sure current draws[i].vertexCount is multiple of 4, to align with index processing
- // NOTE: It implies adding some extra vertex at the end of the draw, those vertex will be
- // processed but are placed in a single point to not result in a fragment output...
- // TODO: System could be improved (a bit) just storing every draw alignment value
- // and adding it to vertexOffset on drawing... maybe in a future...
- int vertexCount = draws[drawsCounter - 1].vertexCount;
- int vertexToAlign = (vertexCount >= 4)? vertexCount%4 : (4 - vertexCount%4);
- for (int i = 0; i < vertexToAlign; i++) rlVertex3f(-1, -1, -1);
-
// Make sure vertexCount is the same for vertices, texcoords, colors and normals
// NOTE: In OpenGL 1.1, one glColor call can be made for all the subsequent glVertex calls
@@ -1283,7 +1295,27 @@ void rlEnableTexture(unsigned int id)
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (draws[drawsCounter - 1].textureId != id)
{
- if (draws[drawsCounter - 1].vertexCount > 0) drawsCounter++;
+ if (draws[drawsCounter - 1].vertexCount > 0)
+ {
+ // Make sure current draws[i].vertexCount is aligned a multiple of 4,
+ // that way, following QUADS drawing will keep aligned with index processing
+ // It implies adding some extra alignment vertex at the end of the draw,
+ // those vertex are not processed but they are considered as an additional offset
+ // for the next set of vertex to be drawn
+ if (draws[drawsCounter - 1].mode == RL_LINES) draws[drawsCounter - 1].vertexAlignment = ((draws[drawsCounter - 1].vertexCount < 4)? draws[drawsCounter - 1].vertexCount : draws[drawsCounter - 1].vertexCount%4);
+ else if (draws[drawsCounter - 1].mode == RL_TRIANGLES) draws[drawsCounter - 1].vertexAlignment = ((draws[drawsCounter - 1].vertexCount < 4)? 1 : (4 - (draws[drawsCounter - 1].vertexCount%4)));
+
+ if (rlCheckBufferLimit(draws[drawsCounter - 1].vertexAlignment)) rlglDraw();
+ else
+ {
+ vertexData[currentBuffer].vCounter += draws[drawsCounter - 1].vertexAlignment;
+ vertexData[currentBuffer].cCounter += draws[drawsCounter - 1].vertexAlignment;
+ vertexData[currentBuffer].tcCounter += draws[drawsCounter - 1].vertexAlignment;
+
+ drawsCounter++;
+ }
+ }
+
if (drawsCounter >= MAX_DRAWCALL_REGISTERED) rlglDraw();
draws[drawsCounter - 1].textureId = id;
@@ -1682,6 +1714,7 @@ void rlglInit(int width, int height)
{
draws[i].mode = RL_QUADS;
draws[i].vertexCount = 0;
+ draws[i].vertexAlignment = 0;
//draws[i].vaoId = 0;
//draws[i].shaderId = 0;
draws[i].textureId = defaultTextureId;
@@ -4190,8 +4223,8 @@ static void DrawBuffersDefault(void)
glDrawElements(GL_TRIANGLES, draws[i].vertexCount/4*6, GL_UNSIGNED_SHORT, (GLvoid *)(sizeof(GLushort)*vertexOffset/4*6));
#endif
}
-
- vertexOffset += draws[i].vertexCount;
+
+ vertexOffset += (draws[i].vertexCount + draws[i].vertexAlignment);
}
if (!vaoSupported)
diff --git a/src/shapes.c b/src/shapes.c
index fd28f3a3..5d93078b 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -183,18 +183,40 @@ 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
-
+ // 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,28 +224,50 @@ 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*(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*(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, 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
@@ -239,7 +283,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 +296,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