Is this good idea? This is the only way I found how to render rectangleshape from derived class.
Object (base class) and Rectangle (derived class)
class Object {
private:
std::string name;
public:
Object() = default;
Object(std::string object_name) : name(object_name) {}
virtual void draw(sf::RenderWindow* renderwindow) = 0;
virtual void setPosition(float, float) = 0;
virtual void setRotation(float) = 0;
const std::string* getName() {
return &name;
}
const virtual Position getPosition() = 0;
const virtual Rotation getRotation() = 0;
};
class Rectangle : public Object {
private:
sf::RectangleShape rectangleshape;
public:
Rectangle() = default;
Rectangle(std::string name, float width, float height) : rectangleshape(sf::Vector2f(width, height)), Object(name) {}
void draw(sf::RenderWindow* renderwindow) {
renderwindow->draw(rectangleshape); //is this good idea, will this slow my Engine?
}
void setPosition(float position_x, float position_y) {
rectangleshape.setPosition(position_x, position_y);
}
void setRotation(float angle) {
rectangleshape.setRotation(angle);
}
sf::RectangleShape* getRectangleShape() {
return &rectangleshape;
}
const Position getPosition() {
return Position(rectangleshape.getPosition().x, rectangleshape.getPosition().y);
}
const Rotation getRotation() {
return Rotation(rectangleshape.getRotation());
}
};
And here is render:
void Engine::startDraw() {
sf::View view;
view.setSize(1920, 1080);
view.setCenter(view.getSize().x / 2, view.getSize().y / 2);
view = getLetterboxView(view, window.getSize().x, window.getSize().y);
while (isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
run = STOP;
window.close();
return;
case sf::Event::Resized:
view = getLetterboxView(view, event.size.width, event.size.height);
break;
}
}
window.clear(sf::Color::Black);
window.setView(view);
getObject("rect")->draw(&window); //good idea?
window.display();
}
}
If not, can someone give me a better way
? Thanks.