GCC Code Coverage Report


Directory: ./
File: submodules/raylib/src/rshapes.c
Date: 2023-09-29 04:53:15
Exec Total Coverage
Lines: 0 768 0.0%
Branches: 0 296 0.0%

Line Branch Exec Source
1 /**********************************************************************************************
2 *
3 * rshapes - Basic functions to draw 2d shapes and check collisions
4 *
5 * ADDITIONAL NOTES:
6 * Shapes can be draw using 3 types of primitives: LINES, TRIANGLES and QUADS.
7 * Some functions implement two drawing options: TRIANGLES and QUADS, by default TRIANGLES
8 * are used but QUADS implementation can be selected with SUPPORT_QUADS_DRAW_MODE define
9 *
10 * Some functions define texture coordinates (rlTexCoord2f()) for the shapes and use a
11 * user-provided texture with SetShapesTexture(), the pourpouse of this implementation
12 * is allowing to reduce draw calls when combined with a texture-atlas.
13 *
14 * By default, raylib sets the default texture and rectangle at InitWindow()[rcore] to one
15 * white character of default font [rtext], this way, raylib text and shapes can be draw with
16 * a single draw call and it also allows users to configure it the same way with their own fonts.
17 *
18 * CONFIGURATION:
19 * #define SUPPORT_MODULE_RSHAPES
20 * rshapes module is included in the build
21 *
22 * #define SUPPORT_QUADS_DRAW_MODE
23 * Use QUADS instead of TRIANGLES for drawing when possible. Lines-based shapes still use LINES
24 *
25 *
26 * LICENSE: zlib/libpng
27 *
28 * Copyright (c) 2013-2023 Ramon Santamaria (@raysan5)
29 *
30 * This software is provided "as-is", without any express or implied warranty. In no event
31 * will the authors be held liable for any damages arising from the use of this software.
32 *
33 * Permission is granted to anyone to use this software for any purpose, including commercial
34 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
35 *
36 * 1. The origin of this software must not be misrepresented; you must not claim that you
37 * wrote the original software. If you use this software in a product, an acknowledgment
38 * in the product documentation would be appreciated but is not required.
39 *
40 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
41 * as being the original software.
42 *
43 * 3. This notice may not be removed or altered from any source distribution.
44 *
45 **********************************************************************************************/
46
47 #include "raylib.h" // Declares module functions
48
49 // Check if config flags have been externally provided on compilation line
50 #if !defined(EXTERNAL_CONFIG_FLAGS)
51 #include "config.h" // Defines module configuration flags
52 #endif
53
54 #if defined(SUPPORT_MODULE_RSHAPES)
55
56 #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
57
58 #include <math.h> // Required for: sinf(), asinf(), cosf(), acosf(), sqrtf(), fabsf()
59 #include <float.h> // Required for: FLT_EPSILON
60 #include <stdlib.h> // Required for: RL_FREE
61
62 //----------------------------------------------------------------------------------
63 // Defines and Macros
64 //----------------------------------------------------------------------------------
65 // Error rate to calculate how many segments we need to draw a smooth circle,
66 // taken from https://stackoverflow.com/a/2244088
67 #ifndef SMOOTH_CIRCLE_ERROR_RATE
68 #define SMOOTH_CIRCLE_ERROR_RATE 0.5f // Circle error rate
69 #endif
70 #ifndef SPLINE_LINE_DIVISIONS
71 #define SPLINE_LINE_DIVISIONS 24 // Spline lines segment divisions
72 #endif
73
74
75 //----------------------------------------------------------------------------------
76 // Types and Structures Definition
77 //----------------------------------------------------------------------------------
78 // Not here...
79
80 //----------------------------------------------------------------------------------
81 // Global Variables Definition
82 //----------------------------------------------------------------------------------
83 Texture2D texShapes = { 1, 1, 1, 1, 7 }; // Texture used on shapes drawing (white pixel loaded by rlgl)
84 Rectangle texShapesRec = { 0.0f, 0.0f, 1.0f, 1.0f }; // Texture source rectangle used on shapes drawing
85
86 //----------------------------------------------------------------------------------
87 // Module specific Functions Declaration
88 //----------------------------------------------------------------------------------
89 static float EaseCubicInOut(float t, float b, float c, float d); // Cubic easing
90
91 //----------------------------------------------------------------------------------
92 // Module Functions Definition
93 //----------------------------------------------------------------------------------
94
95 // Set texture and rectangle to be used on shapes drawing
96 // NOTE: It can be useful when using basic shapes and one single font,
97 // defining a font char white rectangle would allow drawing everything in a single draw call
98 void SetShapesTexture(Texture2D texture, Rectangle source)
99 {
100 // Reset texture to default pixel if required
101 // WARNING: Shapes texture should be probably better validated,
102 // it can break the rendering of all shapes if misused
103 if ((texture.id == 0) || (source.width == 0) || (source.height == 0))
104 {
105 texShapes = (Texture2D){ 1, 1, 1, 1, 7 };
106 texShapesRec = (Rectangle){ 0.0f, 0.0f, 1.0f, 1.0f };
107 }
108 else
109 {
110 texShapes = texture;
111 texShapesRec = source;
112 }
113 }
114
115 // Draw a pixel
116 void DrawPixel(int posX, int posY, Color color)
117 {
118 DrawPixelV((Vector2){ (float)posX, (float)posY }, color);
119 }
120
121 // Draw a pixel (Vector version)
122 void DrawPixelV(Vector2 position, Color color)
123 {
124 #if defined(SUPPORT_QUADS_DRAW_MODE)
125 rlSetTexture(texShapes.id);
126
127 rlBegin(RL_QUADS);
128
129 rlNormal3f(0.0f, 0.0f, 1.0f);
130 rlColor4ub(color.r, color.g, color.b, color.a);
131
132 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
133 rlVertex2f(position.x, position.y);
134
135 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
136 rlVertex2f(position.x, position.y + 1);
137
138 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
139 rlVertex2f(position.x + 1, position.y + 1);
140
141 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
142 rlVertex2f(position.x + 1, position.y);
143
144 rlEnd();
145
146 rlSetTexture(0);
147 #else
148 rlBegin(RL_TRIANGLES);
149
150 rlColor4ub(color.r, color.g, color.b, color.a);
151
152 rlVertex2f(position.x, position.y);
153 rlVertex2f(position.x, position.y + 1);
154 rlVertex2f(position.x + 1, position.y);
155
156 rlVertex2f(position.x + 1, position.y);
157 rlVertex2f(position.x, position.y + 1);
158 rlVertex2f(position.x + 1, position.y + 1);
159
160 rlEnd();
161 #endif
162 }
163
164 // Draw a line
165 void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color)
166 {
167 rlBegin(RL_LINES);
168 rlColor4ub(color.r, color.g, color.b, color.a);
169 rlVertex2f((float)startPosX, (float)startPosY);
170 rlVertex2f((float)endPosX, (float)endPosY);
171 rlEnd();
172 }
173
174 // Draw a line (Vector version)
175 void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
176 {
177 rlBegin(RL_LINES);
178 rlColor4ub(color.r, color.g, color.b, color.a);
179 rlVertex2f(startPos.x, startPos.y);
180 rlVertex2f(endPos.x, endPos.y);
181 rlEnd();
182 }
183
184 // Draw a line defining thickness
185 void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
186 {
187 Vector2 delta = { endPos.x - startPos.x, endPos.y - startPos.y };
188 float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
189
190 if ((length > 0) && (thick > 0))
191 {
192 float scale = thick/(2*length);
193
194 Vector2 radius = { -scale*delta.y, scale*delta.x };
195 Vector2 strip[4] = {
196 { startPos.x - radius.x, startPos.y - radius.y },
197 { startPos.x + radius.x, startPos.y + radius.y },
198 { endPos.x - radius.x, endPos.y - radius.y },
199 { endPos.x + radius.x, endPos.y + radius.y }
200 };
201
202 DrawTriangleStrip(strip, 4, color);
203 }
204 }
205
206 // Draw line using cubic-bezier curves in-out
207 void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
208 {
209 Vector2 previous = startPos;
210 Vector2 current = { 0 };
211
212 Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
213
214 for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
215 {
216 // Cubic easing in-out
217 // NOTE: Easing is calculated only for y position value
218 current.y = EaseCubicInOut((float)i, startPos.y, endPos.y - startPos.y, (float)SPLINE_LINE_DIVISIONS);
219 current.x = previous.x + (endPos.x - startPos.x)/(float)SPLINE_LINE_DIVISIONS;
220
221 float dy = current.y - previous.y;
222 float dx = current.x - previous.x;
223 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
224
225 if (i == 1)
226 {
227 points[0].x = previous.x + dy*size;
228 points[0].y = previous.y - dx*size;
229 points[1].x = previous.x - dy*size;
230 points[1].y = previous.y + dx*size;
231 }
232
233 points[2*i + 1].x = current.x - dy*size;
234 points[2*i + 1].y = current.y + dx*size;
235 points[2*i].x = current.x + dy*size;
236 points[2*i].y = current.y - dx*size;
237
238 previous = current;
239 }
240
241 DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
242 }
243
244 // Draw line using quadratic bezier curves with a control point
245 void DrawLineBezierQuad(Vector2 startPos, Vector2 endPos, Vector2 controlPos, float thick, Color color)
246 {
247 const float step = 1.0f/SPLINE_LINE_DIVISIONS;
248
249 Vector2 previous = startPos;
250 Vector2 current = { 0 };
251 float t = 0.0f;
252
253 Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
254
255 for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
256 {
257 t = step*i;
258
259 float a = powf(1.0f - t, 2);
260 float b = 2.0f*(1.0f - t)*t;
261 float c = powf(t, 2);
262
263 // NOTE: The easing functions aren't suitable here because they don't take a control point
264 current.y = a*startPos.y + b*controlPos.y + c*endPos.y;
265 current.x = a*startPos.x + b*controlPos.x + c*endPos.x;
266
267 float dy = current.y - previous.y;
268 float dx = current.x - previous.x;
269 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
270
271 if (i == 1)
272 {
273 points[0].x = previous.x + dy*size;
274 points[0].y = previous.y - dx*size;
275 points[1].x = previous.x - dy*size;
276 points[1].y = previous.y + dx*size;
277 }
278
279 points[2*i + 1].x = current.x - dy*size;
280 points[2*i + 1].y = current.y + dx*size;
281 points[2*i].x = current.x + dy*size;
282 points[2*i].y = current.y - dx*size;
283
284 previous = current;
285 }
286
287 DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
288 }
289
290 // Draw line using cubic bezier curves with 2 control points
291 void DrawLineBezierCubic(Vector2 startPos, Vector2 endPos, Vector2 startControlPos, Vector2 endControlPos, float thick, Color color)
292 {
293 const float step = 1.0f/SPLINE_LINE_DIVISIONS;
294
295 Vector2 previous = startPos;
296 Vector2 current = { 0 };
297 float t = 0.0f;
298
299 Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
300
301 for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
302 {
303 t = step*i;
304
305 float a = powf(1.0f - t, 3);
306 float b = 3.0f*powf(1.0f - t, 2)*t;
307 float c = 3.0f*(1.0f - t)*powf(t, 2);
308 float d = powf(t, 3);
309
310 current.y = a*startPos.y + b*startControlPos.y + c*endControlPos.y + d*endPos.y;
311 current.x = a*startPos.x + b*startControlPos.x + c*endControlPos.x + d*endPos.x;
312
313 float dy = current.y - previous.y;
314 float dx = current.x - previous.x;
315 float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
316
317 if (i == 1)
318 {
319 points[0].x = previous.x + dy*size;
320 points[0].y = previous.y - dx*size;
321 points[1].x = previous.x - dy*size;
322 points[1].y = previous.y + dx*size;
323 }
324
325 points[2*i + 1].x = current.x - dy*size;
326 points[2*i + 1].y = current.y + dx*size;
327 points[2*i].x = current.x + dy*size;
328 points[2*i].y = current.y - dx*size;
329
330 previous = current;
331 }
332
333 DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
334 }
335
336 // Draw a B-Spline line, minimum 4 points
337 void DrawLineBSpline(Vector2 *points, int pointCount, float thick, Color color)
338 {
339 if (pointCount < 4) return;
340
341 float a[4] = { 0 };
342 float b[4] = { 0 };
343 float dy = 0.0f;
344 float dx = 0.0f;
345 float size = 0.0f;
346
347 Vector2 currentPoint = { 0 };
348 Vector2 nextPoint = { 0 };
349 Vector2 vertices[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
350
351 for (int i = 0; i < (pointCount - 3); i++)
352 {
353 float t = 0.0f;
354 Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
355
356 a[0] = (-p1.x + 3.0f*p2.x - 3.0f*p3.x + p4.x)/6.0f;
357 a[1] = (3.0f*p1.x - 6.0f*p2.x + 3.0f*p3.x)/6.0f;
358 a[2] = (-3.0f*p1.x + 3.0f*p3.x)/6.0f;
359 a[3] = (p1.x + 4.0f*p2.x + p3.x)/6.0f;
360
361 b[0] = (-p1.y + 3.0f*p2.y - 3.0f*p3.y + p4.y)/6.0f;
362 b[1] = (3.0f*p1.y - 6.0f*p2.y + 3.0f*p3.y)/6.0f;
363 b[2] = (-3.0f*p1.y + 3.0f*p3.y)/6.0f;
364 b[3] = (p1.y + 4.0f*p2.y + p3.y)/6.0f;
365
366 currentPoint.x = a[3];
367 currentPoint.y = b[3];
368
369 if (i == 0) DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
370
371 if (i > 0)
372 {
373 vertices[0].x = currentPoint.x + dy*size;
374 vertices[0].y = currentPoint.y - dx*size;
375 vertices[1].x = currentPoint.x - dy*size;
376 vertices[1].y = currentPoint.y + dx*size;
377 }
378
379 for (int j = 1; j <= SPLINE_LINE_DIVISIONS; j++)
380 {
381 t = ((float)j)/((float)SPLINE_LINE_DIVISIONS);
382
383 nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
384 nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
385
386 dy = nextPoint.y - currentPoint.y;
387 dx = nextPoint.x - currentPoint.x;
388 size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
389
390 if ((i == 0) && (j == 1))
391 {
392 vertices[0].x = currentPoint.x + dy*size;
393 vertices[0].y = currentPoint.y - dx*size;
394 vertices[1].x = currentPoint.x - dy*size;
395 vertices[1].y = currentPoint.y + dx*size;
396 }
397
398 vertices[2*j + 1].x = nextPoint.x - dy*size;
399 vertices[2*j + 1].y = nextPoint.y + dx*size;
400 vertices[2*j].x = nextPoint.x + dy*size;
401 vertices[2*j].y = nextPoint.y - dx*size;
402
403 currentPoint = nextPoint;
404 }
405
406 DrawTriangleStrip(vertices, 2*SPLINE_LINE_DIVISIONS + 2, color);
407 }
408
409 DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
410 }
411
412 // Draw a Catmull Rom spline line, minimum 4 points
413 void DrawLineCatmullRom(Vector2 *points, int pointCount, float thick, Color color)
414 {
415 if (pointCount < 4) return;
416
417 float dy = 0.0f;
418 float dx = 0.0f;
419 float size = 0.0f;
420
421 Vector2 currentPoint = points[1];
422 Vector2 nextPoint = { 0 };
423 Vector2 vertices[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
424
425 DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
426
427 for (int i = 0; i < (pointCount - 3); i++)
428 {
429 float t = 0.0f;
430 Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
431
432 if (i > 0)
433 {
434 vertices[0].x = currentPoint.x + dy*size;
435 vertices[0].y = currentPoint.y - dx*size;
436 vertices[1].x = currentPoint.x - dy*size;
437 vertices[1].y = currentPoint.y + dx*size;
438 }
439
440 for (int j = 1; j <= SPLINE_LINE_DIVISIONS; j++)
441 {
442 t = ((float)j)/((float)SPLINE_LINE_DIVISIONS);
443
444 float q0 = (-1.0f*t*t*t) + (2.0f*t*t) + (-1.0f*t);
445 float q1 = (3.0f*t*t*t) + (-5.0f*t*t) + 2.0f;
446 float q2 = (-3.0f*t*t*t) + (4.0f*t*t) + t;
447 float q3 = t*t*t - t*t;
448
449 nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
450 nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
451
452 dy = nextPoint.y - currentPoint.y;
453 dx = nextPoint.x - currentPoint.x;
454 size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
455
456 if ((i == 0) && (j == 1))
457 {
458 vertices[0].x = currentPoint.x + dy*size;
459 vertices[0].y = currentPoint.y - dx*size;
460 vertices[1].x = currentPoint.x - dy*size;
461 vertices[1].y = currentPoint.y + dx*size;
462 }
463
464 vertices[2*j + 1].x = nextPoint.x - dy*size;
465 vertices[2*j + 1].y = nextPoint.y + dx*size;
466 vertices[2*j].x = nextPoint.x + dy*size;
467 vertices[2*j].y = nextPoint.y - dx*size;
468
469 currentPoint = nextPoint;
470 }
471
472 DrawTriangleStrip(vertices, 2*SPLINE_LINE_DIVISIONS + 2, color);
473 }
474
475 DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
476 }
477
478 // Draw lines sequence
479 void DrawLineStrip(Vector2 *points, int pointCount, Color color)
480 {
481 if (pointCount >= 2)
482 {
483 rlBegin(RL_LINES);
484 rlColor4ub(color.r, color.g, color.b, color.a);
485
486 for (int i = 0; i < pointCount - 1; i++)
487 {
488 rlVertex2f(points[i].x, points[i].y);
489 rlVertex2f(points[i + 1].x, points[i + 1].y);
490 }
491 rlEnd();
492 }
493 }
494
495 // Draw a color-filled circle
496 void DrawCircle(int centerX, int centerY, float radius, Color color)
497 {
498 DrawCircleV((Vector2){ (float)centerX, (float)centerY }, radius, color);
499 }
500
501 // Draw a color-filled circle (Vector version)
502 // NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues
503 void DrawCircleV(Vector2 center, float radius, Color color)
504 {
505 DrawCircleSector(center, radius, 0, 360, 36, color);
506 }
507
508 // Draw a piece of a circle
509 void DrawCircleSector(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color)
510 {
511 if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero
512
513 // Function expects (endAngle > startAngle)
514 if (endAngle < startAngle)
515 {
516 // Swap values
517 float tmp = startAngle;
518 startAngle = endAngle;
519 endAngle = tmp;
520 }
521
522 int minSegments = (int)ceilf((endAngle - startAngle)/90);
523
524 if (segments < minSegments)
525 {
526 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
527 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
528 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
529
530 if (segments <= 0) segments = minSegments;
531 }
532
533 float stepLength = (endAngle - startAngle)/(float)segments;
534 float angle = startAngle;
535
536 #if defined(SUPPORT_QUADS_DRAW_MODE)
537 rlSetTexture(texShapes.id);
538
539 rlBegin(RL_QUADS);
540
541 // NOTE: Every QUAD actually represents two segments
542 for (int i = 0; i < segments/2; i++)
543 {
544 rlColor4ub(color.r, color.g, color.b, color.a);
545
546 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
547 rlVertex2f(center.x, center.y);
548
549 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
550 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength*2.0f))*radius, center.y + sinf(DEG2RAD*(angle + stepLength*2.0f))*radius);
551
552 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
553 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
554
555 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
556 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
557
558 angle += (stepLength*2.0f);
559 }
560
561 // NOTE: In case number of segments is odd, we add one last piece to the cake
562 if ((segments%2) == 1)
563 {
564 rlColor4ub(color.r, color.g, color.b, color.a);
565
566 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
567 rlVertex2f(center.x, center.y);
568
569 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
570 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
571
572 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
573 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
574
575 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
576 rlVertex2f(center.x, center.y);
577 }
578
579 rlEnd();
580
581 rlSetTexture(0);
582 #else
583 rlBegin(RL_TRIANGLES);
584 for (int i = 0; i < segments; i++)
585 {
586 rlColor4ub(color.r, color.g, color.b, color.a);
587
588 rlVertex2f(center.x, center.y);
589 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
590 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
591
592 angle += stepLength;
593 }
594 rlEnd();
595 #endif
596 }
597
598 // Draw a piece of a circle outlines
599 void DrawCircleSectorLines(Vector2 center, float radius, float startAngle, float endAngle, int segments, Color color)
600 {
601 if (radius <= 0.0f) radius = 0.1f; // Avoid div by zero issue
602
603 // Function expects (endAngle > startAngle)
604 if (endAngle < startAngle)
605 {
606 // Swap values
607 float tmp = startAngle;
608 startAngle = endAngle;
609 endAngle = tmp;
610 }
611
612 int minSegments = (int)ceilf((endAngle - startAngle)/90);
613
614 if (segments < minSegments)
615 {
616 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
617 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
618 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
619
620 if (segments <= 0) segments = minSegments;
621 }
622
623 float stepLength = (endAngle - startAngle)/(float)segments;
624 float angle = startAngle;
625 bool showCapLines = true;
626
627 rlBegin(RL_LINES);
628 if (showCapLines)
629 {
630 rlColor4ub(color.r, color.g, color.b, color.a);
631 rlVertex2f(center.x, center.y);
632 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
633 }
634
635 for (int i = 0; i < segments; i++)
636 {
637 rlColor4ub(color.r, color.g, color.b, color.a);
638
639 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
640 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
641
642 angle += stepLength;
643 }
644
645 if (showCapLines)
646 {
647 rlColor4ub(color.r, color.g, color.b, color.a);
648 rlVertex2f(center.x, center.y);
649 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
650 }
651 rlEnd();
652 }
653
654 // Draw a gradient-filled circle
655 // NOTE: Gradient goes from center (color1) to border (color2)
656 void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2)
657 {
658 rlBegin(RL_TRIANGLES);
659 for (int i = 0; i < 360; i += 10)
660 {
661 rlColor4ub(color1.r, color1.g, color1.b, color1.a);
662 rlVertex2f((float)centerX, (float)centerY);
663 rlColor4ub(color2.r, color2.g, color2.b, color2.a);
664 rlVertex2f((float)centerX + cosf(DEG2RAD*(i + 10))*radius, (float)centerY + sinf(DEG2RAD*(i + 10))*radius);
665 rlColor4ub(color2.r, color2.g, color2.b, color2.a);
666 rlVertex2f((float)centerX + cosf(DEG2RAD*i)*radius, (float)centerY + sinf(DEG2RAD*i)*radius);
667 }
668 rlEnd();
669 }
670
671 // Draw circle outline
672 void DrawCircleLines(int centerX, int centerY, float radius, Color color)
673 {
674 rlBegin(RL_LINES);
675 rlColor4ub(color.r, color.g, color.b, color.a);
676
677 // NOTE: Circle outline is drawn pixel by pixel every degree (0 to 360)
678 for (int i = 0; i < 360; i += 10)
679 {
680 rlVertex2f(centerX + cosf(DEG2RAD*i)*radius, centerY + sinf(DEG2RAD*i)*radius);
681 rlVertex2f(centerX + cosf(DEG2RAD*(i + 10))*radius, centerY + sinf(DEG2RAD*(i + 10))*radius);
682 }
683 rlEnd();
684 }
685
686 // Draw ellipse
687 void DrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color color)
688 {
689 rlBegin(RL_TRIANGLES);
690 for (int i = 0; i < 360; i += 10)
691 {
692 rlColor4ub(color.r, color.g, color.b, color.a);
693 rlVertex2f((float)centerX, (float)centerY);
694 rlVertex2f((float)centerX + cosf(DEG2RAD*(i + 10))*radiusH, (float)centerY + sinf(DEG2RAD*(i + 10))*radiusV);
695 rlVertex2f((float)centerX + cosf(DEG2RAD*i)*radiusH, (float)centerY + sinf(DEG2RAD*i)*radiusV);
696 }
697 rlEnd();
698 }
699
700 // Draw ellipse outline
701 void DrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color color)
702 {
703 rlBegin(RL_LINES);
704 for (int i = 0; i < 360; i += 10)
705 {
706 rlColor4ub(color.r, color.g, color.b, color.a);
707 rlVertex2f(centerX + cosf(DEG2RAD*(i + 10))*radiusH, centerY + sinf(DEG2RAD*(i + 10))*radiusV);
708 rlVertex2f(centerX + cosf(DEG2RAD*i)*radiusH, centerY + sinf(DEG2RAD*i)*radiusV);
709 }
710 rlEnd();
711 }
712
713 // Draw ring
714 void DrawRing(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color)
715 {
716 if (startAngle == endAngle) return;
717
718 // Function expects (outerRadius > innerRadius)
719 if (outerRadius < innerRadius)
720 {
721 float tmp = outerRadius;
722 outerRadius = innerRadius;
723 innerRadius = tmp;
724
725 if (outerRadius <= 0.0f) outerRadius = 0.1f;
726 }
727
728 // Function expects (endAngle > startAngle)
729 if (endAngle < startAngle)
730 {
731 // Swap values
732 float tmp = startAngle;
733 startAngle = endAngle;
734 endAngle = tmp;
735 }
736
737 int minSegments = (int)ceilf((endAngle - startAngle)/90);
738
739 if (segments < minSegments)
740 {
741 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
742 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
743 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
744
745 if (segments <= 0) segments = minSegments;
746 }
747
748 // Not a ring
749 if (innerRadius <= 0.0f)
750 {
751 DrawCircleSector(center, outerRadius, startAngle, endAngle, segments, color);
752 return;
753 }
754
755 float stepLength = (endAngle - startAngle)/(float)segments;
756 float angle = startAngle;
757
758 #if defined(SUPPORT_QUADS_DRAW_MODE)
759 rlSetTexture(texShapes.id);
760
761 rlBegin(RL_QUADS);
762 for (int i = 0; i < segments; i++)
763 {
764 rlColor4ub(color.r, color.g, color.b, color.a);
765
766 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
767 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
768
769 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
770 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
771
772 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
773 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
774
775 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
776 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
777
778 angle += stepLength;
779 }
780 rlEnd();
781
782 rlSetTexture(0);
783 #else
784 rlBegin(RL_TRIANGLES);
785 for (int i = 0; i < segments; i++)
786 {
787 rlColor4ub(color.r, color.g, color.b, color.a);
788
789 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
790 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
791 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
792
793 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
794 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
795 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
796
797 angle += stepLength;
798 }
799 rlEnd();
800 #endif
801 }
802
803 // Draw ring outline
804 void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, float startAngle, float endAngle, int segments, Color color)
805 {
806 if (startAngle == endAngle) return;
807
808 // Function expects (outerRadius > innerRadius)
809 if (outerRadius < innerRadius)
810 {
811 float tmp = outerRadius;
812 outerRadius = innerRadius;
813 innerRadius = tmp;
814
815 if (outerRadius <= 0.0f) outerRadius = 0.1f;
816 }
817
818 // Function expects (endAngle > startAngle)
819 if (endAngle < startAngle)
820 {
821 // Swap values
822 float tmp = startAngle;
823 startAngle = endAngle;
824 endAngle = tmp;
825 }
826
827 int minSegments = (int)ceilf((endAngle - startAngle)/90);
828
829 if (segments < minSegments)
830 {
831 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
832 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/outerRadius, 2) - 1);
833 segments = (int)((endAngle - startAngle)*ceilf(2*PI/th)/360);
834
835 if (segments <= 0) segments = minSegments;
836 }
837
838 if (innerRadius <= 0.0f)
839 {
840 DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, color);
841 return;
842 }
843
844 float stepLength = (endAngle - startAngle)/(float)segments;
845 float angle = startAngle;
846 bool showCapLines = true;
847
848 rlBegin(RL_LINES);
849 if (showCapLines)
850 {
851 rlColor4ub(color.r, color.g, color.b, color.a);
852 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
853 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
854 }
855
856 for (int i = 0; i < segments; i++)
857 {
858 rlColor4ub(color.r, color.g, color.b, color.a);
859
860 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
861 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
862
863 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
864 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
865
866 angle += stepLength;
867 }
868
869 if (showCapLines)
870 {
871 rlColor4ub(color.r, color.g, color.b, color.a);
872 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
873 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
874 }
875 rlEnd();
876 }
877
878 // Draw a color-filled rectangle
879 void DrawRectangle(int posX, int posY, int width, int height, Color color)
880 {
881 DrawRectangleV((Vector2){ (float)posX, (float)posY }, (Vector2){ (float)width, (float)height }, color);
882 }
883
884 // Draw a color-filled rectangle (Vector version)
885 // NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues
886 void DrawRectangleV(Vector2 position, Vector2 size, Color color)
887 {
888 DrawRectanglePro((Rectangle){ position.x, position.y, size.x, size.y }, (Vector2){ 0.0f, 0.0f }, 0.0f, color);
889 }
890
891 // Draw a color-filled rectangle
892 void DrawRectangleRec(Rectangle rec, Color color)
893 {
894 DrawRectanglePro(rec, (Vector2){ 0.0f, 0.0f }, 0.0f, color);
895 }
896
897 // Draw a color-filled rectangle with pro parameters
898 void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color)
899 {
900 Vector2 topLeft = { 0 };
901 Vector2 topRight = { 0 };
902 Vector2 bottomLeft = { 0 };
903 Vector2 bottomRight = { 0 };
904
905 // Only calculate rotation if needed
906 if (rotation == 0.0f)
907 {
908 float x = rec.x - origin.x;
909 float y = rec.y - origin.y;
910 topLeft = (Vector2){ x, y };
911 topRight = (Vector2){ x + rec.width, y };
912 bottomLeft = (Vector2){ x, y + rec.height };
913 bottomRight = (Vector2){ x + rec.width, y + rec.height };
914 }
915 else
916 {
917 float sinRotation = sinf(rotation*DEG2RAD);
918 float cosRotation = cosf(rotation*DEG2RAD);
919 float x = rec.x;
920 float y = rec.y;
921 float dx = -origin.x;
922 float dy = -origin.y;
923
924 topLeft.x = x + dx*cosRotation - dy*sinRotation;
925 topLeft.y = y + dx*sinRotation + dy*cosRotation;
926
927 topRight.x = x + (dx + rec.width)*cosRotation - dy*sinRotation;
928 topRight.y = y + (dx + rec.width)*sinRotation + dy*cosRotation;
929
930 bottomLeft.x = x + dx*cosRotation - (dy + rec.height)*sinRotation;
931 bottomLeft.y = y + dx*sinRotation + (dy + rec.height)*cosRotation;
932
933 bottomRight.x = x + (dx + rec.width)*cosRotation - (dy + rec.height)*sinRotation;
934 bottomRight.y = y + (dx + rec.width)*sinRotation + (dy + rec.height)*cosRotation;
935 }
936
937 #if defined(SUPPORT_QUADS_DRAW_MODE)
938 rlSetTexture(texShapes.id);
939
940 rlBegin(RL_QUADS);
941
942 rlNormal3f(0.0f, 0.0f, 1.0f);
943 rlColor4ub(color.r, color.g, color.b, color.a);
944
945 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
946 rlVertex2f(topLeft.x, topLeft.y);
947
948 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
949 rlVertex2f(bottomLeft.x, bottomLeft.y);
950
951 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
952 rlVertex2f(bottomRight.x, bottomRight.y);
953
954 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
955 rlVertex2f(topRight.x, topRight.y);
956
957 rlEnd();
958
959 rlSetTexture(0);
960 #else
961 rlBegin(RL_TRIANGLES);
962
963 rlColor4ub(color.r, color.g, color.b, color.a);
964
965 rlVertex2f(topLeft.x, topLeft.y);
966 rlVertex2f(bottomLeft.x, bottomLeft.y);
967 rlVertex2f(topRight.x, topRight.y);
968
969 rlVertex2f(topRight.x, topRight.y);
970 rlVertex2f(bottomLeft.x, bottomLeft.y);
971 rlVertex2f(bottomRight.x, bottomRight.y);
972
973 rlEnd();
974 #endif
975 }
976
977 // Draw a vertical-gradient-filled rectangle
978 // NOTE: Gradient goes from bottom (color1) to top (color2)
979 void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2)
980 {
981 DrawRectangleGradientEx((Rectangle){ (float)posX, (float)posY, (float)width, (float)height }, color1, color2, color2, color1);
982 }
983
984 // Draw a horizontal-gradient-filled rectangle
985 // NOTE: Gradient goes from bottom (color1) to top (color2)
986 void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2)
987 {
988 DrawRectangleGradientEx((Rectangle){ (float)posX, (float)posY, (float)width, (float)height }, color1, color1, color2, color2);
989 }
990
991 // Draw a gradient-filled rectangle
992 // NOTE: Colors refer to corners, starting at top-lef corner and counter-clockwise
993 void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4)
994 {
995 rlSetTexture(texShapes.id);
996
997 rlBegin(RL_QUADS);
998 rlNormal3f(0.0f, 0.0f, 1.0f);
999
1000 // NOTE: Default raylib font character 95 is a white square
1001 rlColor4ub(col1.r, col1.g, col1.b, col1.a);
1002 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1003 rlVertex2f(rec.x, rec.y);
1004
1005 rlColor4ub(col2.r, col2.g, col2.b, col2.a);
1006 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1007 rlVertex2f(rec.x, rec.y + rec.height);
1008
1009 rlColor4ub(col3.r, col3.g, col3.b, col3.a);
1010 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1011 rlVertex2f(rec.x + rec.width, rec.y + rec.height);
1012
1013 rlColor4ub(col4.r, col4.g, col4.b, col4.a);
1014 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1015 rlVertex2f(rec.x + rec.width, rec.y);
1016 rlEnd();
1017
1018 rlSetTexture(0);
1019 }
1020
1021 // Draw rectangle outline
1022 // NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues
1023 void DrawRectangleLines(int posX, int posY, int width, int height, Color color)
1024 {
1025 #if defined(SUPPORT_QUADS_DRAW_MODE)
1026 DrawRectangle(posX, posY, width, 1, color);
1027 DrawRectangle(posX + width - 1, posY + 1, 1, height - 2, color);
1028 DrawRectangle(posX, posY + height - 1, width, 1, color);
1029 DrawRectangle(posX, posY + 1, 1, height - 2, color);
1030 #else
1031 rlBegin(RL_LINES);
1032 rlColor4ub(color.r, color.g, color.b, color.a);
1033 rlVertex2f(posX + 1, posY + 1);
1034 rlVertex2f(posX + width, posY + 1);
1035
1036 rlVertex2f(posX + width, posY + 1);
1037 rlVertex2f(posX + width, posY + height);
1038
1039 rlVertex2f(posX + width, posY + height);
1040 rlVertex2f(posX + 1, posY + height);
1041
1042 rlVertex2f(posX + 1, posY + height);
1043 rlVertex2f(posX + 1, posY + 1);
1044 rlEnd();
1045 #endif
1046 }
1047
1048 // Draw rectangle outline with extended parameters
1049 void DrawRectangleLinesEx(Rectangle rec, float lineThick, Color color)
1050 {
1051 if ((lineThick > rec.width) || (lineThick > rec.height))
1052 {
1053 if (rec.width > rec.height) lineThick = rec.height/2;
1054 else if (rec.width < rec.height) lineThick = rec.width/2;
1055 }
1056
1057 // When rec = { x, y, 8.0f, 6.0f } and lineThick = 2, the following
1058 // four rectangles are drawn ([T]op, [B]ottom, [L]eft, [R]ight):
1059 //
1060 // TTTTTTTT
1061 // TTTTTTTT
1062 // LL RR
1063 // LL RR
1064 // BBBBBBBB
1065 // BBBBBBBB
1066 //
1067
1068 Rectangle top = { rec.x, rec.y, rec.width, lineThick };
1069 Rectangle bottom = { rec.x, rec.y - lineThick + rec.height, rec.width, lineThick };
1070 Rectangle left = { rec.x, rec.y + lineThick, lineThick, rec.height - lineThick*2.0f };
1071 Rectangle right = { rec.x - lineThick + rec.width, rec.y + lineThick, lineThick, rec.height - lineThick*2.0f };
1072
1073 DrawRectangleRec(top, color);
1074 DrawRectangleRec(bottom, color);
1075 DrawRectangleRec(left, color);
1076 DrawRectangleRec(right, color);
1077 }
1078
1079 // Draw rectangle with rounded edges
1080 void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color)
1081 {
1082 // Not a rounded rectangle
1083 if ((roundness <= 0.0f) || (rec.width < 1) || (rec.height < 1 ))
1084 {
1085 DrawRectangleRec(rec, color);
1086 return;
1087 }
1088
1089 if (roundness >= 1.0f) roundness = 1.0f;
1090
1091 // Calculate corner radius
1092 float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
1093 if (radius <= 0.0f) return;
1094
1095 // Calculate number of segments to use for the corners
1096 if (segments < 4)
1097 {
1098 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
1099 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
1100 segments = (int)(ceilf(2*PI/th)/4.0f);
1101 if (segments <= 0) segments = 4;
1102 }
1103
1104 float stepLength = 90.0f/(float)segments;
1105
1106 /*
1107 Quick sketch to make sense of all of this,
1108 there are 9 parts to draw, also mark the 12 points we'll use
1109
1110 P0____________________P1
1111 /| |\
1112 /1| 2 |3\
1113 P7 /__|____________________|__\ P2
1114 | |P8 P9| |
1115 | 8 | 9 | 4 |
1116 | __|____________________|__ |
1117 P6 \ |P11 P10| / P3
1118 \7| 6 |5/
1119 \|____________________|/
1120 P5 P4
1121 */
1122 // Coordinates of the 12 points that define the rounded rect
1123 const Vector2 point[12] = {
1124 {(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
1125 {rec.x + rec.width, (float)(rec.y + rec.height) - radius}, {(float)(rec.x + rec.width) - radius, rec.y + rec.height}, // P3, P4
1126 {(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
1127 {(float)rec.x + radius, (float)rec.y + radius}, {(float)(rec.x + rec.width) - radius, (float)rec.y + radius}, // P8, P9
1128 {(float)(rec.x + rec.width) - radius, (float)(rec.y + rec.height) - radius}, {(float)rec.x + radius, (float)(rec.y + rec.height) - radius} // P10, P11
1129 };
1130
1131 const Vector2 centers[4] = { point[8], point[9], point[10], point[11] };
1132 const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
1133
1134 #if defined(SUPPORT_QUADS_DRAW_MODE)
1135 rlSetTexture(texShapes.id);
1136
1137 rlBegin(RL_QUADS);
1138 // Draw all the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
1139 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1140 {
1141 float angle = angles[k];
1142 const Vector2 center = centers[k];
1143
1144 // NOTE: Every QUAD actually represents two segments
1145 for (int i = 0; i < segments/2; i++)
1146 {
1147 rlColor4ub(color.r, color.g, color.b, color.a);
1148 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1149 rlVertex2f(center.x, center.y);
1150
1151 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1152 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength*2))*radius, center.y + sinf(DEG2RAD*(angle + stepLength*2))*radius);
1153
1154 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1155 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1156
1157 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1158 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1159
1160 angle += (stepLength*2);
1161 }
1162
1163 // NOTE: In case number of segments is odd, we add one last piece to the cake
1164 if (segments%2)
1165 {
1166 rlColor4ub(color.r, color.g, color.b, color.a);
1167 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1168 rlVertex2f(center.x, center.y);
1169
1170 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1171 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1172
1173 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1174 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1175
1176 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1177 rlVertex2f(center.x, center.y);
1178 }
1179 }
1180
1181 // [2] Upper Rectangle
1182 rlColor4ub(color.r, color.g, color.b, color.a);
1183 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1184 rlVertex2f(point[0].x, point[0].y);
1185 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1186 rlVertex2f(point[8].x, point[8].y);
1187 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1188 rlVertex2f(point[9].x, point[9].y);
1189 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1190 rlVertex2f(point[1].x, point[1].y);
1191
1192 // [4] Right Rectangle
1193 rlColor4ub(color.r, color.g, color.b, color.a);
1194 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1195 rlVertex2f(point[2].x, point[2].y);
1196 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1197 rlVertex2f(point[9].x, point[9].y);
1198 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1199 rlVertex2f(point[10].x, point[10].y);
1200 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1201 rlVertex2f(point[3].x, point[3].y);
1202
1203 // [6] Bottom Rectangle
1204 rlColor4ub(color.r, color.g, color.b, color.a);
1205 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1206 rlVertex2f(point[11].x, point[11].y);
1207 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1208 rlVertex2f(point[5].x, point[5].y);
1209 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1210 rlVertex2f(point[4].x, point[4].y);
1211 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1212 rlVertex2f(point[10].x, point[10].y);
1213
1214 // [8] Left Rectangle
1215 rlColor4ub(color.r, color.g, color.b, color.a);
1216 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1217 rlVertex2f(point[7].x, point[7].y);
1218 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1219 rlVertex2f(point[6].x, point[6].y);
1220 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1221 rlVertex2f(point[11].x, point[11].y);
1222 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1223 rlVertex2f(point[8].x, point[8].y);
1224
1225 // [9] Middle Rectangle
1226 rlColor4ub(color.r, color.g, color.b, color.a);
1227 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1228 rlVertex2f(point[8].x, point[8].y);
1229 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1230 rlVertex2f(point[11].x, point[11].y);
1231 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1232 rlVertex2f(point[10].x, point[10].y);
1233 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1234 rlVertex2f(point[9].x, point[9].y);
1235
1236 rlEnd();
1237 rlSetTexture(0);
1238 #else
1239 rlBegin(RL_TRIANGLES);
1240
1241 // Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
1242 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1243 {
1244 float angle = angles[k];
1245 const Vector2 center = centers[k];
1246 for (int i = 0; i < segments; i++)
1247 {
1248 rlColor4ub(color.r, color.g, color.b, color.a);
1249 rlVertex2f(center.x, center.y);
1250 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*radius, center.y + sinf(DEG2RAD*(angle + stepLength))*radius);
1251 rlVertex2f(center.x + cosf(DEG2RAD*angle)*radius, center.y + sinf(DEG2RAD*angle)*radius);
1252 angle += stepLength;
1253 }
1254 }
1255
1256 // [2] Upper Rectangle
1257 rlColor4ub(color.r, color.g, color.b, color.a);
1258 rlVertex2f(point[0].x, point[0].y);
1259 rlVertex2f(point[8].x, point[8].y);
1260 rlVertex2f(point[9].x, point[9].y);
1261 rlVertex2f(point[1].x, point[1].y);
1262 rlVertex2f(point[0].x, point[0].y);
1263 rlVertex2f(point[9].x, point[9].y);
1264
1265 // [4] Right Rectangle
1266 rlColor4ub(color.r, color.g, color.b, color.a);
1267 rlVertex2f(point[9].x, point[9].y);
1268 rlVertex2f(point[10].x, point[10].y);
1269 rlVertex2f(point[3].x, point[3].y);
1270 rlVertex2f(point[2].x, point[2].y);
1271 rlVertex2f(point[9].x, point[9].y);
1272 rlVertex2f(point[3].x, point[3].y);
1273
1274 // [6] Bottom Rectangle
1275 rlColor4ub(color.r, color.g, color.b, color.a);
1276 rlVertex2f(point[11].x, point[11].y);
1277 rlVertex2f(point[5].x, point[5].y);
1278 rlVertex2f(point[4].x, point[4].y);
1279 rlVertex2f(point[10].x, point[10].y);
1280 rlVertex2f(point[11].x, point[11].y);
1281 rlVertex2f(point[4].x, point[4].y);
1282
1283 // [8] Left Rectangle
1284 rlColor4ub(color.r, color.g, color.b, color.a);
1285 rlVertex2f(point[7].x, point[7].y);
1286 rlVertex2f(point[6].x, point[6].y);
1287 rlVertex2f(point[11].x, point[11].y);
1288 rlVertex2f(point[8].x, point[8].y);
1289 rlVertex2f(point[7].x, point[7].y);
1290 rlVertex2f(point[11].x, point[11].y);
1291
1292 // [9] Middle Rectangle
1293 rlColor4ub(color.r, color.g, color.b, color.a);
1294 rlVertex2f(point[8].x, point[8].y);
1295 rlVertex2f(point[11].x, point[11].y);
1296 rlVertex2f(point[10].x, point[10].y);
1297 rlVertex2f(point[9].x, point[9].y);
1298 rlVertex2f(point[8].x, point[8].y);
1299 rlVertex2f(point[10].x, point[10].y);
1300 rlEnd();
1301 #endif
1302 }
1303
1304 // Draw rectangle with rounded edges outline
1305 void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, float lineThick, Color color)
1306 {
1307 if (lineThick < 0) lineThick = 0;
1308
1309 // Not a rounded rectangle
1310 if (roundness <= 0.0f)
1311 {
1312 DrawRectangleLinesEx((Rectangle){rec.x-lineThick, rec.y-lineThick, rec.width+2*lineThick, rec.height+2*lineThick}, lineThick, color);
1313 return;
1314 }
1315
1316 if (roundness >= 1.0f) roundness = 1.0f;
1317
1318 // Calculate corner radius
1319 float radius = (rec.width > rec.height)? (rec.height*roundness)/2 : (rec.width*roundness)/2;
1320 if (radius <= 0.0f) return;
1321
1322 // Calculate number of segments to use for the corners
1323 if (segments < 4)
1324 {
1325 // Calculate the maximum angle between segments based on the error rate (usually 0.5f)
1326 float th = acosf(2*powf(1 - SMOOTH_CIRCLE_ERROR_RATE/radius, 2) - 1);
1327 segments = (int)(ceilf(2*PI/th)/2.0f);
1328 if (segments <= 0) segments = 4;
1329 }
1330
1331 float stepLength = 90.0f/(float)segments;
1332 const float outerRadius = radius + lineThick, innerRadius = radius;
1333
1334 /*
1335 Quick sketch to make sense of all of this,
1336 marks the 16 + 4(corner centers P16-19) points we'll use
1337
1338 P0 ================== P1
1339 // P8 P9 \\
1340 // \\
1341 P7 // P15 P10 \\ P2
1342 || *P16 P17* ||
1343 || ||
1344 || P14 P11 ||
1345 P6 \\ *P19 P18* // P3
1346 \\ //
1347 \\ P13 P12 //
1348 P5 ================== P4
1349 */
1350 const Vector2 point[16] = {
1351 {(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
1352 {rec.x + rec.width + lineThick, (float)(rec.y + rec.height) - innerRadius}, {(float)(rec.x + rec.width) - innerRadius, rec.y + rec.height + lineThick}, // P3, P4
1353 {(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
1354 {(float)rec.x + innerRadius, rec.y}, {(float)(rec.x + rec.width) - innerRadius, rec.y}, // P8, P9
1355 { rec.x + rec.width, (float)rec.y + innerRadius }, {rec.x + rec.width, (float)(rec.y + rec.height) - innerRadius}, // P10, P11
1356 {(float)(rec.x + rec.width) - innerRadius, rec.y + rec.height}, {(float)rec.x + innerRadius, rec.y + rec.height}, // P12, P13
1357 { rec.x, (float)(rec.y + rec.height) - innerRadius}, {rec.x, (float)rec.y + innerRadius} // P14, P15
1358 };
1359
1360 const Vector2 centers[4] = {
1361 {(float)rec.x + innerRadius, (float)rec.y + innerRadius}, {(float)(rec.x + rec.width) - innerRadius, (float)rec.y + innerRadius}, // P16, P17
1362 {(float)(rec.x + rec.width) - innerRadius, (float)(rec.y + rec.height) - innerRadius}, {(float)rec.x + innerRadius, (float)(rec.y + rec.height) - innerRadius} // P18, P19
1363 };
1364
1365 const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };
1366
1367 if (lineThick > 1)
1368 {
1369 #if defined(SUPPORT_QUADS_DRAW_MODE)
1370 rlSetTexture(texShapes.id);
1371
1372 rlBegin(RL_QUADS);
1373
1374 // Draw all the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1375 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1376 {
1377 float angle = angles[k];
1378 const Vector2 center = centers[k];
1379 for (int i = 0; i < segments; i++)
1380 {
1381 rlColor4ub(color.r, color.g, color.b, color.a);
1382
1383 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1384 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
1385
1386 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1387 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1388
1389 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1390 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1391
1392 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1393 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1394
1395 angle += stepLength;
1396 }
1397 }
1398
1399 // Upper rectangle
1400 rlColor4ub(color.r, color.g, color.b, color.a);
1401 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1402 rlVertex2f(point[0].x, point[0].y);
1403 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1404 rlVertex2f(point[8].x, point[8].y);
1405 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1406 rlVertex2f(point[9].x, point[9].y);
1407 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1408 rlVertex2f(point[1].x, point[1].y);
1409
1410 // Right rectangle
1411 rlColor4ub(color.r, color.g, color.b, color.a);
1412 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1413 rlVertex2f(point[2].x, point[2].y);
1414 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1415 rlVertex2f(point[10].x, point[10].y);
1416 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1417 rlVertex2f(point[11].x, point[11].y);
1418 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1419 rlVertex2f(point[3].x, point[3].y);
1420
1421 // Lower rectangle
1422 rlColor4ub(color.r, color.g, color.b, color.a);
1423 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1424 rlVertex2f(point[13].x, point[13].y);
1425 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1426 rlVertex2f(point[5].x, point[5].y);
1427 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1428 rlVertex2f(point[4].x, point[4].y);
1429 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1430 rlVertex2f(point[12].x, point[12].y);
1431
1432 // Left rectangle
1433 rlColor4ub(color.r, color.g, color.b, color.a);
1434 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1435 rlVertex2f(point[15].x, point[15].y);
1436 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1437 rlVertex2f(point[7].x, point[7].y);
1438 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1439 rlVertex2f(point[6].x, point[6].y);
1440 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1441 rlVertex2f(point[14].x, point[14].y);
1442
1443 rlEnd();
1444 rlSetTexture(0);
1445 #else
1446 rlBegin(RL_TRIANGLES);
1447
1448 // Draw all of the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1449 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1450 {
1451 float angle = angles[k];
1452 const Vector2 center = centers[k];
1453
1454 for (int i = 0; i < segments; i++)
1455 {
1456 rlColor4ub(color.r, color.g, color.b, color.a);
1457
1458 rlVertex2f(center.x + cosf(DEG2RAD*angle)*innerRadius, center.y + sinf(DEG2RAD*angle)*innerRadius);
1459 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1460 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1461
1462 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*innerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*innerRadius);
1463 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1464 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1465
1466 angle += stepLength;
1467 }
1468 }
1469
1470 // Upper rectangle
1471 rlColor4ub(color.r, color.g, color.b, color.a);
1472 rlVertex2f(point[0].x, point[0].y);
1473 rlVertex2f(point[8].x, point[8].y);
1474 rlVertex2f(point[9].x, point[9].y);
1475 rlVertex2f(point[1].x, point[1].y);
1476 rlVertex2f(point[0].x, point[0].y);
1477 rlVertex2f(point[9].x, point[9].y);
1478
1479 // Right rectangle
1480 rlColor4ub(color.r, color.g, color.b, color.a);
1481 rlVertex2f(point[10].x, point[10].y);
1482 rlVertex2f(point[11].x, point[11].y);
1483 rlVertex2f(point[3].x, point[3].y);
1484 rlVertex2f(point[2].x, point[2].y);
1485 rlVertex2f(point[10].x, point[10].y);
1486 rlVertex2f(point[3].x, point[3].y);
1487
1488 // Lower rectangle
1489 rlColor4ub(color.r, color.g, color.b, color.a);
1490 rlVertex2f(point[13].x, point[13].y);
1491 rlVertex2f(point[5].x, point[5].y);
1492 rlVertex2f(point[4].x, point[4].y);
1493 rlVertex2f(point[12].x, point[12].y);
1494 rlVertex2f(point[13].x, point[13].y);
1495 rlVertex2f(point[4].x, point[4].y);
1496
1497 // Left rectangle
1498 rlColor4ub(color.r, color.g, color.b, color.a);
1499 rlVertex2f(point[7].x, point[7].y);
1500 rlVertex2f(point[6].x, point[6].y);
1501 rlVertex2f(point[14].x, point[14].y);
1502 rlVertex2f(point[15].x, point[15].y);
1503 rlVertex2f(point[7].x, point[7].y);
1504 rlVertex2f(point[14].x, point[14].y);
1505 rlEnd();
1506 #endif
1507 }
1508 else
1509 {
1510 // Use LINES to draw the outline
1511 rlBegin(RL_LINES);
1512
1513 // Draw all the 4 corners first: Upper Left Corner, Upper Right Corner, Lower Right Corner, Lower Left Corner
1514 for (int k = 0; k < 4; ++k) // Hope the compiler is smart enough to unroll this loop
1515 {
1516 float angle = angles[k];
1517 const Vector2 center = centers[k];
1518
1519 for (int i = 0; i < segments; i++)
1520 {
1521 rlColor4ub(color.r, color.g, color.b, color.a);
1522 rlVertex2f(center.x + cosf(DEG2RAD*angle)*outerRadius, center.y + sinf(DEG2RAD*angle)*outerRadius);
1523 rlVertex2f(center.x + cosf(DEG2RAD*(angle + stepLength))*outerRadius, center.y + sinf(DEG2RAD*(angle + stepLength))*outerRadius);
1524 angle += stepLength;
1525 }
1526 }
1527
1528 // And now the remaining 4 lines
1529 for (int i = 0; i < 8; i += 2)
1530 {
1531 rlColor4ub(color.r, color.g, color.b, color.a);
1532 rlVertex2f(point[i].x, point[i].y);
1533 rlVertex2f(point[i + 1].x, point[i + 1].y);
1534 }
1535
1536 rlEnd();
1537 }
1538 }
1539
1540 // Draw a triangle
1541 // NOTE: Vertex must be provided in counter-clockwise order
1542 void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
1543 {
1544 #if defined(SUPPORT_QUADS_DRAW_MODE)
1545 rlSetTexture(texShapes.id);
1546
1547 rlBegin(RL_QUADS);
1548 rlColor4ub(color.r, color.g, color.b, color.a);
1549
1550 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1551 rlVertex2f(v1.x, v1.y);
1552
1553 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1554 rlVertex2f(v2.x, v2.y);
1555
1556 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1557 rlVertex2f(v2.x, v2.y);
1558
1559 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1560 rlVertex2f(v3.x, v3.y);
1561 rlEnd();
1562
1563 rlSetTexture(0);
1564 #else
1565 rlBegin(RL_TRIANGLES);
1566 rlColor4ub(color.r, color.g, color.b, color.a);
1567 rlVertex2f(v1.x, v1.y);
1568 rlVertex2f(v2.x, v2.y);
1569 rlVertex2f(v3.x, v3.y);
1570 rlEnd();
1571 #endif
1572 }
1573
1574 // Draw a triangle using lines
1575 // NOTE: Vertex must be provided in counter-clockwise order
1576 void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
1577 {
1578 rlBegin(RL_LINES);
1579 rlColor4ub(color.r, color.g, color.b, color.a);
1580 rlVertex2f(v1.x, v1.y);
1581 rlVertex2f(v2.x, v2.y);
1582
1583 rlVertex2f(v2.x, v2.y);
1584 rlVertex2f(v3.x, v3.y);
1585
1586 rlVertex2f(v3.x, v3.y);
1587 rlVertex2f(v1.x, v1.y);
1588 rlEnd();
1589 }
1590
1591 // Draw a triangle fan defined by points
1592 // NOTE: First vertex provided is the center, shared by all triangles
1593 // By default, following vertex should be provided in counter-clockwise order
1594 void DrawTriangleFan(Vector2 *points, int pointCount, Color color)
1595 {
1596 if (pointCount >= 3)
1597 {
1598 rlSetTexture(texShapes.id);
1599 rlBegin(RL_QUADS);
1600 rlColor4ub(color.r, color.g, color.b, color.a);
1601
1602 for (int i = 1; i < pointCount - 1; i++)
1603 {
1604 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1605 rlVertex2f(points[0].x, points[0].y);
1606
1607 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1608 rlVertex2f(points[i].x, points[i].y);
1609
1610 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1611 rlVertex2f(points[i + 1].x, points[i + 1].y);
1612
1613 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1614 rlVertex2f(points[i + 1].x, points[i + 1].y);
1615 }
1616 rlEnd();
1617 rlSetTexture(0);
1618 }
1619 }
1620
1621 // Draw a triangle strip defined by points
1622 // NOTE: Every new vertex connects with previous two
1623 void DrawTriangleStrip(Vector2 *points, int pointCount, Color color)
1624 {
1625 if (pointCount >= 3)
1626 {
1627 rlBegin(RL_TRIANGLES);
1628 rlColor4ub(color.r, color.g, color.b, color.a);
1629
1630 for (int i = 2; i < pointCount; i++)
1631 {
1632 if ((i%2) == 0)
1633 {
1634 rlVertex2f(points[i].x, points[i].y);
1635 rlVertex2f(points[i - 2].x, points[i - 2].y);
1636 rlVertex2f(points[i - 1].x, points[i - 1].y);
1637 }
1638 else
1639 {
1640 rlVertex2f(points[i].x, points[i].y);
1641 rlVertex2f(points[i - 1].x, points[i - 1].y);
1642 rlVertex2f(points[i - 2].x, points[i - 2].y);
1643 }
1644 }
1645 rlEnd();
1646 }
1647 }
1648
1649 // Draw a regular polygon of n sides (Vector version)
1650 void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color)
1651 {
1652 if (sides < 3) sides = 3;
1653 float centralAngle = rotation*DEG2RAD;
1654 float angleStep = 360.0f/(float)sides*DEG2RAD;
1655
1656 #if defined(SUPPORT_QUADS_DRAW_MODE)
1657 rlSetTexture(texShapes.id);
1658
1659 rlBegin(RL_QUADS);
1660 for (int i = 0; i < sides; i++)
1661 {
1662 rlColor4ub(color.r, color.g, color.b, color.a);
1663 float nextAngle = centralAngle + angleStep;
1664
1665 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1666 rlVertex2f(center.x, center.y);
1667
1668 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1669 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1670
1671 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1672 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1673
1674 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1675 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1676
1677 centralAngle = nextAngle;
1678 }
1679 rlEnd();
1680 rlSetTexture(0);
1681 #else
1682 rlBegin(RL_TRIANGLES);
1683 for (int i = 0; i < sides; i++)
1684 {
1685 rlColor4ub(color.r, color.g, color.b, color.a);
1686
1687 rlVertex2f(center.x, center.y);
1688 rlVertex2f(center.x + cosf(centralAngle + angleStep)*radius, center.y + sinf(centralAngle + angleStep)*radius);
1689 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1690
1691 centralAngle += angleStep;
1692 }
1693 rlEnd();
1694 #endif
1695 }
1696
1697 // Draw a polygon outline of n sides
1698 void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color)
1699 {
1700 if (sides < 3) sides = 3;
1701 float centralAngle = rotation*DEG2RAD;
1702 float angleStep = 360.0f/(float)sides*DEG2RAD;
1703
1704 rlBegin(RL_LINES);
1705 for (int i = 0; i < sides; i++)
1706 {
1707 rlColor4ub(color.r, color.g, color.b, color.a);
1708
1709 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1710 rlVertex2f(center.x + cosf(centralAngle + angleStep)*radius, center.y + sinf(centralAngle + angleStep)*radius);
1711
1712 centralAngle += angleStep;
1713 }
1714 rlEnd();
1715 }
1716
1717 void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, float lineThick, Color color)
1718 {
1719 if (sides < 3) sides = 3;
1720 float centralAngle = rotation*DEG2RAD;
1721 float exteriorAngle = 360.0f/(float)sides*DEG2RAD;
1722 float innerRadius = radius - (lineThick*cosf(DEG2RAD*exteriorAngle/2.0f));
1723
1724 #if defined(SUPPORT_QUADS_DRAW_MODE)
1725 rlSetTexture(texShapes.id);
1726
1727 rlBegin(RL_QUADS);
1728 for (int i = 0; i < sides; i++)
1729 {
1730 rlColor4ub(color.r, color.g, color.b, color.a);
1731 float nextAngle = centralAngle + exteriorAngle;
1732
1733 rlTexCoord2f(texShapesRec.x/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1734 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1735
1736 rlTexCoord2f(texShapesRec.x/texShapes.width, texShapesRec.y/texShapes.height);
1737 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1738
1739 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, (texShapesRec.y + texShapesRec.height)/texShapes.height);
1740 rlVertex2f(center.x + cosf(nextAngle)*innerRadius, center.y + sinf(nextAngle)*innerRadius);
1741
1742 rlTexCoord2f((texShapesRec.x + texShapesRec.width)/texShapes.width, texShapesRec.y/texShapes.height);
1743 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1744
1745 centralAngle = nextAngle;
1746 }
1747 rlEnd();
1748 rlSetTexture(0);
1749 #else
1750 rlBegin(RL_TRIANGLES);
1751 for (int i = 0; i < sides; i++)
1752 {
1753 rlColor4ub(color.r, color.g, color.b, color.a);
1754 float nextAngle = centralAngle + exteriorAngle;
1755
1756 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1757 rlVertex2f(center.x + cosf(centralAngle)*radius, center.y + sinf(centralAngle)*radius);
1758 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1759
1760 rlVertex2f(center.x + cosf(centralAngle)*innerRadius, center.y + sinf(centralAngle)*innerRadius);
1761 rlVertex2f(center.x + cosf(nextAngle)*innerRadius, center.y + sinf(nextAngle)*innerRadius);
1762 rlVertex2f(center.x + cosf(nextAngle)*radius, center.y + sinf(nextAngle)*radius);
1763
1764 centralAngle = nextAngle;
1765 }
1766 rlEnd();
1767 #endif
1768 }
1769
1770 //----------------------------------------------------------------------------------
1771 // Module Functions Definition - Collision Detection functions
1772 //----------------------------------------------------------------------------------
1773
1774 // Check if point is inside rectangle
1775 bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
1776 {
1777 bool collision = false;
1778
1779 if ((point.x >= rec.x) && (point.x < (rec.x + rec.width)) && (point.y >= rec.y) && (point.y < (rec.y + rec.height))) collision = true;
1780
1781 return collision;
1782 }
1783
1784 // Check if point is inside circle
1785 bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius)
1786 {
1787 bool collision = false;
1788
1789 collision = CheckCollisionCircles(point, 0, center, radius);
1790
1791 return collision;
1792 }
1793
1794 // Check if point is inside a triangle defined by three points (p1, p2, p3)
1795 bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3)
1796 {
1797 bool collision = false;
1798
1799 float alpha = ((p2.y - p3.y)*(point.x - p3.x) + (p3.x - p2.x)*(point.y - p3.y)) /
1800 ((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
1801
1802 float beta = ((p3.y - p1.y)*(point.x - p3.x) + (p1.x - p3.x)*(point.y - p3.y)) /
1803 ((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
1804
1805 float gamma = 1.0f - alpha - beta;
1806
1807 if ((alpha > 0) && (beta > 0) && (gamma > 0)) collision = true;
1808
1809 return collision;
1810 }
1811
1812 // Check if point is within a polygon described by array of vertices
1813 // NOTE: Based on http://jeffreythompson.org/collision-detection/poly-point.php
1814 bool CheckCollisionPointPoly(Vector2 point, Vector2 *points, int pointCount)
1815 {
1816 bool collision = false;
1817
1818 if (pointCount > 2)
1819 {
1820 for (int i = 0; i < pointCount - 1; i++)
1821 {
1822 Vector2 vc = points[i];
1823 Vector2 vn = points[i + 1];
1824
1825 if ((((vc.y >= point.y) && (vn.y < point.y)) || ((vc.y < point.y) && (vn.y >= point.y))) &&
1826 (point.x < ((vn.x - vc.x)*(point.y - vc.y)/(vn.y - vc.y) + vc.x))) collision = !collision;
1827 }
1828 }
1829
1830 return collision;
1831 }
1832
1833 // Check collision between two rectangles
1834 bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2)
1835 {
1836 bool collision = false;
1837
1838 if ((rec1.x < (rec2.x + rec2.width) && (rec1.x + rec1.width) > rec2.x) &&
1839 (rec1.y < (rec2.y + rec2.height) && (rec1.y + rec1.height) > rec2.y)) collision = true;
1840
1841 return collision;
1842 }
1843
1844 // Check collision between two circles
1845 bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2)
1846 {
1847 bool collision = false;
1848
1849 float dx = center2.x - center1.x; // X distance between centers
1850 float dy = center2.y - center1.y; // Y distance between centers
1851
1852 float distance = sqrtf(dx*dx + dy*dy); // Distance between centers
1853
1854 if (distance <= (radius1 + radius2)) collision = true;
1855
1856 return collision;
1857 }
1858
1859 // Check collision between circle and rectangle
1860 // NOTE: Reviewed version to take into account corner limit case
1861 bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec)
1862 {
1863 bool collision = false;
1864
1865 int recCenterX = (int)(rec.x + rec.width/2.0f);
1866 int recCenterY = (int)(rec.y + rec.height/2.0f);
1867
1868 float dx = fabsf(center.x - (float)recCenterX);
1869 float dy = fabsf(center.y - (float)recCenterY);
1870
1871 if (dx > (rec.width/2.0f + radius)) { return false; }
1872 if (dy > (rec.height/2.0f + radius)) { return false; }
1873
1874 if (dx <= (rec.width/2.0f)) { return true; }
1875 if (dy <= (rec.height/2.0f)) { return true; }
1876
1877 float cornerDistanceSq = (dx - rec.width/2.0f)*(dx - rec.width/2.0f) +
1878 (dy - rec.height/2.0f)*(dy - rec.height/2.0f);
1879
1880 collision = (cornerDistanceSq <= (radius*radius));
1881
1882 return collision;
1883 }
1884
1885 // Check the collision between two lines defined by two points each, returns collision point by reference
1886 bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint)
1887 {
1888 bool collision = false;
1889
1890 float div = (endPos2.y - startPos2.y)*(endPos1.x - startPos1.x) - (endPos2.x - startPos2.x)*(endPos1.y - startPos1.y);
1891
1892 if (fabsf(div) >= FLT_EPSILON)
1893 {
1894 collision = true;
1895
1896 float xi = ((startPos2.x - endPos2.x)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.x - endPos1.x)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
1897 float yi = ((startPos2.y - endPos2.y)*(startPos1.x*endPos1.y - startPos1.y*endPos1.x) - (startPos1.y - endPos1.y)*(startPos2.x*endPos2.y - startPos2.y*endPos2.x))/div;
1898
1899 if (((fabsf(startPos1.x - endPos1.x) > FLT_EPSILON) && (xi < fminf(startPos1.x, endPos1.x) || (xi > fmaxf(startPos1.x, endPos1.x)))) ||
1900 ((fabsf(startPos2.x - endPos2.x) > FLT_EPSILON) && (xi < fminf(startPos2.x, endPos2.x) || (xi > fmaxf(startPos2.x, endPos2.x)))) ||
1901 ((fabsf(startPos1.y - endPos1.y) > FLT_EPSILON) && (yi < fminf(startPos1.y, endPos1.y) || (yi > fmaxf(startPos1.y, endPos1.y)))) ||
1902 ((fabsf(startPos2.y - endPos2.y) > FLT_EPSILON) && (yi < fminf(startPos2.y, endPos2.y) || (yi > fmaxf(startPos2.y, endPos2.y))))) collision = false;
1903
1904 if (collision && (collisionPoint != 0))
1905 {
1906 collisionPoint->x = xi;
1907 collisionPoint->y = yi;
1908 }
1909 }
1910
1911 return collision;
1912 }
1913
1914 // Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
1915 bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshold)
1916 {
1917 bool collision = false;
1918
1919 float dxc = point.x - p1.x;
1920 float dyc = point.y - p1.y;
1921 float dxl = p2.x - p1.x;
1922 float dyl = p2.y - p1.y;
1923 float cross = dxc*dyl - dyc*dxl;
1924
1925 if (fabsf(cross) < (threshold*fmaxf(fabsf(dxl), fabsf(dyl))))
1926 {
1927 if (fabsf(dxl) >= fabsf(dyl)) collision = (dxl > 0)? ((p1.x <= point.x) && (point.x <= p2.x)) : ((p2.x <= point.x) && (point.x <= p1.x));
1928 else collision = (dyl > 0)? ((p1.y <= point.y) && (point.y <= p2.y)) : ((p2.y <= point.y) && (point.y <= p1.y));
1929 }
1930
1931 return collision;
1932 }
1933
1934 // Get collision rectangle for two rectangles collision
1935 Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
1936 {
1937 Rectangle overlap = { 0 };
1938
1939 float left = (rec1.x > rec2.x)? rec1.x : rec2.x;
1940 float right1 = rec1.x + rec1.width;
1941 float right2 = rec2.x + rec2.width;
1942 float right = (right1 < right2)? right1 : right2;
1943 float top = (rec1.y > rec2.y)? rec1.y : rec2.y;
1944 float bottom1 = rec1.y + rec1.height;
1945 float bottom2 = rec2.y + rec2.height;
1946 float bottom = (bottom1 < bottom2)? bottom1 : bottom2;
1947
1948 if ((left < right) && (top < bottom))
1949 {
1950 overlap.x = left;
1951 overlap.y = top;
1952 overlap.width = right - left;
1953 overlap.height = bottom - top;
1954 }
1955
1956 return overlap;
1957 }
1958
1959 //----------------------------------------------------------------------------------
1960 // Module specific Functions Definition
1961 //----------------------------------------------------------------------------------
1962
1963 // Cubic easing in-out
1964 // NOTE: Used by DrawLineBezier() only
1965 static float EaseCubicInOut(float t, float b, float c, float d)
1966 {
1967 if ((t /= 0.5f*d) < 1) return 0.5f*c*t*t*t + b;
1968
1969 t -= 2;
1970
1971 return 0.5f*c*(t*t*t + 2.0f) + b;
1972 }
1973
1974 #endif // SUPPORT_MODULE_RSHAPES
1975