Created
March 10, 2016 06:49
-
-
Save dontangg/a7c3acec4bf05e61dd58 to your computer and use it in GitHub Desktop.
Rounded rect for DrawNode in Cocos2d-x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void DrawNode::drawSolidRoundedRect(const Vec2 &origin, const Vec2 &destination, float radius, unsigned int segments, const Color4F &color) | |
{ | |
const float coef = 2.0f * (float)M_PI/(segments - 8); | |
Vec2 *vertices = new (std::nothrow) Vec2[segments]; | |
if( ! vertices ) | |
return; | |
// Draw right edge | |
vertices[0].x = destination.x; | |
vertices[0].y = origin.y + radius; | |
vertices[1].x = destination.x; | |
vertices[1].y = destination.y - radius; | |
unsigned int quadrant = 1; | |
unsigned int radsI = 0; | |
for(unsigned int i = 2; i <= segments; i++) { | |
float rads = radsI*coef; | |
GLfloat j = radius * cosf(rads); | |
GLfloat k = radius * sinf(rads); | |
if (rads < M_PI_2 || rads > M_PI + M_PI_2) { | |
if (quadrant == 3) { | |
// Draw bottom edge | |
vertices[i].x = origin.x + radius; | |
vertices[i].y = origin.y; | |
i++; | |
vertices[i].x = destination.x - radius; | |
vertices[i].y = origin.y; | |
quadrant++; | |
continue; | |
} | |
j += destination.x - radius; | |
} else { | |
if (quadrant == 1) { | |
// Draw top edge | |
vertices[i].x = destination.x - radius; | |
vertices[i].y = destination.y; | |
i++; | |
vertices[i].x = origin.x + radius; | |
vertices[i].y = destination.y; | |
quadrant++; | |
continue; | |
} | |
j += origin.x + radius; | |
} | |
if (rads < M_PI) { | |
k += destination.y - radius; | |
} else { | |
if (quadrant == 2) { | |
// Draw left edge | |
vertices[i].x = origin.x; | |
vertices[i].y = destination.y - radius; | |
i++; | |
vertices[i].x = origin.x; | |
vertices[i].y = origin.y + radius; | |
quadrant++; | |
continue; | |
} | |
k += origin.y + radius; | |
} | |
vertices[i].x = j; | |
vertices[i].y = k; | |
radsI++; | |
} | |
drawSolidPoly(vertices, segments, color); | |
CC_SAFE_DELETE_ARRAY(vertices); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
segments+1
in line 5 to prevent crashes!