Thread: How can I create an object?

  1. #1
    Registered User
    Join Date
    Oct 2013

    How can I create an object?

    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.

    // 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
    This is what I've done so far, with help from my teacher.

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The point of numtriangles is to tell you how much memory you need for model, so I would suspect you should use that information when you allocate memory for model. (That is, you should need numtriangles*[the size of one triangle] of memory.)

    You should look more carefully at memcpy to see which order your parameters go in (ie destination is first, source is second). Your numtriangles are going to come up again.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    The assignment is straight forward. The object needs to start at the top of the screen with a random speed going either left or right in the X axis.

    That means you need to store the speed, direction and position of the object, as well as the data representing the object itself, such as color, dimensions, SDL information etc..

    Presumably the "object_t" type is already known/declared for you somewhere? It would be useful to share that with us.

    So what else should your createObject function do, besides just copying the given triangle array? It should set the initial speed, direction of the object as well (and perhaps a random color/size/etc.. as suggested by the instructions)

    It sounds to me like the professor has given you everything you need including a big head start. Have you been listening what your professor is teaching you in class??

  4. #4
    Registered User
    Join Date
    Oct 2013
    The thing is, we barely have any lectures, and most of them have been irrelevant to this assignment as far as I'm aware. Also, the use of malloc and memcpy is something we got told to use, without having heard of it before, so most this class is self-study. That's something I'm fine with generally, but in this case I'm not sure what information to look for, and I'm not sure where to look. Google'ing close to aimlessly for thing like memcpy and malloc doesn't get me anywhere with this assignment, unfortunately.

    Below is the Object.h file, that includes something about Object_t. Can I somehow wrap a spoiler or something around it?

    #ifndef OBJECT_H_
    #define OBJECT_H_
    typedef struct object object_t;
    struct object {
        float       scale;          // Object scale
        float       rotation;       // Object rotation
        float       tx, ty;         // Position on screen
        float       speedx, speedy; // Object speed in x and y direction
        unsigned int ttl;           // Time till object should be removed from screen
        int         numtriangles;   // Number of triangles in model
        triangle_t  *model;         // Model triangle array
        SDL_Surface *screen;        // SDL screen
    /* Interfaces */
    object_t *CreateObject(SDL_Surface *screen, triangle_t *triangles, int numtriangles);
    void DestroyObject(object_t *object);
    void DrawObject(object_t *object);
    #endif /*OBJECT_H_*/
    Please do let me know if you have any input, guidelines or help regarding this. I will attempt to implement what you've already said into the code.

    Thanks for the help so far, appreciated.

  5. #5
    Registered User
    Join Date
    Oct 2013
    I've made some slight progress in Object.c

    // 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(triangle_t)*numtriangles);
    	memcpy(object->model, model, sizeof(triangle_t)*numtriangles);
    	object->screen = screen;
    	object->numtriangles = numtriangles;
    	return object;
        // Implement me
    And in main.c

    void BouncingBalls(SDL_Surface *screen)
    	SDL_Event 					event;
    	Object_t *object = CreateObject (screen, sphere_model, SPHERE_NUMTRIANGLES);
    	object->scale = 0.5;
    	object->speedx = 5;
    	object->speedy = 0;
    	object->tx = 50;
    	object->ty = 50;
    	CleaScreen(SDL_Surface *screen);
    	Object->tx + = object->speedx
    	SDL_updateRect(screen, 0, 0, screen->w, screen->h);
    if (event type = SDL_QUIT)
       // Implement me
    Am I onto something here, and what should my Drawobject contain?

    Please do let me know if I need to provide more code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a new class object through a function
    By Darkroman in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2012, 08:10 AM
  2. Is there anyway to create a 24-bit object?
    By maxhavoc in forum C++ Programming
    Replies: 10
    Last Post: 06-15-2006, 07:28 AM
  3. using switch to create an object
    By djardim in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 08:08 PM
  4. how to create a tree of object
    By winsonlee in forum C++ Programming
    Replies: 1
    Last Post: 08-25-2004, 01:20 AM
  5. Create Link List for object
    By winsonlee in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2004, 01:57 PM