3

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.

NinjaGameDev
  • 73
  • 2
  • 7
  • 1
    What do you mean by a "static class"? A class with only static members? – molbdnilo Apr 24 '20 at 07:53
  • Yeah only static variables and static methods. – NinjaGameDev Apr 24 '20 at 07:54
  • 2
    I think the memory footprint of your ResourceManager class will be tiny compared to the memory footprint of the resources it is loading. So I wouldn't worry about creating multiple resource manager objects. – john Apr 24 '20 at 07:55
  • Resources Manager is static. So how I create multiple object from a static class. – NinjaGameDev Apr 24 '20 at 08:00
  • This seems like a problem with the class itself, where it only loads hard coded assets. Maybe you can make that part more dynamic? Maybe it would also help to see the actual class, so we get a better idea of what you are doing. – Lukas-T Apr 24 '20 at 08:08
  • What stops you from just unloading the stuff that you don't need anymore? – Timo Apr 24 '20 at 08:08
  • 4
    Please note that C++ also has `namespace`. Some "static classes" don't have to be a class at all. – nada Apr 24 '20 at 08:11

2 Answers2

2

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.

merlinND
  • 799
  • 8
  • 15
  • It is just different syntax for mostly the same thing. And that doesn't solve OP issue to have one instance by level for automatic de-allocation... – Jarod42 Apr 24 '20 at 10:18
2

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.

  1. Create a stateless pipeline for loading your assets from permanent storage (disk). This could be a static class or just a collection of namespace functions. This pipeline is only for loading your assets. It doesn't take any ownership or manage the life time of those assets.
  2. Create a stateful resource cache. This could be a std::vector<MyTextures> or a custom type that holds all the resources/assets you need for a particular level.
  3. Add an instance of that resource cache to the class that represents your level and load the resources (using the pipeline from step 1) into the cache when initializing your level (could be in the constructor or any initialize or setup function).
  4. Create a static (maybe global) instance of that resource cache type. In this object you can store everything that isn't level related and outlives your levels (like player textures, sound effects, shared assets, ...).

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).

Timo
  • 9,269
  • 2
  • 28
  • 58