What is the best way to group objects in SFML?

Asked

Viewed 40 times

0

If I have for example 3 different shapes in SFML, and I want to rotate them all relative to a single center, as if these 3 shapes were inside a square, what would be the best shape? Would it be leaving them inside a view and rotating the view? Or is there some more practical way?

1 answer

1


Apparently, from everything I’ve seen, the simplest and most intuitive way is to put objects inside the view.

For example:

  RectangleShape background (Vector2f(windowWidth, windowHeight)); // draw a full rectangle to show the container dimensions
    background.setFillColor(Color::White);

    RectangleShape r1 (Vector2f(100,100)); // 1st object
    r1.setFillColor(Color::Red);
    r1.setPosition(Vector2f(30,10));


    RectangleShape r2 (Vector2f(150,200)); // 2nd object
    r2.setFillColor(Color::Blue);
    r2.setPosition(Vector2f(120,160));


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

        window.clear();
        window.setView(view);
        window.draw(background);
        window.draw(r1);
        window.draw(r2);
        view.setRotation(30); // affects all elements

        window.display();
    }

    return 0;
}

Here is result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.