Handling resources
This one might be a bit longer but maybe you can point me in an direction? I wounder how to efficiently store my audio, graphics ect within the game so that all that needs have access to it. Right now I'm going with a singleton, but my main problem here is I don't know how I should write the code that determines wheither an graphical resources should be unloaded or keept in memory, like for example if you switch between "States/Screens" I want to keep the resources that are loaded the most so that I don't do unload the imidiatly load them again.
With idiomatic C++, you use RAII to manage resources. That is, you have classes that store resources and are responsible for their lifetime. You should not use singletons and global variables, they bring a lot of problems. A good design mostly makes them unnecessary.
For example, you can store your textures in an associative STL container like
std::map. The key type is some kind of ID to refer to the texture, the mapped type is the texture itself or a
std::unique_ptr<sf::Texture>. In the class that manages your resources, you initially load the resources. As long as they are used, your object stays alive. If it is destroyed, all resources are
automatically freed -- if you make it right, you need not define a destructor.
This is for the case where you have relatively deterministic lifetimes: You acquire assets at initialization time and release them at destruction. If you have dynamic requirements like resources that should be loaded on-demand, you can try the Thor.Resources module I have written (
tutorial and
API doc). But if you don't actually need the dynamic semantics, it tends to overcomplicate things