Hello! So I've got this assignement from my school to create a Bouncing Ball animation using SDL.
I have been provided a header file which contains an array of all the triangles that make up the ball. Also I have been provided some empty methods that needs implementation.
Each ball must be represented as a separate object data structure (see "object.h" in the zip file). The object data structure contains all variables pertinent to rendering the object on the screen (translation, scale, model coordinates, etc.). Note that the object data structure uses floats to represent translation coordinates. This is to make it easier to handle very small movements (at points, a ball might be moving at a speed less than a pixel). Cast the translation coordinates into integers before drawing the triangles on the screen.
Your code must keep track of objects (balls) by placing the object data structures in a linked list. You need to create your own linked list implementation. Below is a brief description of the object programming interface:
- CreateObject - Create a new object. The function accepts as input parameters a pointer to the SDL screen, a pointer to a model triangle array, and a variable telling the size of the model triangle array. The function returns a pointer to a new object data structure. The model triangle array specified as input parameter should not be shared across objects. (Not sharing the model triangle array allows e.g. objects to have different colors.) Perform the necessary memory allocation and copying.
- DestroyObject - Free object. The function accepts as input parameters a pointer to an object data structure. The function should free all memory allocated to represent the object (memory allocated for the model triangle array and the object data structure itself).
- Drawobject - Draw object on screen. The function accepts as input parameters a pointer to an object data structure. The function must draw the object on the screen by calling DrawTriangle on each of the model triangles. Remember to update scale, translation, etc., in each triangle data structure before invoking DrawTriangle.
This is my assignment. I dont want anyone to post entire code, because I want to learn myself. So far I have 1 single ball on the screen(not moving yet) However I'm having some difficulties with storing the Objects. To make 1 ball i need to iterate with a for loop through the entire triangle array.
int i;
for(i = 0; i < SPHERE_NUMTRIANGLES; i++){
object = CreateObject(screen,&sphere_model[i],SPHERE_NUMTRIANGLES);
DrawObject(object);
}
This creates 1 ball, but then i need to save my object into a linked list. How do i store my entire object into ONE object? instead of storing each object for each sphere_model? The reason is because i need to create multiple balls to be shown on the screen at once.
object_t *CreateObject(SDL_Surface *screen, triangle_t *model, int numtriangles)
{
// Implement me
object_t *object = malloc(sizeof(object_t));
object->model = malloc(sizeof(triangle_t) * numtriangles);
object->screen = screen;
object->ttl = 5000;
//TODO: ADD RANDOM SPEED
object->speedx = 10;
object->speedy = 5;
object->model = model;
object->model->scale = 0.1f;
object->model->tx = screen->w/2.f;
object->model->ty = 70.0f;
return object;