my model loader essentially looks like this:
class model //this is our model class, it contains dynamic vert and linedef arrays.
{
public:
unsigned numl;
unsigned numv;
vec* verts;
unsigned* linea;
unsigned* lineb;
model( char file[32] ) //model objects that use this constructor are usually ones that are left in model space and are copied into other model objects to be transformed
{
numl = 0;
numv = 0;
verts = NULL;
verts_ms = NULL;
linea = NULL;
lineb = NULL;
unsigned msize = 256; //space for 256 objects will be allocated, if filled then it will allocate space for another 256 objects
cout << "Loading model: " << file << endl;
verts = (vec *)malloc(msize * sizeof(vec)); //now that we have our sizes of our defs, set length of dynamic arrays.
verts_ms = (vec *)malloc(msize * sizeof(vec));
linea = (unsigned *)malloc(msize * sizeof(unsigned)); //allocate initial block for line defs
lineb = (unsigned *)malloc(msize * sizeof(unsigned));
if (verts == NULL || verts_ms == NULL || linea == NULL || lineb == NULL)
{
cout << "Memory allocation error, aborting model load." << endl;
free(verts);
free(verts_ms);
free(linea);
free(lineb);
numl = numv = 0;
return;
}
char stage = 0;
char data[32];
ifstream nlm(file);
if(!nlm.is_open()) //idiot proofing
{
cout << "Couldnt open file: " << file << endl;
return;
}
while(!nlm.eof() && stage < 4) //loop through file word by word, allocate more memory as neccisary and load data
{
if(stage != 4)
nlm >> data;
if(stage==0) //find the vertdef start string
{
if(strncmp(data, "vertdefs", 8)==0)
stage++;
}
else if(stage==1) //count data
{
if(strncmp(data, "end", 3)==0)
{
stage++; //once we see the end we stop counting and up our stage
msize = 256; //reset this so we can allocate line space as needed
}
else
{
if (numv == msize)
{
msize += 256;
verts = (vec *)realloc(verts, msize * sizeof(vec));
verts_ms = (vec *)realloc(verts_ms, msize * sizeof(vec));
cout << "Too many verts, reallocating memory." << endl;
if (verts == NULL || verts_ms == NULL)
{
cout << "Memory allocation error, aborting model load." << endl;
free(verts);
free(verts_ms);
free(linea);
free(lineb);
numl = numv = 0;
return;
}
}
istringstream fdat(data);
fdat >> verts[numv].x;
nlm >> verts[numv].y >> verts[numv].z;
verts_ms[numv] = verts[numv];
cout << "Vert " << numv << " set to " << verts[numv].x << ", " << verts[numv].y << ", " << verts[numv].z << endl;
numv++;
}
}
else if(stage==2) //find linedef start string
{
if(strncmp(data, "linedefs", 8)==0)
stage++;
}
else if(stage==3) //count more data
{
if(strncmp(data, "end", 3)==0)
stage++; //once we see the end we stop counting and up our stage
else
{
if (numl == msize)
{
msize += 256;
linea = (unsigned *)realloc(linea, msize * sizeof(unsigned));
lineb = (unsigned *)realloc(lineb, msize * sizeof(unsigned));
cout << "Too many line defs, allocating more memory." << endl;
if (linea == NULL || lineb == NULL)
{
cout << "Memory allocation error, aborting model load." << endl;
free(verts);
free(verts_ms);
free(linea);
free(lineb);
numl = numv = 0;
return;
}
}
istringstream idat(data);
idat >> linea[numl];
nlm >> lineb[numl];
cout << "Vert indices for line " << numl << " set to: " << linea[numl] << ", " << lineb[numl] << endl;
numl++;
}
}
}
verts = (vec *)realloc(verts, numv * sizeof(vec)); // compact the size of the memory blocks
verts_ms = (vec *)realloc(verts_ms, numv * sizeof(vec));
linea = (unsigned *)realloc(linea, numl * sizeof(unsigned));
lineb = (unsigned *)realloc(lineb, numl * sizeof(unsigned));
if (verts == NULL || verts_ms == NULL || linea == NULL || lineb == NULL)
{
cout << "Memory allocation error, aborting model load." << endl;
free(verts);
free(verts_ms);
free(linea);
free(lineb);
numl = numv = 0;
return;
}
nlm.close();
cout << "Model load complete." << endl;
}
//the rest of the class goes here
so its sorta the same thing right?