Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading a modelfile once.

Started by kenwi Aug 1, 2004 at 12:21 PM 2 replies 900+ views
Original Post
kenwi
kenwi
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.

cAsteroid3d::cAsteroid3d( char *filename, char *texturename )
{
  M.Open( filename );
};

This is how the model is read.

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());
};
Thanks in advance, I hope any of you can enlighten me a little on this topic.
vovansim
vovansim
Hi,

There are several ways to do that. :)

The easiest would be to put a static member variable into your cAsteroid3d class, which would store the model. Then, you only initialize that variable once, and then use it over again when you create asteriods.

The problem with that approach would be that every asteriod has to look the same then.

A more flexible approach would be to code a simple resource manager class. Basically, you could have a class, MeshManager, for instance, and inside it you would have a map, or a hashtable. You'd then use filenames as the keys. Basically, the class would be something like this:

class MeshManager:  private Map loadedMeshes;  public Mesh* getMesh(char* filename);


And then the pseudo-code for the getMesh function would look like this:

public Mesh* getMesh(char* filename) {  if (loadedMeshes.containsKey(filename)) {    return loadedMeshes.get(filename);  } else {    Mesh* result = new Mesh();    result->open(filename);    loadedMeshes.put(filename, result);    return result;  }}


You could use std::map for the hashtable there. :)

Hope that helps,

Vovan
Vovan
snk_kid
snk_kid
Well there are probably a millions ways you could do it. One simple way could be in your cAsteroid3d type use a pointer/reference to a mesh instead of an instance of one e.g.

class mesh;class cAsteroid3d {public:   typedef mesh* mesh_ptr;private:   mesh_ptr _m;public:   cAsteroid3d(mesh& m): _m(&m) {}   ~cAsteroid3d() { _m = 0; }};


[Edited by - snk_kid on August 1, 2004 1:53:59 PM]
kenwi
kenwi
Thanks to you both for your answers, I tested out snk_kid's suggestion and it worked nicely. I might also check out the mesh manager solution later.

Thanks again! Greatly appreaciated!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.