5 registered members (AndrewAMD, ozgur, Ayumi, 2 invisible),
690
guests, and 10
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
'Structured' programming in Lite-C
#277335
07/08/09 21:02
07/08/09 21:02
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
To all those who are new to structures, or trouble with errors, or get lost in yer own spaghetti, or because you just like to read this, I present you a solution I commonly use to keep things structured with as few memory leaks, code spaghetti and unpredictable behaviour as possible. First, an example code about structures. Next, the explanation about structures. Follows, the way I do it, and an explanation.
typedef struct {
var itemNumber;
BMAP *image; //pointer to bitmap
String *name; //pointer to string
} Item;
typedef struct {
Item item1; //no pointer, actual object!
Item item2;
} Inventory;
void main() {
Inventory *inv; //pointer to inventory
inv = malloc( sizeof( Inventory ) ); //assign a new inventory
//first you do after allocating a new structure, is fill all its parameters!
//Otherwise you can end up with some nasty unpredictable errors
//fill item1
//arrow (->) means you call from pointer "inv" to its member "item1"
//point (.) means you call from object "item1" to its member "itemNumber" (or "image", "name")
inv->item1.itemNumber=0;
inv->item1.image = bmap_create("bla");
inv->item1.name = "bitmap of item 1";
//same for item2
[...]
//for the sake of this example, let's remove this inventory again
free( inventory ); //remove the inventory (and automatically the items!)
}
You see, the inventory can now be removed by free(), and it'll remove the items along automatically as they are part of the inventory structure. If the items were pointers, you had to remove the items manually before removing the inventory. Else their pointer will be lost and thus cause a memory leak. If you look further, you see that the item structures have a bitmap and string pointer. While the item structures are removed along with the inventory, the bitmaps and strings will not! So, before removing the inventory, you will have to remove the bitmap and strings of the item structures first in order to prevent memory leaks. As you see, this causes quite some hassle, and is pretty "dangerous" as you may easily oversee a memory leak. Your memory will slowly fill with 'floating', unreachable data structures, often freezing your game after a while, or give other unpredictable results. To solve this requires some dedication to tidy programming. What I do, is give EVERY object I make a new file. This should be consequently done, no exceptions, to keep your code overviewable and tidy. Each objects always gets a creation function, a remove function, and possibly (commonly) a run function. Now, another example code, the way I usually do it:
//Item.c
typedef struct {
int itemNumber;
BMAP *image;
String *name;
} Item;
//a function to visualize an item on a panel
void item_visualize( Inventory *inventory, Panel *panel ) {
panel->bmap = inventory->image;
}
//Item creation function, returns pointer to the newly created Item
Item *item_create( int number, String *imageName ) {
Item *item = malloc( sizeof( Item ) );
//always fill ALL the item's properties immediately after!
item->itemNumber = number;
item->image = bmap_create( imageName );
item->name = imageName;
return item; //return the new item back
}
//function to remove an item
void item_remove( Item *item ) {
ptr_remove( item->image ); //remove bitmap first
ptr_remove( item->name ); //remove name
free( item ); //THEN remove item
}
//Inventory.c
typedef struct {
bool alive;
Panel *panel;
Item *item1;
Item *item2;
} Inventory
//function to keep the inventory running and catch input
void inventory_run( Inventory *inventory ) {
//infinite loop
//in order to check if the object still exists and prevent empty pointer errors,
//you can best add an "alive" state, just add an "alive" boolean in the structure, and check it here
//after the while, call the inventory_remove function to fully destroy
//We simply make an inventory_kill() function, and set the boolean to false to kill the object
while( inventory->alive ) { //as long as the inventory is alive, keep loopin'
//when we press key_1, show item1
if( key_1 ) {
item_visualize( inventory->item1, inventory->panel ); //remember this function in the item code?
}
//when we press key_2, show item2
if( key_2 ) {
item_visualize( inventory->item2, inventory->panel );
}
wait( 1 );
}
inventory_remove( inventory ); //the object is dead, remove it!
}
//function kill
void inventory_kill( Inventory *inventory ) {
inventory->alive = false;
}
//function to create a new inventory
Inventory *inventory_create() {
//allocate new inventory structure
Inventory *inventory = malloc( sizeof( Inventory ) );
//once again, fill all its parameters immediately after
inventory->alive = true; //we set this object to alive first!
inventory->panel = pan_create("bla");
inventory->item1 = item_create( 1, "item1.tga" ); //easily create an item
inventory->item2 = item_create( 2, "item2.tga" );
//in addition to the item code, the inventory code catches player input
//this run function handles this input, and loops infinitely
inventory_run( inventory );
return inventory; //give back the inventory
}
void inventory_remove( Inventory *inventory ) {
item_remove( inventory->item1 ); //remove item1 first
item_remove( inventory->item2 ); //remove item2
ptr_remove( inventory->panel ); //don't forget to remove the panel too
free( inventory ); //THEN remove inventory safely
}
//main
#include "Item.c"
#include "Inventory.c"
void main() {
//create a new inventory!
Inventory *inventory = inventory_create();
//main loop (a little dirty, but forgiveable for now :) )
while( 1 ) {
//when we press q, remove the inventory
if( key_q ) {
inventory_remove( inventory ); //this should go smooth without errors...
}
wait( 1 );
}
}
The biggest fight is against pointer management. You can easily end up under heaps of empty pointer errors, or worse, random crashes that occur at unpredictable times, or even more worse, errors pointing to parts of code that aren't wrong at all! Yes, Lite-c has its bad moments, but they can be overcome. So, quite the hassle for a bit of code that's sort of tidy and emulates a part of object orientation, but this way I discovered to be working quite well when your project gets big. Too big to handle. All comments and critics are welcome. Regards, Joozey P.s. The code represented above is not tested at all. It's about the concept, not about giving away a piece of inventory code. IF something does not work for you and you fail to see why, feel free to comment, I shall change it in the example!
Last edited by Joozey; 07/08/09 21:08.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Widi]
#277343
07/08/09 21:20
07/08/09 21:20
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
I did my best to explain clearly. Feel free to point out any oddities and parts that are hard to understand. Good luck with them structures . Regards, Joozey
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Cowabanga]
#277502
07/09/09 12:44
07/09/09 12:44
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
The most important thing when starting to program like this, is separating responsibilities clearly over your different objects. The only object that can make the Item visible on a panel, is the Item itself. Therefore you need to create a method in the Item "class" (I name each separate object in a file a class, but I'm still waiting for Lite-C to introduce the real deal with classes!!) to visualize the Item (item_visualize()). You may then call this function from every other "class" that uses an Item. If you, for example, start assigning an Item bitmap to a panel bitmap in a different class without calling the item_visualize() function, you'd get a big mess very soon.
Create responsibilities only where they apply, and only perform calls on those responsibilities in parental "classes", never start fiddling with the parameters and logic-functions from other classes outside the class itself.
Mind you, there are a lot of different ways to distribute responsibilities. Those 'ways' are called Design Patterns. The one I represented is just one way, but there are many more. If you know them well, you can easily make maintainable, extendible and overviewable source code.
Last edited by Joozey; 07/09/09 12:46.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
|