From 294ce2fffca2a89e7102a48459a85d4195fa4b19 Mon Sep 17 00:00:00 2001 From: aismann Date: Sat, 11 Feb 2023 01:05:27 +0100 Subject: [PATCH] drawCircle tester improved (#1054) * drawCircle tester improved * add more info to the tester * codacy fix --- .../DrawPrimitivesTest/DrawPrimitivesTest.cpp | 99 ++++++++++++++----- .../DrawPrimitivesTest/DrawPrimitivesTest.h | 10 +- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp b/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp index 35b94e90ee..71774e3592 100644 --- a/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp +++ b/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp @@ -260,42 +260,91 @@ string Issue11942Test::subtitle() const // BetterCircleRendering::BetterCircleRendering() { - //// DrawNode 0 ------------------------------------------ - //auto draw0 = DrawNode::create(); - //addChild(draw0, 10); + //Add lines to see the correct "scale of the 'rings'" changing the window size + auto draw0 = DrawNode::create(); + draw0->setLineWidth(1); + 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); + for (float y = 0; y < VisibleRect::top().y; y += 10) + { + draw0->drawLine({VisibleRect::left().x, y}, {VisibleRect::right().x, y}, Color4B::GRAY); + } + initSliders(); - //// DrawNode 1 ------------------------------------------ drawNode = DrawNode::create(); addChild(drawNode, 10); - + thick = 0; scheduleUpdate(); +} + +void BetterCircleRendering::changeThickness(ax::Ref* pSender, ax::ui::Slider::EventType type) +{ + if (type == ax::ui::Slider::EventType::ON_PERCENTAGE_CHANGED) + { + ax::ui::Slider* sliderThickness = dynamic_cast(pSender); + thick = sliderThickness->getPercent(); + _thickNessLabel->setString("setLineWidth(" + Value(thick).asString() + ")"); + } +} + +void BetterCircleRendering::changeLineWidth(ax::Ref* pSender, ax::ui::Slider::EventType type) +{ + if (type == ax::ui::Slider::EventType::ON_PERCENTAGE_CHANGED) + { + ax::ui::Slider* sliderLineWidth = dynamic_cast(pSender); + lineWidth = sliderLineWidth->getPercent(); + _lineWidthLabel->setString("drawCircle(pos, radius, ..., segemnts, ..., color, " + Value(lineWidth).asString() + ")"); + } +} + +void BetterCircleRendering::initSliders() +{ + auto vsize = Director::getInstance()->getVisibleSize(); + ax::ui::Slider* slider = ax::ui::Slider::create(); + slider->setPercent(0); + slider->loadBarTexture("cocosui/sliderTrack.png"); + slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", ""); + slider->loadProgressBarTexture("cocosui/sliderProgress.png"); + slider->setPosition(Vec2(vsize.width / 2, vsize.height / 6)); + slider->addEventListener(AX_CALLBACK_2(BetterCircleRendering::changeThickness, this)); + + auto ttfConfig = TTFConfig("fonts/arial.ttf", 8); + _thickNessLabel = Label::createWithTTF(ttfConfig, "setLineWidth(0)"); + addChild(_thickNessLabel, 20); + _thickNessLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 15)); + addChild(slider, 20); + + ax::ui::Slider* sliderLineWidth = ax::ui::Slider::create(); + sliderLineWidth->setPercent(0); + sliderLineWidth->loadBarTexture("cocosui/sliderTrack.png"); + sliderLineWidth->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", ""); + sliderLineWidth->loadProgressBarTexture("cocosui/sliderProgress.png"); + sliderLineWidth->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 35)); + sliderLineWidth->addEventListener(AX_CALLBACK_2(BetterCircleRendering::changeLineWidth, this)); + + _lineWidthLabel = Label::createWithTTF(ttfConfig, "drawCircle(pos, radius, ..., segments, ..., color, 0)"); + addChild(_lineWidthLabel, 20); + _lineWidthLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 6 + 50)); + addChild(sliderLineWidth, 20); } void BetterCircleRendering::update(float dt) { - static float thick = 0; - thick += 0.5; - if (thick > 200) - thick = 0; - - drawNode->clear(); - drawNode->setLineWidth(thick); + drawNode->setLineWidth(thick); // value from the slider - 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); + // Old behavior => faster but badly rendering if line width > 5 (= rings) + drawNode->drawCircle(VisibleRect::center() + Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, Color4F::RED); + + // New behavior => slower but good rendering if line width > 5 + auto color = Color4F::GREEN; + if (thick <= lineWidth) + { + color = Color4F::RED; // using the faster rendering internal method of drawCircle (old behavior) + } + drawNode->drawCircle(VisibleRect::center() - Vec2(120.0f, 0.0f), 60, AX_DEGREES_TO_RADIANS(90), 36, false, color, + lineWidth); } string BetterCircleRendering::title() const @@ -305,7 +354,7 @@ string BetterCircleRendering::title() const string BetterCircleRendering::subtitle() const { - return "Green be the optimized rendering circle"; + return "Green: smoother rendering; Red: faster but badly rendering"; } Issue829Test::Issue829Test() diff --git a/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.h b/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.h index f92e3d18f9..73dad45759 100644 --- a/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.h +++ b/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.h @@ -24,6 +24,7 @@ #pragma once #include "axmol.h" +#include "ui/CocosGUI.h" #include "../BaseTest.h" #include @@ -58,7 +59,6 @@ public: virtual std::string subtitle() const override; }; - class BetterCircleRendering : public DrawPrimitivesBaseTest { public: @@ -70,8 +70,16 @@ public: virtual std::string subtitle() const override; void update(float dt); + void initSliders(); + void changeThickness(Ref* pSender, ax::ui::Slider::EventType type); + void changeLineWidth(Ref* pSender, ax::ui::Slider::EventType type); + private: ax::DrawNode* drawNode; + float thick = 0; + ax::Label* _thickNessLabel; + float lineWidth = 0; + ax::Label* _lineWidthLabel; }; class Issue829Test : public DrawPrimitivesBaseTest