mirror of https://github.com/axmolengine/axmol.git
Improve drawCircle rendering for big rings (#1013)
* Update HelloWorldScene.cpp replace _director->end(); with menuCloseCallback(this); * Rendering drawCircle
This commit is contained in:
parent
855b493dad
commit
b1c5f106ca
|
@ -354,7 +354,8 @@ void DrawNode::drawCircle(const Vec2& center,
|
|||
bool drawLineToCenter,
|
||||
float scaleX,
|
||||
float scaleY,
|
||||
const Color4B& color)
|
||||
const Color4B& color,
|
||||
float minThickness)
|
||||
{
|
||||
const float coef = 2.0f * (float)M_PI / segments;
|
||||
|
||||
|
@ -366,13 +367,21 @@ void DrawNode::drawCircle(const Vec2& center,
|
|||
vertices[i].x = radius * cosf(rads + angle) * scaleX + center.x;
|
||||
vertices[i].y = radius * sinf(rads + angle) * scaleY + center.y;
|
||||
}
|
||||
if (drawLineToCenter)
|
||||
if (_lineWidth > minThickness)
|
||||
{
|
||||
vertices[segments + 1] = center;
|
||||
drawPoly(vertices, segments + 2, true, color);
|
||||
auto scaleFactor = AX_CONTENT_SCALE_FACTOR() * 2 ;
|
||||
drawPolygon(vertices, segments, Color4B(1.0f, 0.0f, 0.0f, 1.0f), _lineWidth / scaleFactor, color);
|
||||
}
|
||||
else
|
||||
drawPoly(vertices, segments + 1, true, color);
|
||||
{
|
||||
if (drawLineToCenter)
|
||||
{
|
||||
vertices[segments + 1] = center;
|
||||
drawPoly(vertices, segments + 2, true, color);
|
||||
}
|
||||
else
|
||||
drawPoly(vertices, segments + 1, true, color);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawNode::drawCircle(const Vec2& center,
|
||||
|
@ -380,9 +389,10 @@ void DrawNode::drawCircle(const Vec2& center,
|
|||
float angle,
|
||||
unsigned int segments,
|
||||
bool drawLineToCenter,
|
||||
const Color4B& color)
|
||||
const Color4B& color,
|
||||
float minThickness)
|
||||
{
|
||||
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color);
|
||||
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color, minThickness);
|
||||
}
|
||||
|
||||
void DrawNode::drawQuadBezier(const Vec2& origin,
|
||||
|
|
|
@ -129,6 +129,7 @@ public:
|
|||
* @param scaleX The scale value in x.
|
||||
* @param scaleY The scale value in y.
|
||||
* @param color Set the circle color.
|
||||
* @param thickness Set the thickness and draws a better rendered polygon.
|
||||
*/
|
||||
void drawCircle(const Vec2& center,
|
||||
float radius,
|
||||
|
@ -137,7 +138,8 @@ public:
|
|||
bool drawLineToCenter,
|
||||
float scaleX,
|
||||
float scaleY,
|
||||
const Color4B& color);
|
||||
const Color4B& color,
|
||||
float minThickness = 200); // 200 should "simulate/save" th backwards compatibility
|
||||
|
||||
/** Draws a circle given the center, radius and number of segments.
|
||||
*
|
||||
|
@ -147,13 +149,15 @@ public:
|
|||
* @param segments The number of segments.
|
||||
* @param drawLineToCenter Whether or not draw the line from the origin to center.
|
||||
* @param color Set the circle color.
|
||||
* @param thickness Set the thickness which will be draws a better rendered polygon.
|
||||
*/
|
||||
void drawCircle(const Vec2& center,
|
||||
float radius,
|
||||
float angle,
|
||||
unsigned int segments,
|
||||
bool drawLineToCenter,
|
||||
const Color4B& color);
|
||||
const Color4B& color,
|
||||
float minThickness = 200); // 200 should "simulate/save" th backwards compatibility
|
||||
|
||||
/** Draws a quad bezier path.
|
||||
*
|
||||
|
|
|
@ -34,6 +34,7 @@ DrawPrimitivesTests::DrawPrimitivesTests()
|
|||
{
|
||||
ADD_TEST_CASE(DrawNodeTest);
|
||||
ADD_TEST_CASE(Issue11942Test);
|
||||
ADD_TEST_CASE(BetterCircleRendering);
|
||||
ADD_TEST_CASE(Issue829Test);
|
||||
}
|
||||
|
||||
|
@ -225,10 +226,10 @@ Issue11942Test::Issue11942Test()
|
|||
// draw a circle thickness 10
|
||||
draw0->setLineWidth(10);
|
||||
draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1));
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
|
||||
draw0->setLineWidth(1); // thickness 10 will replaced with thickness 1 (also for all 'same' draw commands before!)
|
||||
draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1));
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
|
||||
|
||||
|
||||
// DrawNode 1 ------------------------------------------
|
||||
|
@ -238,10 +239,10 @@ Issue11942Test::Issue11942Test()
|
|||
// draw a second circle thickness 1
|
||||
draw1->setLineWidth(1);
|
||||
draw1->drawCircle(VisibleRect::center() + Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1));
|
||||
draw1->setLineWidth(10); // thickness 1 will replaced with thickness 10 (also for all 'same' draw commands before!)
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
|
||||
draw1->setLineWidth(20); // thickness 1 will replaced with thickness 10 (also for all 'same' draw commands before!)
|
||||
draw1->drawCircle(VisibleRect::center() + Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1));
|
||||
Color4F(AXRANDOM_0_1(), AXRANDOM_0_1(), AXRANDOM_0_1(), 1.0f));
|
||||
}
|
||||
|
||||
string Issue11942Test::title() const
|
||||
|
@ -254,6 +255,59 @@ string Issue11942Test::subtitle() const
|
|||
return "setLineWidth() change the WHOLE DrawNode object 'line with'";
|
||||
}
|
||||
|
||||
//
|
||||
// drawCircle new feature
|
||||
//
|
||||
BetterCircleRendering::BetterCircleRendering()
|
||||
{
|
||||
//// DrawNode 0 ------------------------------------------
|
||||
//auto draw0 = DrawNode::create();
|
||||
//addChild(draw0, 10);
|
||||
|
||||
//// draw a circle thickness 10
|
||||
//draw0->setLineWidth(10);
|
||||
//draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, 40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
// Color4F::GREEN, 2);
|
||||
//draw0->setLineWidth(30); // thickness 10 will replaced with thickness 1 (also for all 'same' draw commands before!)
|
||||
//draw0->drawCircle(VisibleRect::center() - Vec2(140.0f, -40.0f), 50, AX_DEGREES_TO_RADIANS(90), 30, false,
|
||||
// Color4F::GREEN, 2);
|
||||
|
||||
//// DrawNode 1 ------------------------------------------
|
||||
drawNode = DrawNode::create();
|
||||
addChild(drawNode, 10);
|
||||
|
||||
scheduleUpdate();
|
||||
|
||||
}
|
||||
|
||||
void BetterCircleRendering::update(float dt)
|
||||
{
|
||||
static float thick = 0;
|
||||
thick += 0.5;
|
||||
if (thick > 200)
|
||||
thick = 0;
|
||||
|
||||
|
||||
drawNode->clear();
|
||||
drawNode->setLineWidth(thick);
|
||||
|
||||
drawNode->drawCircle(VisibleRect::center() + Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false,
|
||||
Color4F::RED);
|
||||
|
||||
drawNode->drawCircle(VisibleRect::center() - Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false,
|
||||
Color4F::GREEN, 2);
|
||||
}
|
||||
|
||||
string BetterCircleRendering::title() const
|
||||
{
|
||||
return "Rendering drawCircle";
|
||||
}
|
||||
|
||||
string BetterCircleRendering::subtitle() const
|
||||
{
|
||||
return "Green be the optimized rendering circle";
|
||||
}
|
||||
|
||||
Issue829Test::Issue829Test()
|
||||
{
|
||||
Vec2 vertices0[] = {{50.0, 20.0}, {100.0, 0.0}, {80.0, 50.0}, {100.0, 100.0},
|
||||
|
|
|
@ -58,6 +58,22 @@ public:
|
|||
virtual std::string subtitle() const override;
|
||||
};
|
||||
|
||||
|
||||
class BetterCircleRendering : public DrawPrimitivesBaseTest
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(BetterCircleRendering);
|
||||
|
||||
BetterCircleRendering();
|
||||
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
void update(float dt);
|
||||
|
||||
private:
|
||||
ax::DrawNode* drawNode;
|
||||
};
|
||||
|
||||
class Issue829Test : public DrawPrimitivesBaseTest
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue