Skip to main content
gamedev.net
🔒 Locked

bouncing Ball

Started by Pinzix Sep 14, 2005 at 11:30 AM 3 replies 1.2k views
Original Post
Pinzix
Pinzix
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?
Simian Man
Simian Man
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.

Chad Smith
Chad Smith
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.



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

SDL_Flip(screen);
}



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.

Chad.



Drew_Benton
Drew_Benton
Quote:
Original post by Chad Smith
Again 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.
pures
pures
The Cone3d tutorials cover this problem.

http://cone3d.gamedev.net

Look at the second and third one.

Pures

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.