# Unlimited Structures?

Hi.

I'm new to game programming in c and just wondering how to do this...

In blitzbasic there is types which are basically structures. With the types we can create as many as we like with the same name and then have a for loop that will do the same thing to each of the types with that name.
eg... (can't remember the exact commands in blitz. it's been a while)

--------------

Type THING
field x
field y
End Type

for i = 0 to 20
New THING thing
next

for each(thing)
thing.x = 10
next

--------------

This would create 21 things and then set their x's to 10.

Can you do something like this in c? I want to do this for bullets.

• : Hi.
:
: I'm new to game programming in c and just wondering how to do this...
:
: In blitzbasic there is types which are basically structures. With the types we can create as many as we like with the same name and then have a for loop that will do the same thing to each of the types with that name.
: eg... (can't remember the exact commands in blitz. it's been a while)
:
: --------------
:
: Type THING
: field x
: field y
: End Type
:
: for i = 0 to 20
: New THING thing
: next
:
: for each(thing)
: thing.x = 10
: next
:
: --------------
:
: This would create 21 things and then set their x's to 10.
:
: Can you do something like this in c? I want to do this for bullets.
:
:
:
[blue]You can do everything in "C":[/blue]
[code]
typedef struct
{
int x;
int y;
} THING;

#define NEW(type) ((type*) malloc (sizeof (type)))

THING* array [20];

// Why use two loops? - if you can do it in one!
int iThing;

for (iThing=0; iThing<20; iThing++)
{
array [iThing] = NEW (THING);
array [iThing]->x = 10;
}
[/code]
• : : Hi.
: :
: : I'm new to game programming in c and just wondering how to do this...
: :
: : In blitzbasic there is types which are basically structures. With the types we can create as many as we like with the same name and then have a for loop that will do the same thing to each of the types with that name.
: : eg... (can't remember the exact commands in blitz. it's been a while)
: :
: : --------------
: :
: : Type THING
: : field x
: : field y
: : End Type
: :
: : for i = 0 to 20
: : New THING thing
: : next
: :
: : for each(thing)
: : thing.x = 10
: : next
: :
: : --------------
: :
: : This would create 21 things and then set their x's to 10.
: :
: : Can you do something like this in c? I want to do this for bullets.
: :
: :
: :
: [blue]You can do everything in "C":[/blue]
: [code]
: typedef struct
: {
: int x;
: int y;
: } THING;
:
: #define NEW(type) ((type*) malloc (sizeof (type)))
:
: THING* array [20];
:
: // Why use two loops? - if you can do it in one!
: int iThing;
:
: for (iThing=0; iThing<20; iThing++)
: {
: array [iThing] = NEW (THING);
: array [iThing]->x = 10;
: }
: [/code]
:

But won't that only let me have 20 (whatever number you choose) "thing"'s in the array? That is kinda what i'm doing at the moment but can you have it so that you don't have to say how big the array is so you can just keep adding more bullets if needed?

In blitz you can have it so that you hit the fire button and it basically does, struct BULLET bullet, and it will let you name any number of types the same thing for use in a for loop like, for each(bullet), and the loop will loop for each bullet you have created. Also you can delete any one of the bullet's and it gets rid of it completly whereas with an array i guess you would just have like

bullet[1]->active = 0;

and then you would have to check which bullets aren't active so you can use that bit of the array. Which is basically what i'm doing now. And is probably what blitz was doing behind my back

Cheers.

