Sharpnel
Sharpnel

Reputation: 173

Load textures while playing

For my 2D game :
While playing, I have to load some large texture2D (2000x2500 pixels) and Unload some others.
Of course I want to Load and Unload texture2D without game/draw freeze (or the lesser...) :x I don't know if it's possible.

I already use a thread to load some texture while drawing the "Loading screen"

//thread Loading
ThreadStart th_loadingScreen = delegate { DisplayLoading(LoadingScreen); };
new Thread(th_loadingScreen).Start();

But I think it's pretty different.

Of course, I tried something :

private void LoadUnload()
{
    for (int j = 0; j <= NbrRow; j++)
        for (int i = 0; i <= NbrCol; i++)
        {
            if(somethingTrue)
            {
                ThreadStart th_LoadInGame = delegate
                    {
                        LoadInGame(i, j, TextureStringPathToLoad);
                    };
                new Thread(th_LoadInGame).Start();
            }
        }
}

But I have a little freeze.

And, I know how to unload the content, but I don't know how to unload a single loaded texture :x

Upvotes: 3

Views: 107

Answers (1)

user155407
user155407

Reputation:

There's no way to unload individual elements within a ContentManager object. So, what I do is make multiple ContentManagers and split up what I need to among them. That way I can Unload one, for example, and keep others in memory still.

As for the little freeze you're seeing, threading by itself does not guarantee an absence of hiccups or the like. I would mess around with the Thread's Priority property; perhaps try setting it lower and see what happens. Even this is no guarantee however. From the article:

Operating systems are not required to honor the priority of a thread.

Upvotes: 2

Related Questions