I am making my first small mobile game in C# XNA. Lets say I have 3 screens, the main menu, options and game screen. A single game session usually lasts for 1 min, so the user will alternate frequently between the main menu and game screen. Therefore, once I load the textures for either screen, I want to keep them in memory to avoid frequent reloading. Both screens share some assets like their background textures, but differ in others.
The first solution I came up with is making 2 texture factory classes, MainScreenAssetFactory and GameScreenAssetFactory, each with their own content manager, and ill store them in a globally accessible point so that they persist after either screen is destroyed. There is also a OptionsScreenAssetFactory, but that I dont want to cache it since the options screen is rarely visited.
A typical Factory would look something like this
public class MainScreenAssetFactory
{
private readonly ContentManager contentManager;
public MainScreenAssetFactory(IServiceProvider serviceProvider, string rootDirectory)
{
contentManager = new ContentManager(serviceProvider) { RootDirectory = rootDirectory };
}
public Texture2D ListElementBackground
{
get { return return contentManager.Load<Texture2D>("UserTab"); }
}
public Texture2D ListElementBulletPoint
{
get { return return contentManager.Load<Texture2D>("TabIcon"); }
}
public Texture2D LoggedOutUser
{
get { return return contentManager.Load<Texture2D>("LoggedOutUser"); }
}
}
Since both Main, Options and Game Screen share some common resources, instead of loading them more than once, I created another class CommonAssetTexFactory which holds the common stuff and stays in-memory during the app lifetime. For example, this class gets passed to the options screen when it is created.
However, given my small game with its few assets, I am already finding this solution cumbersome and inflexible. Changing anything would require looking to see if its already in the common factory, and if not, modifying existing factories and so on. And this is just considering textures currently, i didnt add sound files yet. I cant imagine bigger games with thousands of resources using this approach. A better idea must exist. Would someone please enlighten me?