SFML community forums

Help => Graphics => Topic started by: Renardesque on July 12, 2019, 12:55:41 am

Title: Is clipping needed ?
Post by: Renardesque on July 12, 2019, 12:55:41 am

I know that SFML doesn't have a real clipping feature (if I'm wrong please correct me and ignore the rest of this post).
I think that it can be simulated by using the viewport. Or maybe by drawing on a RenderTexture.

Having not used these techniques yet, my questions is: is it a pain to use these clipping simulation techniques ?
In other words: is clipping a crucial feature or can it be simulated just fine ?
Title: Re: Is clipping needed ?
Post by: Hapax on July 12, 2019, 10:42:48 pm
If you mean visible clipping to a rectangle then yes, either of those methods work.

Note, though, that they don't clip as in remove draw calls that are outside of that section.
It's best to remove things that are out of view from your drawing manually.
Title: Re: Is clipping needed ?
Post by: Renardesque on July 13, 2019, 04:55:29 pm
Are those methods a good alternative to built-in clipping, or do they make the code a lot more complicated ? (maybe by introducing coupling between drawable stuff and window size for the viewport technique, for instance).
Title: Re: Is clipping needed ?
Post by: texus on July 13, 2019, 05:44:14 pm
I use the viewport method for clipping and if you encapsulate the code then it shouldn't make the rest of your code more complicated (at least it doesn't make it more complicated for my use case). The only real downside of the method is that you need to pass the new view to the clipping code every time the view is changed (but that only happens in one place in my code). The rest of the code doesn't need to care about how you do the clipping.

I've implemented my code (https://github.com/texus/TGUI/blob/0.8/src/TGUI/Clipping.cpp) in such a way that I just need to pass the render target and states (which all my draw functions have) to the constructor of the Clipping class together with the rectangle that should be clipped (relative to the render states). As long as the clipping object exists, all draw calls are clipped. The old view will be restored when the clipping object is destroyed. It works when a custom view and viewport were already set, so it even works recursively (two clipping objects can exist at the same time and things will only be drawn inside the intersection).
Clipping clippingObject(target, states, clippingRectTopLeft, clippingRectSize);
target.draw(drawable, states); // drawing will be clipped

Drawing to a RenderTexture will probably work just as well, as long as you encapsulate the rendering so that your code doesn't need to know whether it is drawing to the screen or to an intermediate texture.