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

Author Topic: SFML behaves odd  (Read 1387 times)

0 Members and 1 Guest are viewing this topic.

Dajcok

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
SFML behaves odd
« on: January 16, 2020, 12:24:15 pm »
Hello guys,

I'm making drawing program as my school project and i have little trouble with it. I have several shapes which you can draw like triangles, rectangles,... , and I want to include user's own shapes.

void Shapes::getShape(int mousePositionXShapes, int mousePositionYShapes //gets mouse position from another function)
{
        sf::VertexArray userShape(sf::TrianglesStrip, arrayLength);

        if (clickRelease)
        {
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        userShape[n].position = sf::Vector2f(mousePositionXShapes, mousePositionYShapes);
                        userShape[n].color = sf::Color::Yellow;

                        if (n != 0)
                        {
                                int positionX = userShape[n - 1].position.x;
                                int currentPositionX = userShape[n].position.x;

                                cout << "previous position: " << positionX << endl;
                                cout << "current position: " << currentPositionX << endl;
                        }

                        clickRelease = false;
                }
        }

        else if (!sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                if (!clickRelease)
                {
                        clickRelease = true;

                        arrayLength++;
                        n++;

               
                }
        }

        window.clear(sf::Color::White);
        window.draw(userShape);
        window.display();

        //drawShape(userShape);

}
 

This part of code should let user choose his own points by clicking. If he will click 3 points it will form into triangle and each next point will add next triangle. It's basically this: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php -> Primitive Types -> sf::TriangleStrip.

Problem comes when I start the program. First click will define position for first vertex but if i click again for another vertex position, previous vertex will be removed.

Console output looks like this:
previous position: 0
current position: 683
previous position: 0
current position: 1021
previous position: 0
current position: 451
previous position: 0
current position: 527

Why is this happening ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML behaves odd
« Reply #1 on: January 16, 2020, 12:56:36 pm »
You're creating a new vertex array (userShape) every time the function is called. This variable should obviously be declared elsewhere, in a more persistent scope.
Laurent Gomila - SFML developer

Dajcok

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: SFML behaves odd
« Reply #2 on: January 16, 2020, 01:24:11 pm »
I knew it will be something so primitive.. I'm so stupid... Thx for help  ;D