I have an assignment that asks me to implement a variant of the classic bouncing balls program.
'Each ball should start off at the top of the screen with a random speed in the x direction. Whenever a ball hits a screen boundary it should bounce off at an angle equal to the impact angle and lose some speed. Eventually each ball should come to a rest at the bottom of the screen. Five seconds after coming to a rest a ball should be removed from 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.
Hint: Do not make the bouncing algorithm too complex. Bouncing a ball off a vertical or horizontal surface can be accomplished without resorting to calculating impact angles.'
The function I'm stuck at, is the first one - createobject.
My first aim is to get one ball on the screen.
This is what I've done so far, with help from my teacher.Code:// Create new object object_t *CreateObject(SDL_Surface *screen, triangle_t *model, int numtriangles) { object_t *object = malloc(sizeof(object_t)); object->model = malloc(sizeof(sphere_model)); object->screen = SDL_Surface; memcpy(*model, object->model, sizeof(object_t)); return object; // Implement me }
I need to get a better understanding of this, and I'd be happy to read, study and do research concerning this topic. The problem is I don't know where to start.
Can anyone come with some help, tips or guidelines with the information I have provided, or do you need any of the other files/code as well?
Thanks in advance.