best way to find number of meshes in .x

Started by
2 comments, last by Buckeye 16 years, 1 month ago
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?
Advertisement
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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 oncetemplate <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.
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement