Smart pointer for sf:Shape and removed useless dynamic_cast (Virtual functions are useful, aren't they ?)

This commit is contained in:
trotFunky 2019-05-22 19:26:15 +02:00
parent 86273890ae
commit 52ccc0b426
5 changed files with 18 additions and 31 deletions

View file

@ -37,6 +37,8 @@ target_link_libraries(xmlParser
sfml-graphics
pugixml)
target_compile_options(xmlParser PRIVATE -Wall -Wpedantic -Wextra)
add_executable(gTestXMLParser gTestXMLParser.cpp)
target_compile_options(gTestXMLParser PRIVATE -pthread)

View file

@ -9,7 +9,7 @@ namespace xmlParser
Circle::Circle(std::string label, float x, float y, float r, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), r(2)
{
shape = new sf::CircleShape(r);
shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(r));
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
@ -20,16 +20,11 @@ namespace xmlParser
{
label = "Test Circle";
}
shape = new sf::CircleShape(r);
shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(r));
shape->setPosition(x-r,y-r);
shape->setFillColor(color);
}
void Circle::draw(sf::RenderWindow& window)
{
window.draw(*(dynamic_cast<sf::CircleShape*>(shape)));
}
float Circle::getR() const
{
return r;
@ -38,7 +33,7 @@ namespace xmlParser
void Circle::setR(float newR)
{
r = newR;
dynamic_cast<sf::CircleShape*>(shape)->setRadius(r);
dynamic_cast<sf::CircleShape*>(shape.get())->setRadius(r);
shape->setPosition(x-r,y-r);
}

View file

@ -22,8 +22,6 @@ namespace xmlParser
explicit Circle(const pugi::xml_node&);
void draw(sf::RenderWindow&) override;
void setX(float newX) override;
void setY(float newY) override;

View file

@ -9,16 +9,21 @@
namespace xmlParser
{
DrawingElement::DrawingElement(std::string label, float x, float y, const sf::Color& color) : x(x), y(-y), label(std::move(label)),
DrawingElement::DrawingElement(std::string label, float x, float y, const sf::Color& color) : label(std::move(label)), x(x), y(-y),
color(color), shape(nullptr)
{}
DrawingElement::DrawingElement(const pugi::xml_node& node) : x(node.attribute("x").as_int(0)),
y(-node.attribute("y").as_int(1)), label(node.attribute("label").as_string("Test DrawingElement")), shape(nullptr)
DrawingElement::DrawingElement(const pugi::xml_node& node) : label(node.attribute("label").as_string("Test DrawingElement")),
x(node.attribute("x").as_int(0)), y(-node.attribute("y").as_int(1)), shape(nullptr)
{
setColor(node.attribute("color").as_string("Yellow"));
}
void DrawingElement::draw(sf::RenderWindow& window)
{
window.draw(*shape);
}
void DrawingElement::draw(sf::RenderWindow& window, float referenceX, float referenceY)
{
setX(x+referenceX);
@ -37,7 +42,7 @@ namespace xmlParser
{
x = newX;
if(shape != nullptr)
if(shape)
{
shape->setPosition(x,-y);
}
@ -52,7 +57,7 @@ namespace xmlParser
{
y = newY;
if(shape != nullptr)
if(shape)
{
shape->setPosition(x,-y);
}
@ -83,19 +88,6 @@ namespace xmlParser
color = strToColor(stringColor);
}
const std::string DrawingElement::toString(int indent) const
{
std::stringstream string;
for(int i = 0;i<indent;i++)
{
string << "\t";
}
string << label << " at x:" << getX() << " y:" << getY();
string << " | color:" << getStringColor();
return(string.str());
}
std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element)
{
ostream << element.toString(0);

View file

@ -26,7 +26,7 @@ namespace xmlParser
explicit DrawingElement(const pugi::xml_node&);
virtual void draw(sf::RenderWindow&) = 0;
virtual void draw(sf::RenderWindow&);
void draw(sf::RenderWindow&, float referenceX, float referenceY);
std::string label;
@ -42,7 +42,7 @@ namespace xmlParser
void setColor(const sf::Color& newColor);
void setColor(const std::string& stringColor);
virtual const std::string toString(int indent) const;
virtual const std::string toString(int indent) const = 0;
friend std::ostream& operator<<(std::ostream&,const DrawingElement&);
@ -52,7 +52,7 @@ namespace xmlParser
sf::Color color;
sf::Shape* shape;
std::unique_ptr<sf::Shape> shape;
};
}