# Sprites flickering when drawing background image?

## Recommended Posts

TFS_Waldo    146
Hi. I am using SDL in MS Visual C++ 6.0, and I am making a breakout clone. Well, I have the basic main menu done, and I have it drawing the paddle and the ball when you select "New Game". But the problem is, I'm trying to draw a background image, also. When I draw the paddle and ball alone, it's fine. But when I draw the background image, the sprites tend to flicker. Here is my code for drawing:
			SDL_FillRect(g_pScreen, NULL, 0);    // Clear the screen

gamebg.Display(g_pScreen, 0, 0);


What could be the problem here. BTW, here is the code for drawing an image using my image class (which is what those images use):
	SDL_Rect dest; // Destination rectangle
dest.x = x;    // Setup image's x
dest.y = y;    // and y coordinates
dest.w = m_pImage->w;    // Setup image's width
dest.h = m_pImage->h;    // and height

SDL_BlitSurface(m_pImage, NULL, pScreen, &dest);   // Draw the image

SDL_UpdateRects(pScreen, 1, &dest);    // Update changed portion of screen


Well, thanks in advance. I'll be here for a while. I hope someone can help! =) Thanks, Matt U. P.S.: BTW, how is my coding style (formatting, comments, etc.)? ;) (Curiosity gets to me sometimes. =P

##### Share on other sites
nullsquared    126
According to what you showed us, I dont see the problem... If you are drawing a background before all your images then don't clear the screen sine the background will go over the previous stuff. Try that and see if it helps.

##### Share on other sites
TFS_Waldo    146
Well, I tried that. I get the same results. I've tried removing the call to SDL_Flip as well. And nothing seems to work. But thanks for trying. Anyhow else?

##### Share on other sites
TFS_Waldo    146
I know it has something to do with the background image being drawn (which is the same as the resolution 640x480). When I comment that line out, it's fine. :(

##### Share on other sites
nullsquared    126
Well... So you say it works by just clearing the screen without drawing the back? That's odd. Try blitting a different bitmap for a back. Are you sure the back is drawn every frame? Maybe something from your main menu is toggling the back from drawing and not drawing. Do the sprites you draw also flicker?

P.S. Can you show some more code? I don't know how it can help, but your blitting is normal, so it most likely cant be from that...

##### Share on other sites
evillive2    779
If you are not using double buffering and you use SDL_UpdateRects, many times you will see flickering when things are being redrawn at different times durring the same frame. The reason this is avoided with double buffering is that everything is being drawn in the background and the entire screen is updated at once when SDL_Flip is called.

In your case with the code provided above, you update the screen every time a sprite is drawn by calling SDL_UpdateRects. You won't notice the flicker without a background image because there is nothing behind it to update and visually compare against. The solution in this case would be to use a double buffer.

By doing all of your drawing first and THEN calling SDL_Flip or even SDL_UpdateRects with NULL for the update rect, all of the drawing will be updated to the screen at once (hopefully) eliminating the flickering.

For most graphics cards today just clearing and redrawing the entire screen again is no big deal and is prefered over using a dirty rectangle method.

##### Share on other sites
TFS_Waldo    146
Hey everyone. Thanks for the post. And I already (yesterday) took out the call to "SDL_UpdateRects". It works now. LoL =P

Thanks for the attempts to help; I appreciate it. =)

Thanks again!