Monday, April 23, 2012

Multiple bouncing balls.

The first example was very primitive. It had magic values hard coded in the main program. If you wanted to bounce more than just the one ball, you had to create and manage lots of dangling variable that were not attached to anything. The older program also just assumed that the sprite bitmap was loaded and went on it's merry way, which would probably end in a crash at some point.
This newer version makes the balls an object and then creates a list that it works with to update and display objects. I could easily bound dozens of balls around using this program. I also check return values and quit if I manage to not load any balls.

multiballbounce.c

https://github.com/BuckRogers1965/SDLtut/blob/master/ball.bmp
https://github.com/BuckRogers1965/SDLtut/blob/master/BigBlueBall.bmp

Download the two ball images and the .c file to the same directory.

Compile the program using
 
gcc `sdl-config --cflags --libs`  multiballbounce.c

This is much changed from the original file here. While I was working on this program I needed to find out what data the SDL_Surface pointer gave you access to after you loaded an image. I found this site which has detailed info on all the SDL function calls and data structures. Next up is a little collision detection between sprites. After that I want to work on some physics so that gravity shows it's hand.

No comments:

Post a Comment