BlackOHAWK BlackOHAWK - 2 months ago 8
C++ Question

Flickering when drawing thr background outside of the main loop

Here's my CODE:

#include <SFML\Graphics.hpp>
sf::RenderWindow renderWindow(sf::VideoMode(300, 300), "The Problem");
int main()
{
sf::Texture tX;
tX.loadFromFile("Recursos/Fondo.png");
sf::Sprite tXt(tX);
renderWindow.draw(tXt);
while (true) // main loop
{
renderWindow.display();
}
}


tX
is a large sprite (think: background), so I want to avoid redrawing it every frame.

How do I achieve this without the flickering?

Answer

Your code, simplified, pseudo:

Initialize();
Draw();
Loop {
    Display();
}

What you're doing is drawing the scene to a buffer (without clearing it first, by the way), and then displaying it in a loop (without redrawing anything).

I think the reason you have flickering is due to double-buffering (v-sync). What ends up happening is that you drew to one buffer, but left the other one empty. When you display the active buffer, they get flipped (so the next time you display, it's an empty buffer). You really should draw inside the loop.


Alternatively, you can disable multiple-buffering, or try drawing, displaying, drawing again, and then looping (so that you draw to both buffers, assuming there are only two).

Both solutions are not elegant.

If drawing the background really needs optimization, I'm afraid SFML won't give you that granularity. You might want to look in to OpemGL Stencil wizardry.