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.
what's the point of writing new Rectangle class if you already have sf::RectangleShape?
Indeed.
The only extra thing in your rectangle class, Putarda, seems to be the fact that it has a "name":
std::unordered_map<std::string, sf::RectangleShape> rectangles; // instead of your classes
window.draw(rectangles["rect"]); // instead of "getObject("rect")->draw(&window);"
std::unordered_map<std::string, sf::Drawable*> objects;
This was already mentioned by korczurekk above.
or even:
std::unordered_map<std::string, sf::Shape*> objects;
SFML's Drawable class is a base class used by all SFML's drawable classes and can be used for custom drawable classes by inheriting from sf::Drawable (http://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php#creating-an-sfml-like-entity).
SFML's Shape class inherits from sf::Drawable itself and all SFML shapes inherit from sf::Shape so if you create your new shapes by inheriting from sf::Shape (http://www.sfml-dev.org/tutorials/2.4/graphics-shape.php#custom-shape-types), you can use that, whereas if you cannot use sf::Shape (there are limitations to the type of shape that can be created) then you can inherit from sf::Drawable directly.
Remember that you may also wish to inherit from sf::Transformable if you wish to include those features in your 'object' but drawable would be the one from which all of your drawable classes should derive (to keep a cleaner syntax and also help with your problem).