SFML community forums

Help => Graphics => Topic started by: smurf on May 08, 2017, 04:12:20 am

Title: Why does RenderTarget class not declare the display() function?
Post by: smurf on May 08, 2017, 04:12:20 am
Both of it's children, RenderWindow and RenderTexture have a display() function. Why not define it in the base class of RenderTarget?

Why do I care?

I want to re-use the same code for drawing to a window and drawing to a texture. So I want to be able to pass in the parent object (RenderTarget) into a drawing class that will just draw and not care about whether it is to a window or a texture.

But the lack of the display() function is preventing this.  What am I missing? This seems like an obvious thing to do.

Thanks,
Title: Re: Why does RenderTarget class not declare the display() function?
Post by: Mario on May 08, 2017, 09:13:24 am
It's just a design thing I guess. A render target doesn't have to know when it's "done"  (think unbuffered rendering).

I think the problem here is the fact that you're trying to create a "drawable", just not the way SFML wants you to do.

So rather than calling something like `yourObject.draw(renderTarget);` in your code, you're supposed to call `renderTarget.draw(yourObject)`.

The latter can be achieved by subclassing `sf::Drawable`. There's nothing wrong in making this your "super draw everything class".

As an alternative workaround – if you insist on your interface – you could just overload the method called, once for `sf::RenderTexture*` and once for `sf::RenderWindow*`. This also works for a constructor, just make sure to store which subclass of `sf::RenderTarget` you actually received.