Sign in to follow this  
TFS_Waldo

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);

			paddle.Display(g_pScreen, paddleX, (480 - paddle.GetHeight()*2));
			ball.Display(g_pScreen, paddleX, 225);

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this