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

Author Topic: Is clipping needed ?  (Read 3252 times)

0 Members and 2 Guests are viewing this topic.

janf

  • Newbie
  • *
  • Posts: 45
    • View Profile
Is clipping needed ?
« on: July 12, 2019, 12:55:41 am »
Hello,

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 ?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is clipping needed ?
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

janf

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Is clipping needed ?
« Reply #2 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).

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Is clipping needed ?
« Reply #3 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 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.
« Last Edit: July 13, 2019, 05:46:42 pm by texus »
TGUI: C++ SFML GUI

 

anything