Sign in to follow this  
chillypacman

loading things in...

Recommended Posts

I'm kind of stuck with the idea of loading objects and stuff into a game. Lets say I have an engine written in C++ that has a class called 'model' and model has a 'loadModel()' and 'renderModel()' function. If I create a vector of type 'model' and write the vector into a file (lets say model.dat) then create another class called 'readModel' that reads from model.dat. Can I through model.dat successfully invoke loadModel() and renderModel()? If not is there another way? Would I need to lets say take the attributes of model and store them individually and seperately in model.dat then load model.dat and run functions that will actually load/render the model? For instance if model has only one variable, a string called 'name', will I have to write name into model.dat and use a seperate function (lets say 'loadModelFromFile()') to extract name from model.dat and load it via another function such as 'loadModel(string name)' (for instance)?

Share this post


Link to post
Share on other sites
Quote:
Original post by chillypacman
If not is there another way? Would I need to lets say take the attributes of model and store them individually and seperately in model.dat then load model.dat and run functions that will actually load/render the model? For instance if model has only one variable, a string called 'name', will I have to write name into model.dat and use a seperate function (lets say 'loadModelFromFile()') to extract name from model.dat and load it via another function such as 'loadModel(string name)' (for instance)?


Yes! You store data in the file, then you load the data and process it. However you could of course have some kind of scripting in your data file and parse it for actions. But traditionally you would just read in data and your function will have to know what to do with it. A very basic example could be:

model.dat content:

My Model
3
1,2,3
4,2,5
3,6,7
flower.jpg


Then the code which loads it might look something like this in psuedo-code:

model loadModelFromFile(filename)
OpenFile(filename)
string model_name = ReadLine(string)
int num_vertices = ReadLine(int)
list<vector3> vertices(num_vertices)

for i = 1 to num_vertices
vertices.add(ParseVector(ReadLine(string)))

string texture_name = ReadLine(string)
handle texture_handle = LoadTexture(texture_name)

return LoadModel(vertices, texture_handle)
end



However I recommend you use a file format such as XML, since it makes file parsing so much easier if you use one of the many free libraries out there.

Share this post


Link to post
Share on other sites
Serialization is often used to write data structures like a model to a file.
Its kind of complex so I won't dive into that.

The method you are describing will work under certain conditions:
1.
The data structure for a model does not contain any pointers to data.
If you think about it this is kindof obvious. If you save a pointer to a file, the address the pointer points to is stored in the file, not the data is points to.
2.
You have a fixed number of models to save and load.

This is the simplest case I can imagine. You could simply write the models to the file and load them back in using a loop.

If you have a vector of X models you want to store in the file, you can get away with writing the number of models (X) to the beginning of the file before writing the models, and read that number back out before loading the models themself.

If the model data structure contain pointers to data, or data structures from the c++ STL library, like std::string or std::list, this method will not work.
In that case you could look into serialization, or you could use a pre defined file format like XML.
There is a small library out there called tinyXML that you can use to load and store data in the XML format.

Share this post


Link to post
Share on other sites
I was thinking of creating a structure then writing a vector of the structure into a file.

After which I can load it into the program and pass the appropriate values to the appropriate functions. Would this idea work?

Share this post


Link to post
Share on other sites
Sorry I'm not quite sure what you mean here.
Do you mean writing a wrapper class for the class in the vector and storing those wrappers to file?
Or do you mean writing a header class/structure for the vector of classes you want to store to file?
Or writing a class with functions that can store/load your model class to/from the file?

Please elaborate...

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