1
\$\begingroup\$

I want to load a texture when I need it, so I'd need a method in order to do so.

      LoadTexture(string fileName)

I'd only use that one parameter, but what would I put in the method? So far I've only ever loaded content at startup with something like

    content.Load<Texture2D>("blah");

But I want to move away from loading everything at startup and only load content when I need it.

EDIT: In order to explain what I'm going for, I'll give more detail. Say I have a chunk of blocks 50x50x50. Most of these won't be drawn, so it doesn't make sense to load the texture for the cubes not drawn. This is why I want a method that I can call whenever the texture needs to be loaded for the cube. Instead of loading 6 textures for 6 sides for 125,000 cubes, I cut back the amount of loading to only the visible cubes.

\$\endgroup\$
  • 1
    \$\begingroup\$ Put Content.Load<T>(string) in the method? \$\endgroup\$ – Dialock Jul 17 '13 at 16:28
  • \$\begingroup\$ This is a programming question that belongs on stackoverflow.com. \$\endgroup\$ – MichaelHouse Jul 17 '13 at 16:35
  • \$\begingroup\$ Dynamic loaders can be very complex beasts. See: floatingorigin.com/mirror/continuous-world.htm Perhaps you should do more research on ways to architect such a system by looking at how other games have done it. I think this is more of an architecture issue than anything. If this is for a game, I dont see why this cannot be here on gamedev. However, getting architecture advice does not really fit our Q&A format. Try hitting up the chat if you want to talk architecture and more opinion-based matters. Good luck \$\endgroup\$ – Dean Knight Jul 17 '13 at 16:45
  • \$\begingroup\$ that's going to be a pretty tricky thing to implement since you will have to load and unload textures dynamically. I guess the best I can suggest is for simplicity is check the distance between yourself and an object, if it is x units away load the texture, if it is y units away and has textures loaded unload the texture. Bear in mind this isn't exactly a great solution since really large far away objects might not fall in the load bounds, it's just something you can play with. \$\endgroup\$ – Benjamin Danger Johnson Jul 17 '13 at 17:20
  • \$\begingroup\$ oh also, loading and unloading textures is pretty expensive, so doing something like "only load what is visible" could crush your CPU if you have a lot of twists and turns in your game. \$\endgroup\$ – Benjamin Danger Johnson Jul 17 '13 at 17:22
1
\$\begingroup\$
public Texture2D LoadTexture(string fileName)
{
    return Content.Load<Texture2D>(fileName);
}

That would be the method, but I don't see why you wouldn't just call Content.Load()?

\$\endgroup\$
  • \$\begingroup\$ He wants a system that is more smart about when it loads content. ie) floatingorigin.com/mirror/continuous-world.htm It is more of an architecture thing as far as I can tell. \$\endgroup\$ – Dean Knight Jul 17 '13 at 16:49
  • 1
    \$\begingroup\$ @DeanKnight if he does, he should clarify that rather than us assuming that's what he's asking. \$\endgroup\$ – Outurnate Jul 17 '13 at 16:52
  • \$\begingroup\$ Yeah my wording was a little confusing there. \$\endgroup\$ – Christian Frantz Jul 17 '13 at 16:55
  • \$\begingroup\$ @ChristianFrantz Understandable. Perhaps you might edit the question to make it clearer for the future? \$\endgroup\$ – Outurnate Jul 17 '13 at 16:56
  • 2
    \$\begingroup\$ Alright I tried to explain it a little better \$\endgroup\$ – Christian Frantz Jul 17 '13 at 17:00
0
\$\begingroup\$

XNA implicitly supports what you're doing via it's GameComponent classes. Taking advantage of these will solve your issue, but then you're shoehorned into using their design approach.

The Game class -- which your main object loop inherits from -- has a handy property called Content, which is a reference to a ContentManager instance. This is all set up for you by default. Now what's cool is when you create an instance of a GameComponent, it asks for a Game object.

public class SomeCustomClass : GameComponent
{
    public SomeCustomClass(Game game)
        : base(game)
    {
    }
}

The above code shows the minimum implementation for a GameComponent. But because you pass an instance of your game loop, it can now see every public property in your Game, including Content, which is accessed via base.Game

So from there, you'd implement a method to load/unload content as needed, using base.Game.Content.Load<Texture2D>("someResource"); or something similar.

See also: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game_members.aspx

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.