Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Quad, aliswee), 835 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 3
Page 1 of 4 1 2 3 4
'Structured' programming in Lite-C #277335
07/08/09 21:02
07/08/09 21:02
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline OP
Expert
Joozey  Offline 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.

Code:
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:

Code:
//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
}



Code:
//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
}



Code:
//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: Joozey] #277340
07/08/09 21:18
07/08/09 21:18
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
Thanks for that example, i am not so good with structs now. But with this Code i can learn more about it.

Re: 'Structured' programming in Lite-C [Re: Joozey] #277342
07/08/09 21:20
07/08/09 21:20
Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
VeT Offline

Serious User
VeT  Offline

Serious User

Joined: Aug 2004
Posts: 1,345
Kyiv, Ukraine
Very nice explanation, Joozey. wink


1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy

Newton2 videos: http://tinyurl.com/NewtonVideos
LiteC+Newton2 discussion: http://tinyurl.com/NewtonWrapperDiscussion
Latest LiteC+Newton2 version(v23, from 29.10.2009): http://depositfiles.com/files/ae1l0tpro
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 Offline OP
Expert
Joozey  Offline 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 smile.

Regards,
Joozey


Click and join the 3dgs irc community!
Room: #3dgs
Re: 'Structured' programming in Lite-C [Re: Joozey] #277441
07/09/09 09:54
07/09/09 09:54
Joined: Jun 2009
Posts: 22
Finland
4gottenname Offline
Newbie
4gottenname  Offline
Newbie

Joined: Jun 2009
Posts: 22
Finland
Thank you so much for posting that! Been trying to understand and use them structs last couple of days and this will realy help a lot. laugh

Re: 'Structured' programming in Lite-C [Re: 4gottenname] #277474
07/09/09 11:50
07/09/09 11:50
Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
Cowabanga Offline
Expert
Cowabanga  Offline
Expert

Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
Great explanation, thanks!! laugh smile

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 Offline OP
Expert
Joozey  Offline 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
Re: 'Structured' programming in Lite-C [Re: Joozey] #277529
07/09/09 14:08
07/09/09 14:08
Joined: May 2009
Posts: 137
Ohio, U.S.A.
PigHunter Offline
Member
PigHunter  Offline
Member

Joined: May 2009
Posts: 137
Ohio, U.S.A.
Thank you for this explaination of structures, Joozey. Any tips on how to use add_struct for game_save?

Thanks again!

Re: 'Structured' programming in Lite-C [Re: PigHunter] #277563
07/09/09 16:18
07/09/09 16:18
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline OP
Expert
Joozey  Offline OP
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
I'm affraid you'll have to figure that out yourself, as I've never worked with add_struct smile. But for what I read, you can easily add a save function for an object with this function.


Click and join the 3dgs irc community!
Room: #3dgs
Re: 'Structured' programming in Lite-C [Re: Joozey] #277566
07/09/09 16:35
07/09/09 16:35
Joined: May 2009
Posts: 137
Ohio, U.S.A.
PigHunter Offline
Member
PigHunter  Offline
Member

Joined: May 2009
Posts: 137
Ohio, U.S.A.
From what I've read, I don't think anyone has worked with add_struct before.

Page 1 of 4 1 2 3 4

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1