1

So basically i dont know how to draw one layer on another in SFML. Ive got a layer of sf::ConvexShape, and know i want to draw sf::CircleShape randomly on it, the problem is that I cant draw it OLNY on that layer.

Tree image

And the question is now, how to make a mask to draw ONLY on one or more (vector) layers. I already tried to make a border form one corner of tree segment to another but it doesnt work at all.

for (int i = 0; i < Swiatelka.size(); ++i)
        {
            if (Swiatelka[i].getPosition().x == (info.winWidth/2) - (choinka[1].getLocalBounds().width/2) || Swiatelka[i].getPosition().x == (info.winWidth / 2) + (choinka[1].getLocalBounds().width / 2))
            {
                continue;
            }
            else
            {
                window.draw(Swiatelka[i]);
            }
        }
thmspl
  • 2,437
  • 3
  • 22
  • 48
SymbiotyK
  • 13
  • 2
  • Could you clarify what you mean by layers? Do you want to draw circles in a specific area, or avoid a specific area? – ShadowMitia Dec 30 '19 at 12:48
  • By layer I mean chosen by me `sf::ConvexShape`, in that case one of the tree segment and i want to draw a circles only in that area avoiding the background which in my code is a `window.clear(sf::Color());` – SymbiotyK Dec 30 '19 at 16:32
  • I see! So you mean draw circles only on the triangles of the tree? Each triangle being a "layer"? – ShadowMitia Dec 30 '19 at 19:23
  • Yes exactly,and each triangle are in a `std::vector` so I can easily manipulate them, the point is that i dont know how to make some kind of mask for the circles to just draw them randomly and they will only appear i area where thoes tringales are. – SymbiotyK Dec 30 '19 at 20:05

1 Answers1

0

So there are ways to mask like you want, and one way I know is called a "stencil buffer", but I don't know if SFML supports it, and it's probably overkill for what you want to do. (In my opinion).

Since you have the coordinates of each triangle, it should be easy to find a point randomy inside of it. You can use that coordinate for the center of your circle.

You could take the basis of your triangle and create a rectangle from it, and then pick a coordinate from inside of it randomly. You can use this answer on stackoverflow to check if the coordinate is in fact inside the triangle, otherwise just randomly select a new coordinate until it is.

Changing the size of both that rectangle and of the "target" triangle, will allow you to target what area of the tree you want the circles to appear in.

(Random though : if you alternate between drawing triangles and drawing circles, you can have circles "beneath twigs" of the higher part of the tree, I don't know how pretty it'll look, but it could be a fun exercise).

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24
  • It worked, all that time I was focused to avoide the background I dont think about check is the point inside. – SymbiotyK Dec 31 '19 at 11:29