SFML community forums

Help => Graphics => Topic started by: Putarda on October 30, 2016, 01:14:40 pm

Title: Rendering object through virtual function
Post by: Putarda on October 30, 2016, 01:14:40 pm
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.
Title: Re: Rendering object through virtual function
Post by: korczurekk on October 30, 2016, 02:10:16 pm
Take a look at the sf::Drawable class. ;) And what's the point of writing new Rectangle class if you already have sf::RectangleShape?
Title: Re: Rendering object through virtual function
Post by: Hapax on October 30, 2016, 03:50:46 pm
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);"
Title: Re: Rendering object through virtual function
Post by: Putarda on October 30, 2016, 04:19:55 pm
The point of writing new Rectangle class is because I can't store sf::RectangleShape in map<std::string, Object*>. Any better solution than mine?

I wrote Object class because I needed more types of shapes in std::map. I found a way by writing base class Object and having derived classes (like types: Rectangle, Circle). Spent so much time researching for that code, I forgot the point of it.
Title: Re: Rendering object through virtual function
Post by: Hapax on October 31, 2016, 12:49:09 am
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).
Title: Re: Rendering object through virtual function
Post by: Putarda on October 31, 2016, 08:54:33 pm
Damn, I can't believe how much time I spent researching, while I can just put Drawable...
Anyway thanks  ;D