Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rendering object through virtual function  (Read 2381 times)

0 Members and 1 Guest are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Rendering object through virtual function
« 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.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Rendering object through virtual function
« Reply #1 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?
« Last Edit: October 30, 2016, 02:14:59 pm by korczurekk »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rendering object through virtual function
« Reply #2 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);"
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Rendering object through virtual function
« Reply #3 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.
« Last Edit: October 30, 2016, 04:34:02 pm by Putarda »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rendering object through virtual function
« Reply #4 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.

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, 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).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Rendering object through virtual function
« Reply #5 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
« Last Edit: October 31, 2016, 11:01:05 pm by Putarda »

 

anything