Find the answer to your Linux question:
Results 1 to 6 of 6
I am working on a project in C, that I wish to be cross platform. Using SDL and openGL as my base libraries. The goal of the project is to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Location
    Gastonia, North Carolina
    Posts
    1

    Idea's on handling variables in C


    I am working on a project in C, that I wish to be cross platform. Using SDL and openGL as my base libraries.

    The goal of the project is to create a simple cross platform game creation engine, that requires little programming knowledge, Sort of a BASIC for graphical programming.

    My first step is to create a structure for my Objects list, which I am currently lost on how to proceed.

    My current idea of what I want in my structure:

    /* Object structure */

    struct Object
    {
    bool state; /* object visible, cue to be removed or added, etc.
    int ix, iy, iz; /* initial point of entry to the screen
    int cx, cy, cz; /* current position
    object_data *obd /* point to where the data for the image is located.
    object_logic *obl /* point to how object reacts to world around it.
    }


    How best to alloc memory on my object structure, since I want to add or removed as needed to the processing que.

    My thought is that all screen items will be handled like objects, boundary objects, static objects, text objects, etc.

    Any pointers or links that maybe helpful?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,527
    For designing software that has this level of complexity, you very much want to model it in a UML tool first. There are a number of Eclipse UML plugins which may help. I've used some in the past that were reasonable for a novice. Myself, I use Sparx Enterprise Architect which is an enterprise class UML tool, but it supports full round-trip engineering and reverse engineering of model <-> code. Having a visual representation of how the system components (classes) are related is incredibly helpful in keeping it all together. If you want, Sparx has a basic version for $135 USD. The Professional version (which I use) is $199 USD.

    FYI, Sparx is a Windows program, but it is designed to run well with Wine, which is how I use it on both my CentOS 5 workstation and Ubuntu 9.04 laptop.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,527
    Anyway, to get to your question...

    There are a couple of issues with your struct Object.

    1. In C++ it should be a class instead of a struct for better data encapsulation and hiding. In C++ a class and a structure are much the same, except that in a structure all members and methods are public.
    2. Why the object_data and object_logic members? The logic is part of the class via class methods. The pointer to an object is instantiated when you create an instance of the class. There may be many instances of a class. Your construct only allows for one.

    Anyway, it is apparent to me that you need to read more about object-oriented programming in general, and C++ in particular before you go much further. My comments previously about using UML to design and model the system are relevant. The process of designing your model will help you understand how the classes are structured and related.

    Have fun! You have quite a journey ahead of you!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Nov 2010
    Posts
    6
    Agreed, in C++, all struct's are translated into classes with all public members. Also read a chapter or two on linked list and such. Remember to always deallocate memory at the end of your program. You don't want to have memory leaks.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,527
    Actually, all allocated memory (heap) will be returned to the system when the program terminates. However, you do want to be sure to free/delete allocated memory when the objects it points to are no longer in use. I developed a reference-counting garbage collector for C++ so in 10M lines of code, we never had to delete anything, and had no memory leaks... However, that is not simple to do, especially when needing to deal with circular references in complex classes/structures (obja -> objb -> objc -> obja). The USE was simple, but the logic for the collector was not trivial, to say the least!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2

    Linked List

    This pisses me off too: you said C, but people automatically think you mean c++ >.<. Well, to do it in c, you want to use a linked list. This is how you do a linked list:
    Code:
    struct node {
    	int value; /* or some other value type, it could be FILE * for all we care. Just do: something value; */ /* this is the value contained by this node in the list.
    	struct node *prev; /* a pointer to the previous node */
    	struct node *next; /* a pointer to the next node */
    	/* how this works: this is a list, or circle of nodes. Each one points to the one before it, and after it. Hold the home node somewhere, you the program knows which one is node 0. Then, to access value 1, do home->next->value. */
    };
    
    /* the main reason for a linked list is so each element in the list can be allocated individually, unlike an array */
    
    struct node *init_node(int /*or something else */ value, struct node *prev, struct node *next){
    	struct node *ret = (struct node *) malloc(sizeof(struct node)); /* allocate it */
            if (ret == NULL) return NULL; /* out of memory */
    	ret->value = value; /* initialize */
    	ret->prev = prev;  /* initialize */
            ret->next = next; /* initialize */
            return ret;
    }
    
    struct node *home_init(int value){
            struct node *ret = (struct node *) malloc(sizeof(struct node));
            ret->value = value;
            ret->next = ret; /* the next one is itself, because it is a loop, with one member! */
            ret->prev = ret; /* this one too, same reason */
            return ret;
    }
    /* now for struct Object */
    
    struct Object {
            //... 
            struct node *home;
    	//...
    };
    
    struct node *add_node(struct node *home, int value_to_add, int location /* 0 = home */){ /* returns pointer to home node WARNING: if location == 0, passed node will not longer be home */
    	struct node *curr = home;
            int i;
            for (i = 0; i < location, i++){
    		curr = curr->next;
                    if (curr == home) return NULL; /* index out of bounds */
            }
            struct node *ret = init_node(value_to_add, curr->prev, curr);
            curr->prev->next = ret;
            curr->prev = ret;
            if (location == 0) return ret;
            return home;
    }
    
    /* you probably can figure out how to do the rest, and if you can't google linked list c */

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •