Original Post
Good evening. I'm making a small arkade space-shooter, game's running just fine, but I got aware of a big mistake I've done when writing the model loading routines. Ie: I've got asteroids coming towards the player, and you've got to shoot them. All asteroids are seperate objects, when a new asteroid object is created it reads the modelfile to get modeldata. In my test, I spawn a new asteroid every 10 second, I notice a little delay everytime it spawns caused by the reading of the file. "Easy" I thought, I just load the modelfile once when the game starts. Then store the modeldata in memory and just assign it to the object when created. The problem is I seem to be unable to work out how I do this. I sat all last night programming to sort out this problem, but I were unable to solve the problem. So, can anyone pinpoint me to a solution for this? A code example would be _very_ nice. Constructor for the asteroid class. This is how the model is read. Thanks in advance, I hope any of you can enlighten me a little on this topic.
cAsteroid3d::cAsteroid3d( char *filename, char *texturename )
{
M.Open( filename );
};
void Mesh::Open(char *filename)
{
if(strcmp(filename+strlen(filename)-4,"rawc")==0 ||
strcmp(filename+strlen(filename)-4,"RAWC")==0 ){
OpenColor(filename);
return ;
}
FILE *fp;
fp=fopen(filename,"r");
if(fp==0) {
printf("Unable to open %s\n",filename);
// exit(-1);
}
has_color=false;
Triangle tt;
T.clear();
float xf,yf,zf;
int nc;
while(1)
{
nc=fscanf(fp,"%f %f %f",&xf,&yf,&zf);
if(nc!=3) break;
tt.v[0].v=Point3f(xf,yf,zf);
nc=fscanf(fp,"%f %f %f",&xf,&yf,&zf);
if(nc!=3) break;
tt.v[1].v=Point3f(xf,yf,zf);
nc=fscanf(fp,"%f %f %f",&xf,&yf,&zf);
if(nc!=3) break;
tt.v[2].v=Point3f(xf,yf,zf);
tt.CalcNormal();
T.push_back(tt);
}
fclose(fp);
printf("Read %i triangles\n",T.size());
};