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

Author Topic: [Solved]Shape objects as class member not drawing?  (Read 3392 times)

0 Members and 1 Guest are viewing this topic.

SomeUser1111

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved]Shape objects as class member not drawing?
« on: December 28, 2021, 12:09:37 pm »
I found that drawable objects(such as CircleShape, VertexArray...etc) cannot be drawn if they are class member. What is going wrong? Is there a problem in constructing a object?

I'm using:
SFML 2.5.1
GCC 7.3.0

This is an simple example regards the problem.
#include <SFML/Graphics.hpp>

class Class{
        public:
        sf::CircleShape circle;
        Class(){
                sf::CircleShape circle(100.f);
        }
        void main(){
                sf::RenderWindow window(sf::VideoMode(640,480),"Title");
                while(window.isOpen()){
                        sf::Event event;
                        while(window.pollEvent(event)){
                                if (event.type == sf::Event::Closed) window.close();
                        }
                        window.clear(sf::Color::Black);
                        window.draw(circle); // This does not draw
                        window.display();
                }
        }
};
int main(){
        Class clazz;
        clazz.main();
       
        sf::RenderWindow window(sf::VideoMode(640,480),"Title");
        while(window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event)){
                        if (event.type == sf::Event::Closed) window.close();
                }
                window.clear(sf::Color::Black);
                window.draw(clazz.circle); // This doesn't draw either
                window.display();
        }
}
 
« Last Edit: December 28, 2021, 12:51:27 pm by SomeUser1111 »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Shape objects as class member not drawing?
« Reply #1 on: December 28, 2021, 12:40:33 pm »
You are making two CircleShapes. The first is the CircleShape in Class, which has the default radius of 0 (so you can't see it). The second is a temporary CircleShape in the Class constructor with a radius of 100, but it is deleted as soon as the constructor ends.

Try changing your constructor to:
Class(){
                circle.setRadius(100.f);
        }
That way it's modifying the original circle instead of making a second one.

Another alternative (slightly more efficient) would be to use initializer lists:
Class():circle(100.f){
        }
https://www.softwaretestinghelp.com/initializer-lists-in-cpp
https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c

SomeUser1111

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Shape objects as class member not drawing?
« Reply #2 on: December 28, 2021, 12:50:56 pm »
Thank you so much for your reply. I just didn't know how classes work, that's embrassing. :-[

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: [Solved]Shape objects as class member not drawing?
« Reply #3 on: December 28, 2021, 12:54:28 pm »
Don't worry, it's a VERY complex language. :)
I've been teaching C++ for 20 years and bits of it still confuse me (well, the newer bits, I'm used to old school C++).

 

anything