Hello!
I have been experimenting a little bit with SFML and so far it seems nice. Ive got a little nice Server-Client setup with moving sprites and so on, and that part works fine.
Then I had a go at drawing some particle effects, that works fine as well, but here I get some concerns about performance. First off, here is my experiment with particles:
Its based of red rectangles that rise and get smaller and more transparent. The yellow comes from a BlendAdd between each rectangle. Its not that slow, but if a make a few of these I can see that fps starts to drop, so I tried to see how I can optimize it.
First step was to use a VertexArray of quads instead of Rectangle shapes, this part works pretty well once I got the hinge of how it worked.
The remaining problem is that in order to get the Blend to look as I want it to I need to exclude the green background, so I did this by drawing it to a RenderingTexture and then apply that texture to a Sprite which I draw to the window.
So far so good, but I feel that I need some advice here.
First question is short.
Is there some better way to get the blending on "only the particles" without using the RenderingTexture?Second question is a bit longer,
if I have to use the RenderingTexture, what is the optimal way of doing that? Let me give you some of my thoughts.
The first naive way is to just add a Rendering Texture just where it is needed
class FireParticle : public sf::Drawable, public sf::Transformable
{
void draw(sf::RenderTarget &target, sf::RenderStates states) const
{
sf::RenderTexture renderTexture;
renderTexture.create(100, 100);
states.transform *= getTransform();
renderTexture.clear(sf::Color::Transparent);
/*particles is the VertexArray containing all the rectangles*/
renderTexture.draw(particles, sf::RenderStates(sf::BlendAdd));
renderTexture.display();
sf::Sprite sprite;
s.setTexture(renderTexture.getTexture());
target.draw(sprite, states);
}
}
This works and is what I have right now as my PoC, but I see myself that this is not optimal, the constantly new Rendering Textures should be a drag on fps (and it is!)
So I need to reuse the RendeingTexture, and here I wonder how they really are supposed to be used. I can think of a few ways
- Keep a RenderingTexture of the same size as the effect in each Particle class
- Keep a single RenderingTexture and let everything recreate this in the size of the effect (is this just as bad as creating new RenderingTextures?)
- Create a single RenderingTexture next to the RenderingWindow (with the same dimensions) and pass this along to everything that needs separate rendering
The more I think about this it seems like the third option should be the correct one. But since there is a forum I thought I'd ask
Is there a fourth option that I have not thought of?