Search code examples
c#xna

How to prevent duplicate copies of a loaded texture in XNA Game Studios (Design)


I have zones in my game world that are used to partition renderable areas based on their visibility and proximity to the user.

For simplicity, as such.

class Zone
{
   Textur2D _texture;
   public Zone(...)
   {
      _texture = Content.Load<Texture2D>("Textures\\a");
   }
}

Currently, this structure has _texture as a member of the Zone class. Now, if I need to render, say, 20 zones. I will have to load 20 copies of what could possibly be the same texture.

From my point of view, this seems very inefficient memory wise.

I looked into the ContentManager (called Content above) class on MSDN and around the net, it looks like it has an Unload() feature. Leading me to believe that it is a centralized source for the texture data, meaning that all of the loaded texture's in my hypothetical 20 zones all point to the same reference texture and will all dispose when I call Unload().

Is this the case? Or should I implement a ContentCache to load all of these textures and have my zones point to that centrally loaded texture that they all share?


Solution

  • Yes, ContentManager handles caching of textures and prevents needless reloading. You can find more information here: https://stackoverflow.com/a/9870420/1141432