• [b][red]This message was edited by AsmGuru62 at 2003-11-4 7:50:12[/red][/b][hr]
: : : Hi.
: : :
: : : I'm new to game programming in c and just wondering how to do this...
: : :
: : : In blitzbasic there is types which are basically structures. With the types we can create as many as we like with the same name and then have a for loop that will do the same thing to each of the types with that name.
: : : eg... (can't remember the exact commands in blitz. it's been a while)
: : :
: : : --------------
: : :
: : : Type THING
: : : field x
: : : field y
: : : End Type
: : :
: : : for i = 0 to 20
: : : New THING thing
: : : next
: : :
: : : for each(thing)
: : : thing.x = 10
: : : next
: : :
: : : --------------
: : :
: : : This would create 21 things and then set their x's to 10.
: : :
: : : Can you do something like this in c? I want to do this for bullets.
: : :
: : : Thanks in advance.
: : :
: : :
: : [blue]You can do everything in "C":[/blue]
: : [code]
: : typedef struct
: : {
: : int x;
: : int y;
: : } THING;
: :
: : #define NEW(type) ((type*) malloc (sizeof (type)))
: :
: : THING* array [20];
: :
: : // Why use two loops? - if you can do it in one!
: : int iThing;
: :
: : for (iThing=0; iThing<20; iThing++)
: : {
: : array [iThing] = NEW (THING);
: : array [iThing]->x = 10;
: : }
: : [/code]
: :
:
: Thanks for the reply mate.
:
: But won't that only let me have 20 (whatever number you choose) "thing"'s in the array? That is kinda what i'm doing at the moment but can you have it so that you don't have to say how big the array is so you can just keep adding more bullets if needed?
:
: In blitz you can have it so that you hit the fire button and it basically does, struct BULLET bullet, and it will let you name any number of types the same thing for use in a for loop like, for each(bullet), and the loop will loop for each bullet you have created. Also you can delete any one of the bullet's and it gets rid of it completly whereas with an array i guess you would just have like
:
: bullet[1]->active = 0;
:
: and then you would have to check which bullets aren't active so you can use that bit of the array. Which is basically what i'm doing now. And is probably what blitz was doing behind my back
:
:
: Cheers.
:
:
:
:
[blue]So, you need a dynamic (growable) array - just create an object for that, like so:
[code]
typedef struct
{
void** m_pArray;
int m_iCount; // How many 'void*' inside 'm_pArray'
int m_iAllocated; // How much room inside 'm_pArray'
int m_iGrowBy; // How much to grow when needed
}
DYNARRAY;
[/code]
...and then add some functions which receive the pointer to DYNARRAY and some additional parameters, to work with data inside 'm_pArray':
[code]
#define NEW(type) ((type*) malloc (sizeof (type)))
#define NEWARR(type,items) ((type*) malloc (sizeof (type) * (items)))

DYNARRAY* NewArray (int iGrow)
{
DYNARRAY* pObject = NEW (DYNARRAY);

if (pObject) {
pObject->m_iGrowBy = iGrow;
pObject->m_iAllocated = iGrow;
pObject->m_iCount = 0; // Nothing added yet
pObject->m_pArray = NEWARR (void*, iGrow);
}
return pObject;
}

void Array_Add (DYNARRAY* pArray, void* pThing)
{
if (pArray->m_iCount == pArray->m_iAllocated) {
// Full. Must reallocate...
pObject->m_pArray = (void**) realloc (
pObject->m_pArray,
sizeof (void*) * (pArray->m_iAllocated + pArray->m_iGrowBy));

if (! pObject->m_pArray) {
return; // No more memory
}
// Re-Allocated OK.
pArray->m_iAllocated += pArray->m_iGrowBy;
}
pObject->m_pArray [pArray->m_iCount] = pThing;
(pArray->m_iCount)++;
}
[/code]
This way - whenever you call 'Array_Add()' - the array will be automatically re-allocated (if needed). The bigger the value 'm_iGrowBy' - the better performance of the array.

Sorry, if it looks complicated - it is - once you want a flexibility in "C" - you need to do structures. And you need macros to understand the code better (and write it faster too...)

Cheers!
[/blue]

