organising my code..

Started by
1 comment, last by yacwroy 14 years, 6 months ago
Im writing a program that loads a .obj file (3d model). Im making one class called model to store all the information.. but in the model class I got some structs to store some of the data in. Im also writing a reader class that will be used to read the .obj file and save the info to a model class.. inside the reader class I want to be able to set up a temporary struct of the ones I got insde the the model class.. how should I organize this? I was thinking of making seperate classes for the different structs, but since manipulation of the data is done in the model and reader classes I guess they should still be structs? Could I make a struct.hpp and then include that file into the classes where I need to use them? Whats the best design for this?
Advertisement
I ran into the same situation actually, I had a Vertex, Normal, TexCoord, and some other struct classes with just like 2/3 fields each, and I have multiple model loader classes, so would it be more organized for each to have its own file, or since they are so small put them all into 1 file that contains all the miscellaneous structs/classes?
Are you thinking of something like the following?
(Using Vertex and Color as the structs)

vertex.hpp:
struct Vertex  {    ...  };

color.hpp:
struct Color  {    ...  };

model.hpp:
#include "vertex.hpp"#include "color.hpp"class Model  {    ...    std::vector<Vertex> m_vertices;    std::vector<Color> m_colors;  };

model.cpp:
#include "model.hpp"Model::do_something()  {    ...  }

reader.hpp:
#include "model.hpp"class Reader  {    public:      Model read(std::istream data);    ...    private:      std::vector<Vertex> m_temp_vertices;      std::vector<Vertex> m_temp_colors;  };

reader.cpp:
#include "reader.hpp"Model Reader::read(std::istream data)  {    ...  }


Quote:inside the reader class I want to be able to set up a temporary struct of the ones I got insde the the model class.. how should I organize this?
Quote:Could I make a struct.hpp and then include that file into the classes where I need to use them?
Here, reader.hpp/cpp don't have to include all the individual structs because model.hpp includes them. So if a file just needs Vertex, it #includes "vertex.hpp". If it needs a Model and all associated structs, it #includes "model.hpp".

Personally, I like 1000 tiny files in my source path. It's weighty but very modular. And it's good for avoiding circular includes. It's also possibly easier for other people to find your class definitions.

Quote:I was thinking of making seperate classes for the different structs
I know many people including me reserve struct for PoD types or have some similar rule. Whatever you do, be consistent.


Not sure if I understood what you're asking 100% of the time, though.

This topic is closed to new replies.

Advertisement