Sign in to follow this  
staticVoid2

best way to find number of meshes in .x

Recommended Posts

hi, I'm nearly finished writing a .x file parser but have ran into a problem, for each frame template there can be any amount of nested mesh templates but the frame template does not specify how much there are. what would be the best way to find the number so that I can allocate memory for them? I've considered using dynamic arrays where if I encountered a mesh template I would simply add it to the array. Or save the file position, scan ahead and save the number of occurences of the word "Mesh" and then use this value to allocate memory?

Share this post


Link to post
Share on other sites
It certainly sounds like the best way is to use a vector<> or list<> and let the system allocate memory. Then, if you later need to know the number of meshes, you can call vector<>.size().

If need be, you can create separate meshes from each vector entry and eventually call vector<>.clear() to release the memory.

Share this post


Link to post
Share on other sites
I didn't really want to use dynamic arrays during rendering so I was just going to use them to load the meshes and then copy the static array of meshes and the number of meshes from it to the frame object, heres what ive got so far:


#pragma once



template <class T>
class DArray
{
public:
DArray(void);
~DArray(void);

void add(T object);
T get(int index);
T * getStaticArray();

unsigned int tracker;
private:
T * main;
static const unsigned int INITIAL = 10;
unsigned int size;
};




template <class T>
DArray<T>::DArray(void)
{
main = new T[INITIAL];
tracker = 0;
size = INITIAL;
}

template <class T>
DArray<T>::~DArray(void)
{
}

template <class T>
void DArray<T>::add(T object)
{
if(tracker == size) {
main = new T[size += INITIAL];
// copy array
}
main[tracker++] = object;
}

template <class T>
T DArray<T>::get(int index)
{
return main[index];
}


template <class T>
T * DArray<T>::getStaticArray()
{
return main;
}







in the function I use to process a frame template I just create a temp DArray and then pass a reference to this to the function for processing a mesh, the mesh function creates a temp mesh object and once initiated adds this mesh to the array. once the frame template is finished processing, I use:

frame->nMeshes = DArray.tracker;
frame->meshes = DArray.getStaticArray();

Somthings not working though, I'll have to go over the code to see what it is, but would this be an efficient way of doing this?

Edit:

I know ive forget to add code for copying the array, but the file im testing only has somthing like 2/3 meshes for the frame so it wouldn't matter.

Share this post


Link to post
Share on other sites
I'm afraid I don't see much difference between your array class and a standard library class. It appears you're duplicating the functionality of standard classes. I would think using library classes is much more efficient.

Also, each "new" should have a corresponding "delete" somewhere.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this