Smart pointer for sf:Shape and removed useless dynamic_cast (Virtual functions are useful, aren't they ?)
This commit is contained in:
parent
86273890ae
commit
52ccc0b426
5 changed files with 18 additions and 31 deletions
|
@ -37,6 +37,8 @@ target_link_libraries(xmlParser
|
||||||
sfml-graphics
|
sfml-graphics
|
||||||
pugixml)
|
pugixml)
|
||||||
|
|
||||||
|
target_compile_options(xmlParser PRIVATE -Wall -Wpedantic -Wextra)
|
||||||
|
|
||||||
add_executable(gTestXMLParser gTestXMLParser.cpp)
|
add_executable(gTestXMLParser gTestXMLParser.cpp)
|
||||||
target_compile_options(gTestXMLParser PRIVATE -pthread)
|
target_compile_options(gTestXMLParser PRIVATE -pthread)
|
||||||
|
|
||||||
|
|
|
@ -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)
|
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->setPosition(x-r,y-r);
|
||||||
shape->setFillColor(color);
|
shape->setFillColor(color);
|
||||||
}
|
}
|
||||||
|
@ -20,16 +20,11 @@ namespace xmlParser
|
||||||
{
|
{
|
||||||
label = "Test Circle";
|
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->setPosition(x-r,y-r);
|
||||||
shape->setFillColor(color);
|
shape->setFillColor(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Circle::draw(sf::RenderWindow& window)
|
|
||||||
{
|
|
||||||
window.draw(*(dynamic_cast<sf::CircleShape*>(shape)));
|
|
||||||
}
|
|
||||||
|
|
||||||
float Circle::getR() const
|
float Circle::getR() const
|
||||||
{
|
{
|
||||||
return r;
|
return r;
|
||||||
|
@ -38,7 +33,7 @@ namespace xmlParser
|
||||||
void Circle::setR(float newR)
|
void Circle::setR(float newR)
|
||||||
{
|
{
|
||||||
r = newR;
|
r = newR;
|
||||||
dynamic_cast<sf::CircleShape*>(shape)->setRadius(r);
|
dynamic_cast<sf::CircleShape*>(shape.get())->setRadius(r);
|
||||||
shape->setPosition(x-r,y-r);
|
shape->setPosition(x-r,y-r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,6 @@ namespace xmlParser
|
||||||
|
|
||||||
explicit Circle(const pugi::xml_node&);
|
explicit Circle(const pugi::xml_node&);
|
||||||
|
|
||||||
void draw(sf::RenderWindow&) override;
|
|
||||||
|
|
||||||
void setX(float newX) override;
|
void setX(float newX) override;
|
||||||
|
|
||||||
void setY(float newY) override;
|
void setY(float newY) override;
|
||||||
|
|
|
@ -9,16 +9,21 @@
|
||||||
|
|
||||||
namespace xmlParser
|
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)
|
color(color), shape(nullptr)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
DrawingElement::DrawingElement(const pugi::xml_node& node) : x(node.attribute("x").as_int(0)),
|
DrawingElement::DrawingElement(const pugi::xml_node& node) : label(node.attribute("label").as_string("Test DrawingElement")),
|
||||||
y(-node.attribute("y").as_int(1)), label(node.attribute("label").as_string("Test DrawingElement")), shape(nullptr)
|
x(node.attribute("x").as_int(0)), y(-node.attribute("y").as_int(1)), shape(nullptr)
|
||||||
{
|
{
|
||||||
setColor(node.attribute("color").as_string("Yellow"));
|
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)
|
void DrawingElement::draw(sf::RenderWindow& window, float referenceX, float referenceY)
|
||||||
{
|
{
|
||||||
setX(x+referenceX);
|
setX(x+referenceX);
|
||||||
|
@ -37,7 +42,7 @@ namespace xmlParser
|
||||||
{
|
{
|
||||||
x = newX;
|
x = newX;
|
||||||
|
|
||||||
if(shape != nullptr)
|
if(shape)
|
||||||
{
|
{
|
||||||
shape->setPosition(x,-y);
|
shape->setPosition(x,-y);
|
||||||
}
|
}
|
||||||
|
@ -52,7 +57,7 @@ namespace xmlParser
|
||||||
{
|
{
|
||||||
y = newY;
|
y = newY;
|
||||||
|
|
||||||
if(shape != nullptr)
|
if(shape)
|
||||||
{
|
{
|
||||||
shape->setPosition(x,-y);
|
shape->setPosition(x,-y);
|
||||||
}
|
}
|
||||||
|
@ -83,19 +88,6 @@ namespace xmlParser
|
||||||
color = strToColor(stringColor);
|
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)
|
std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element)
|
||||||
{
|
{
|
||||||
ostream << element.toString(0);
|
ostream << element.toString(0);
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace xmlParser
|
||||||
|
|
||||||
explicit DrawingElement(const pugi::xml_node&);
|
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);
|
void draw(sf::RenderWindow&, float referenceX, float referenceY);
|
||||||
|
|
||||||
std::string label;
|
std::string label;
|
||||||
|
@ -42,7 +42,7 @@ namespace xmlParser
|
||||||
void setColor(const sf::Color& newColor);
|
void setColor(const sf::Color& newColor);
|
||||||
void setColor(const std::string& stringColor);
|
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&);
|
friend std::ostream& operator<<(std::ostream&,const DrawingElement&);
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ namespace xmlParser
|
||||||
|
|
||||||
sf::Color color;
|
sf::Color color;
|
||||||
|
|
||||||
sf::Shape* shape;
|
std::unique_ptr<sf::Shape> shape;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue