I'm working on an asset manager for my game engine and I'm still new to C++ so I'm not super interested in doing anything fancy and optimization and all that really just trying to get something that works, however, what I currently have just doesn't sit right with me so although it does work I don't think it's right to leave it as is there's just a bit to more duplication/repetition going on.
// Assets
struct Mesh {};
struct Texture {};
struct Shader {};
struct Material {};
struct Audio {};
// Asset metadata (filepath, asset instance, modified time)
struct MeshAsset {};
struct TextureAsset {};
struct ShaderAsset {};
struct MaterialAsset {};
struct AudioAsset {};
// Asset containers
std::unordered_map<unsigned int, MeshAsset> meshes;
std::unordered_map<unsigned int, MeshAsset> textures;
std::unordered_map<unsigned int, ShaderAsset> shaders;
std::unordered_map<unsigned int, MaterialAsset> materials;
std::unordered_map<unsigned int, AudioAsset> audios;
// Functions to get and load assets
unsigned int LoadMesh() {}
MeshAsset GetMesh(unsigned int id) {}
MeshAsset GetMesh(const std::string& filepath) {}
unsigned int LoadTexture() {}
TextureAsset GetTexture(unsigned int id) {}
TextureAsset GetTexture(const std::string& filepath) {}
unsigned int LoadShader() {}
ShaderAsset GetShader(unsigned int id) {}
ShaderAsset GetShader(const std::string& filepath) {}
unsigned int LoadMaterial() {}
MaterialAsset GetMaterial(unsigned int id) {}
MaterialAsset GetMaterial(const std::string& filepath) {}
unsigned int LoadAudio() {}
AudioAsset GetAudio(unsigned int id) {}
AudioAsset GetAudio(const std::string& filepath) {}
Ideally I'd like to be able to shorten this code to something like this.
// Asset Instance
struct Mesh {};
struct Texture {};
struct Shader {};
struct Material {};
struct Audio {};
// Asset metadata
struct MeshAsset {};
struct TextureAsset {};
struct ShaderAsset {};
struct MaterialAsset {};
struct AudioAsset {};
// Asset containers
std::unordered_map<unsigned int, /* ??? */> assets;
// Functions to get and load assets
unsigned int LoadMesh() {}
unsigned int LoadTexture() {}
unsigned int LoadShader() {}
unsigned int LoadMaterial() {}
unsigned int LoadAudio() {}
/* ??? */ GetAsset(unsigned int id) {}
/* ??? */ GetAsset(const std::string& filepath) {}
I believe templates and/or inheritance comes in handy for this although it's not apparent how to apply those concepts to refactor this code.