Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMarkS

Posted 25 December 2012 - 03:08 PM

I'm in the middle of writing my first tile engine in C++. I've stuck with C for years and I find it easier. Now that I am switching to C++, I am finding that simple matters are suddenly not so simple.

 

My problem arises from the need to use a resource manager and the fact that global variables are a bad thing in C++. In C, I would just make global arrays of textures and what not and the various functions could access them at will. I'm at a loss how to do this in C++.

 

My design is as such:

 

 

class mesh{
//Stuff
};
 
class sprite{
//Stuff
mesh *sprite_mesh;
};
 
class Tile{
//Stuff
sprite *tile_sprite;
};
 
class tile_layer{
//Stuff
std::vector<tile *> tiles;
std::vector<sprite *> sprites;
};
 
class tile_level{
//Stuff
std::vector<tile_layer *> tile_layers;
resource_manager<texture> *textures;
resource_manager<shader> *shaders;
};

 

 

Of course, the actual code is a bit more involved. What I am having trouble with is how to access the resource managers located in the tile_level class from the mesh class. Regardless if the resources are loaded during the construction of the tile_level object or during the loading of the individual meshes, when I call the mesh's render function, I need it to be able to access the resource managers.

 

The only way I can think of is to pass a pointer to the managers up the chain through the constructors, however, this would require needless pointers to the managers in the tile_layer and sprite classes. There has got to be a simple way to do this, and I know it is my unfamiliarity with C++ that is making this difficult.

 

How would I go about this in a C++-correct manor?

 

...Don't know why there is an asterisk in the titlehuh.png... 


#1MarkS

Posted 25 December 2012 - 02:50 PM

I'm in the middle of writing my first tile engine in C++. I've stuck with C for years and I find it easier. Now that I am switching to C++, I am finding that simple matters are suddenly not so simple.

 

My problem arises from the need to use a resource manager and the fact that global variables are a bad thing in C++. In C, I would just make global arrays of textures and what not and the various functions could access them at will. I'm at a loss how to do this in C++.

 

My design is as such:

 

class mesh{
//Stuff
};
 
class sprite{
//Stuff
mesh *sprite_mesh;
};
 
class Tile{
//Stuff
sprite *tile_sprite;
};
 
class tile_layer{
//Stuff
std::vector<tile *> tiles;
std::vector<sprite *> sprites;
};
 
class tile_level{
//Stuff
std::vector<tile_layer *> tile_layers;
resource_manager<texture> *textures;
resource_manager<shader> *shaders;
};

 

Of course, the actual code is a bit more involved. What I am having trouble with is how to access the resource managers located in the tile_level class from the mesh class. Regardless if the resources are loaded during the construction of the tile_level object or during the loading of the individual meshes, when I call the mesh's render function, I need it to be able to access the resource managers.

 

The only way I can think of is to pass a pointer to the managers up the chain through the constructors, however, this would require needless pointers to the managers in the tile_layer and sprite classes. There has got to be a simple way to do this, and I know it is my unfamiliarity with C++ that is making this difficult.

 

How would I go about this in a C++-correct manor?


PARTNERS