# bouncing Ball

This topic is 4801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi to All i've just done a simple program with sdl in order to bounce a ball in a window. The program read two image file: the background and the ball. At each step of the program the background is redrawn and the ball redrawn in a new position. this procedure it seems quite slow because all the background is redrawn each time. So i want to redraw only the zone of the background that was deleted by the ball. Is this way correct? Has SDL a function that help me?

##### Share on other sites
It may seem odd, but it will really be just as fast to redraw everything each frame. You could do the calculations to figure out which protion of the screen needs to be redrawn, but doing this will probably be just as slow as blitting it all. Anyway, when you get to more advanced games, the whole screen will be in motion much of the time, so you will have to redraw it then anyway.

##### Share on other sites
Umm, I am not sure if this will work for what your doing, cause the ball is moving and keeps on moving. But I will post something that will work if you are controlling the ball or w/e. The source code is out of my source that I am creating.

<code>
void DrawScene()
{
DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
DrawIMG(image, xpos, ypos);

SDL_Flip(screen);
}
</code>

I think (don't hold me to it, as I am VERY sick and not thinking right.) that is what you want. We draw the background as a box that starts 2 pixels before and above the (movable blue) box and ends 2 pixels after and below the box.

I hope this helps. Again I am sick and not thinking right. So if something is not right then I am VERY sorry.

##### Share on other sites
Quote:
 Original post by Chad SmithAgain I am sick and not thinking right. So if something is not right then I am VERY sorry.

That makes two of us, UGH not sure if it's allergies or just a cold. Anyways, Pinzix, there are two approaches as mentioned so far prety much. You can easily just clear the entire screen and redraw everything, or if you only wanted to to update what part has moved, you could do that.

If you want to simply clear the screen and redraw, that is the fastest and easiest way, but not always the best performance wise. Sample code is as follows:
// Clear the screen to blackSDL_FillRect( SDL_GetVideoSurface(), 0, 0);...// Draw...// Calls SDL_FLip if you are using double buffering or simply SDL_UpdateRect if you aren'tSDL_Flip( SDL_GetVideoSurface() );

Now that's it and you'd be all done. If you wanted to use the other method you'd need to do something like this:

1. Create a SDL_Surface the same size as your ball image.
2. At the start of the first frame, before you blit the ball, blit the destination rect of where it's going to be drawn onto the surface you made in step 1.
3. Draw the ball at the desired position.
4. Now before you update the balls, position, you need to store it to know where to draw the surface from step 1 at.
5. Update the ball's position
6. Blit the surface from step 1 at the old position of the ball.
7. Now start at step 2 and repeat again.

That's about it really, it's very straight forward, but as you can see more work and perhaps not as efficient as simply clearing the screen this time. In other more complex cases, such as if you had a static background, and weren't doublr buffering, then you'd want to use the second routine so you only have to draw the background once.

##### Share on other sites
The Cone3d tutorials cover this problem.

http://cone3d.gamedev.net

Look at the second and third one.

Pures

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011256
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!