Archived

This topic is now archived and is closed to further replies.

Function to handle verying number of arrays

This topic is 5625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I am working on a 2d DirectDraw Game engine and I have a little problem. The way I have it set up is like this: I have a struct called FRAME that is simply a pointer to a DirectDraw surface. I create an animation by saying FRAME anim_shoot[6] etc... then I have a function to load the frames in: LoadFrames(char *file_name, bla bla all the info I need. The last part of my function is where I give the name of the animation. In this case I would pass anim_shoot as an argument. Depending on the number for frames that I pass to it I want the function to loop through a certain number of times and extract the info from the bitmap and store it in the corresponding frame. But I have run into problem. How does my function know whether the array is anim_shoot[3] or anim_shoot[67]... how can I test that array value to see if the animation has run out in the array. Basically I am asking how I can test to find out the size of an array. Whether it be [67] or [180] - Thanks

Share this post


Link to post
Share on other sites
You can''t in any easy manner. If you really wanted you could set up a dummy record at the end of the array that''d signal the end, or a member variable to state how many frames are in the array. But by far the easiest method would be to simply pass the number of frames as a separate parameter.

Share this post


Link to post
Share on other sites
Unlike Java, C++ does NOT store the length of the array, in the primitive array structure. In your example, anim_shoot is simply a pointer to the 1st element in your array. So, to solve your problem, simply pass in as another argument to the function, the size of the array.

or...

use vectors, which has a size() method

Share this post


Link to post
Share on other sites
Why don''t you pass another parameter (unsigned int, maybe) which sends the array_count?

The way the arrays are handled in C/C++ is same as how a pointer is handled. An array is basically a constant pointer. So there is no way you can find the length of array array, unlike in pascal... were you programming in pascal before starting C/C++ ?

There is a way to find number of elements in an array (not pointer), it goes something like this...


  
unsigned int numbers[]
{
1, 2, 3, 5, 3
};

int count()
{
return (sizeof(numbers)/sizeof(numbers[0]));
}



But i don''t think you can use this technique in your case. As you can see the array defined is an automatic one. This technique is helpful since adding elements to array during development is easier, no need to change #define''s etc.

If you can give the exact function declaration, i will get a better picture of the situation ...

Share this post


Link to post
Share on other sites
Yeah,

thanks for the comments everyone.

I decided to do something like this, I think it works well:

FRAME anim_shoot[7]; //the last frame is always NULL
anim_shoot[6]=NULL;

//inside the function:

while(anim!=NULL)
{
extractCell
etc...
}

-thanks

Share this post


Link to post
Share on other sites