BlackOHAWK BlackOHAWK - 1 year ago 71
C++ Question

Flickering when drawing the 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;
sf::Sprite tXt(tX);
while (true) // main loop

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

When I try this, I get flickering, and I have no idea where it's coming from.

Answer Source

Your code, simplified, pseudo:

Loop {

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.