Screen flicker and SDL_Flip

Hi,

Why does the following code flicker like crazy when using hardware surfaces
and double buffering on Windows full screen? The same image has been loaded
into the front and back buffers and everything is hardcoded to 640x480x32.
It runs fine on windowed mode. And if I use software surfaces and no double
buffering in fullscreen mode, it also has no flicker.

TIA

int bpp = screen->format->BytesPerPixel;
for (int i = 0; i < 10000; i++) {
  SDL_LockSurface(screen);
  for (int j = 0; j < 256; j++) {
    p = (Uint8 *)screen->pixels + (rand()%480) * screen->pitch +
        (rand()%640) * bpp;
*(Uint32 *)p = 0;
  }
  SDL_UnlockSurface(screen);
  if (i%100 == 0)
    SDL_Flip(screen);
}_________________________________________________________________

Chat with friends online, try MSN Messenger: http://messenger.msn.com

Hi,

Why does the following code flicker like crazy when using hardware
surfaces
and double buffering on Windows full screen? The same image has been
loaded
into the front and back buffers and everything is hardcoded to 640x480x32.
It runs fine on windowed mode. And if I use software surfaces and no
double
buffering in fullscreen mode, it also has no flicker.

TIA

int bpp = screen->format->BytesPerPixel;
for (int i = 0; i < 10000; i++) {
  SDL_LockSurface(screen);
  for (int j = 0; j < 256; j++) {
    p = (Uint8 *)screen->pixels + (rand()%480) * screen->pitch +
        (rand()%640) * bpp;

*(Uint32 *)p = 0;
}
SDL_UnlockSurface(screen);
if (i%100 == 0)
SDL_Flip(screen);
}

At first, both front and back contain the image. Then you make some random
pixels black and call SDL_Flip. Now the front buffer contains the image with
random black pixels and the back buffer will hold the untouched image that
was previously in the front buffer. Now you paint some other random pixels
on that and flip again. Obviously that will flicker because the random
pixels in the two buffers will not match.

The effect you are trying to accomplish requires the image data in the
buffer to remain intact, which is not what doublebuffering does.

A solution would be to use a temporary surface to draw into, then blit this
surface onto the backbuffer just before flipping. Not very efficient but I
think it will work.

Dirk Gerrits