• : [b][red]This message was edited by AsmGuru62 at 2003-11-4 7:50:12[/red][/b][hr]
: : : : Hi.
: : : :
: : : : I'm new to game programming in c and just wondering how to do this...
: : : :
: : : : In blitzbasic there is types which are basically structures. With the types we can create as many as we like with the same name and then have a for loop that will do the same thing to each of the types with that name.
: : : : eg... (can't remember the exact commands in blitz. it's been a while)
: : : :
: : : : --------------
: : : :
: : : : Type THING
: : : : field x
: : : : field y
: : : : End Type
: : : :
: : : : for i = 0 to 20
: : : : New THING thing
: : : : next
: : : :
: : : : for each(thing)
: : : : thing.x = 10
: : : : next
: : : :
: : : : --------------
: : : :
: : : : This would create 21 things and then set their x's to 10.
: : : :
: : : : Can you do something like this in c? I want to do this for bullets.
: : : :
: : : : Thanks in advance.
: : : :
: : : :
: : : [blue]You can do everything in "C":[/blue]
: : : [code]
: : : typedef struct
: : : {
: : : int x;
: : : int y;
: : : } THING;
: : :
: : : #define NEW(type) ((type*) malloc (sizeof (type)))
: : :
: : : THING* array [20];
: : :
: : : // Why use two loops? - if you can do it in one!
: : : int iThing;
: : :
: : : for (iThing=0; iThing<20; iThing++)
: : : {
: : : array [iThing] = NEW (THING);
: : : array [iThing]->x = 10;
: : : }
: : : [/code]
: : :
: :
: : Thanks for the reply mate.
: :
: : But won't that only let me have 20 (whatever number you choose) "thing"'s in the array? That is kinda what i'm doing at the moment but can you have it so that you don't have to say how big the array is so you can just keep adding more bullets if needed?
: :
: : In blitz you can have it so that you hit the fire button and it basically does, struct BULLET bullet, and it will let you name any number of types the same thing for use in a for loop like, for each(bullet), and the loop will loop for each bullet you have created. Also you can delete any one of the bullet's and it gets rid of it completly whereas with an array i guess you would just have like
: :
: : bullet[1]->active = 0;
: :
: : and then you would have to check which bullets aren't active so you can use that bit of the array. Which is basically what i'm doing now. And is probably what blitz was doing behind my back
: :
: : Sorry if i have misunderstood your answer or my question.
: :
: : Cheers.
: :
: :
: :
: :
: [blue]So, you need a dynamic (growable) array - just create an object for that, like so:
: [code]
: typedef struct
: {
: void** m_pArray;
: int m_iCount; // How many 'void*' inside 'm_pArray'
: int m_iAllocated; // How much room inside 'm_pArray'
: int m_iGrowBy; // How much to grow when needed
: }
: DYNARRAY;
: [/code]
: ...and then add some functions which receive the pointer to DYNARRAY and some additional parameters, to work with data inside 'm_pArray':
: [code]
: #define NEW(type) ((type*) malloc (sizeof (type)))
: #define NEWARR(type,items) ((type*) malloc (sizeof (type) * (items)))
:
: DYNARRAY* NewArray (int iGrow)
: {
: DYNARRAY* pObject = NEW (DYNARRAY);
:
: if (pObject) {
: pObject->m_iGrowBy = iGrow;
: pObject->m_iAllocated = iGrow;
: pObject->m_iCount = 0; // Nothing added yet
: pObject->m_pArray = NEWARR (void*, iGrow);
: }
: return pObject;
: }
:
: void Array_Add (DYNARRAY* pArray, void* pThing)
: {
: if (pArray->m_iCount == pArray->m_iAllocated) {
: // Full. Must reallocate...
: pObject->m_pArray = (void**) realloc (
: pObject->m_pArray,
: sizeof (void*) * (pArray->m_iAllocated + pArray->m_iGrowBy));
:
: if (! pObject->m_pArray) {
: return; // No more memory
: }
: // Re-Allocated OK.
: pArray->m_iAllocated += pArray->m_iGrowBy;
: }
: pObject->m_pArray [pArray->m_iCount] = pThing;
: (pArray->m_iCount)++;
: }
: [/code]
: This way - whenever you call 'Array_Add()' - the array will be automatically re-allocated (if needed). The bigger the value 'm_iGrowBy' - the better performance of the array.
:
: Sorry, if it looks complicated - it is - once you want a flexibility in "C" - you need to do structures. And you need macros to understand the code better (and write it faster too...)
:
: Cheers!
: [/blue]
:
:
:

That is spot on the money mate

I even understand it all.

Cheers for that.