I'm programming a game engine and having an issue with the design of a function. The function will search a directory for a specific type of file, then use those files to build the proper graphics components.
For example, lets say I have the following:
Folder directory:
...\data\shapes\
files in 'shapes':
circle.shp, square.shp, and readme.txt
The shape components will be built using the .shp files.
A call to the function would look like this:
LoadResources("data\\shapes\\", ".shp", Factory::BuildShape);
The graphics components
1 2 3 4 5 6 7
|
struct Component {};
class Shape : public Component {};
class Camera : public Component();
class Light : public Component();
class Mesh : public Component();
class Material : public Component();
class Texture : public Component();
|
Here are the functions used to build the various components.
1 2 3 4 5 6
|
Shape* const Factory::BuildShape(std::string _file);
Camera* const Factory::BuildCamera(std::string _file);
Light* const Factory::BuildLight(std::string _file);
Mesh* const Factory::BuildMesh(std::string _file);
Material* const Factory::BuildMaterial(std::string _file);
Texture* const Factory::BuildTexture(std::string _file);
|
LoadResources() takes a function pointer as a 3rd parameter.
The function passed is used to construct the components.
1 2
|
void Graphics::LoadResources(std::string _dir, std::string _ext,
Component* (*BuildComponent)(std::string))
|
Would like to make calls like this.
1 2 3
|
LoadResources("data\\shapes\\", ".shp", Factory::BuildShape);
LoadResources("data\\meshes\\", ".fbx", Factory::BuildMesh);
LoadResources("data\\lights\\", ".lit", Factory::BuildLight);
|
At first I thought it would work due to the inheritance, but
it wont work because the function signatures don't match.
BuildShape() returns a Shape*
function pointer returns a Component*
I could make all the Factory::Build functions return Component*, doesn't really make sense though does it? When I call BuildShape(), I would expect it to return the entire Shape, not just the component part.
Any solutions or alternatives would be appreciated.