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

Author Topic: [Solved] Can't draw shapes defined outside the while(window.isOpen()) loop  (Read 2706 times)

0 Members and 1 Guest are viewing this topic.

crissium

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello, I'm new to SFML (and C++, I'm a first-year university student). I defined a custom shape outside of the main loop and it wouldn't draw. But, when I define that inside the loop, it works very well.
What's wrong?
(SquareComponent is something I defined myself. I have tried SFML before, but everything was OK that time.)

int main(void)
{
        sf::RenderWindow window(sf::VideoMode(WindowWidth, WindowHeight), "SFML", sf::Style::Close | sf::Style::Titlebar);

        // SquareComponent thing1({14.f,11.f});

        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear(sf::Color::Black);

                SquareComponent thing1({14.f,11.f}); // if defined outside this loop (line 31), it won't draw!
                window.draw(thing1);
               
                window.display();
        }
}
« Last Edit: February 23, 2022, 03:15:43 am by crissium »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Can't draw shapes defined outside the while(window.isOpen()) loop
« Reply #1 on: February 22, 2022, 11:27:10 am »
this part seems OK, so i think the problem is in the rest of the code. probably in SquareComponent definitions.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

crissium

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Can't draw shapes defined outside the while(window.isOpen()) loop
« Reply #2 on: February 22, 2022, 11:43:11 am »
Thanks, here's the class definition:
struct Position
{
        float x;
        float y;

        Position(float x = 0.f, float y = 0.f) : x(x), y(y)
        {
        }
};

class SquareComponent : public sf::RectangleShape
{
private:
        Position loc;

public:
        const float SideLen = 10.f;
        SquareComponent(Position position = {});
        void setLoc(Position position = {});
};

SquareComponent::SquareComponent(Position position) : RectangleShape(sf::Vector2f(SideLen, SideLen)), loc(position)
{
        setPosition(loc.x, loc.y);
        setFillColor(sf::Color::White);
}

void SquareComponent::setLoc(Position position)
{
        loc = position;
        setPosition(loc.x, loc.y);
}
And I used the built-in shapes (CircleShape, etc.) last time I tried SFML.
« Last Edit: February 22, 2022, 11:45:57 am by crissium »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Can't draw shapes defined outside the while(window.isOpen()) loop
« Reply #3 on: February 22, 2022, 03:05:46 pm »
the rectangle size is not being correctly set.
after this line
SquareComponent::SquareComponent(Position position) : RectangleShape(sf::Vector2f(SideLen, SideLen)), loc(position)
it is still 0. maybe a more experienced C++ user could explain why. but it works if you manually set it inside the constructor using setSize().

by the way, your Position class is exactly the same as sf::Vector2f, so you could use that.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

crissium

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Can't draw shapes defined outside the while(window.isOpen()) loop
« Reply #4 on: February 23, 2022, 03:13:44 am »
Many thanks! It works now, though I still cannot work out why.

Hapax

  • Hero Member
  • *****
  • Posts: 3362
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
I believe that the sideLen member hasn't (necessarily) been initialised at that point as it's still doing its base constructor. It makes it clearer if you set it in the constructor along with the loc member:
It could be doing it like this, for example:
SquareComponent::SquareComponent(Position position) : RectangleShape(sf::Vector2f(SideLen, SideLen)), sideLen(10.f), loc(position)

If you're using this format (and maybe even if not), it can be best to set them all there and not use inline values at declaration.

Since it's a single value and it's being initialised at the same time, you can just use the value directly.
For example, something like:
SquareComponent::SquareComponent(Position position) : RectangleShape(sf::Vector2f(10.f, 10.f)), sideLen(10.f), loc(position)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything