1
Graphics / Overriding sf::RectangleShape::draw
« on: April 16, 2017, 12:52:43 pm »
Hello,
I derive a class Square from RectangleShape. The Square also contains a text I want to draw. But when I override draw to draw the text, how do I also draw the rectangle? Do I have to create yet another class or is there something I'm missing? Here's the code
Thanks in advance.
I derive a class Square from RectangleShape. The Square also contains a text I want to draw. But when I override draw to draw the text, how do I also draw the rectangle? Do I have to create yet another class or is there something I'm missing? Here's the code
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <string>
class Square : public sf::RectangleShape
{
private:
sf::Text mText;
public:
Square(const sf::Font& font, sf::Vector2f position, const std::string& s = "");
void update();
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
Square::Square(const sf::Font& font, sf::Vector2f position, const std::string& s)
{
mText.setFont(font);
mText.setString(s);
mText.setCharacterSize(26);
mText.setFillColor(sf::Color::White);
mText.setPosition(sf::Vector2f(position.x + 16, position.y + 8));
setPosition(position);
setSize(sf::Vector2f(48, 48));
setFillColor(sf::Color::Transparent);
setOutlineColor(sf::Color::White);
setOutlineThickness(1.3f);
}
void Square::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mText);
}
void Square::update()
{
RectangleShape::update();
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "");
sf::Font font;
font.loadFromFile("arial.ttf");
Square square(font, sf::Vector2f(10, 10), "A");
while(window.isOpen())
{
window.clear();
square.update();
window.draw(square);
window.display();
}
}
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <string>
class Square : public sf::RectangleShape
{
private:
sf::Text mText;
public:
Square(const sf::Font& font, sf::Vector2f position, const std::string& s = "");
void update();
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
Square::Square(const sf::Font& font, sf::Vector2f position, const std::string& s)
{
mText.setFont(font);
mText.setString(s);
mText.setCharacterSize(26);
mText.setFillColor(sf::Color::White);
mText.setPosition(sf::Vector2f(position.x + 16, position.y + 8));
setPosition(position);
setSize(sf::Vector2f(48, 48));
setFillColor(sf::Color::Transparent);
setOutlineColor(sf::Color::White);
setOutlineThickness(1.3f);
}
void Square::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mText);
}
void Square::update()
{
RectangleShape::update();
}
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "");
sf::Font font;
font.loadFromFile("arial.ttf");
Square square(font, sf::Vector2f(10, 10), "A");
while(window.isOpen())
{
window.clear();
square.update();
window.draw(square);
window.display();
}
}
Thanks in advance.