Reputation: 93
I am making a small game in my small game engine made with OpenGL and C++. I am using a static class called ResourceManager in my game which is responsible for loading and returning textures, shaders, audios, etc. It is static so I can get textures, shaders, audios, etc. in any class like player without initializing it and it is super easy to asses it. But what if i want different textures and audios for different levels, I have to carry all the previous level loaded textures and sounds to next level and keep adding them. And I think it is not a good practice. I should load needed textures and audios for that level and when going to next level delete all textures and audios and load new textures and audios for that level. This will keep my memory small. But i can not do this with static classes because they don't have constructor destructor.
Should I use non-static class to handle resources of different level.
I am very confused. Please tell me how i can do that and what I am doing wrong and how game developer solve the issue.
Sorry for very poor English. Thanks for reading.
Upvotes: 3
Views: 747
Reputation: 9825
I'm not a game developer so I'm not aware of any common practices addressing this issues specifically for games, but here is what I would do.
std::vector<MyTextures>
or a custom type that holds all the resources/assets you need for a particular level.initialize
or setup
function).This design has the advantage that (given it's implemented properly) resources are unloaded automatically once they're no longer needed. For example, if you finish a level, you also destroy your level object, and since the resource cache is part of the level object, those resources will also be destroyed (again, given it's implemented properly).
Upvotes: 2
Reputation: 855
The Singleton pattern seems appropriate for what you are trying to achieve.
It has the flexibility of a normal class, but the "global" ease of access of the static-only class you proposed.
Upvotes: 2