4

I made a game for Windows 8 in monogame but running into a problem. We finally got our hands on a Surface RT but we noticed that the loading times are really long on this device. We looked at several other games and noticed that this wasn't an uncommon issue. To fight the boredom of the user during the loading of the resources I want to draw a loading bar and some random facts onto the screen. The problem is that loading the resources blocks the rest of the game and doesn't allow me to draw anything because it stays stuck at the initializing part.

I searched for creating a lose Thread but found that Windows Store didn't support that so now my question to you how can I load my resources in the background or another way to not block the complete game and be able to call handle my Draw() function to draw a loading bar onto my screen.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
MrME
  • 337
  • 2
  • 14

1 Answers1

4

I did something like this:

protected volatile bool ContentLoaded = false;

protected async override void LoadContent()
{
    base.LoadContent();

    Enabled = false;
    await ThreadPool.RunAsync(new WorkItemHandler(LoadAllContent));
}

protected void LoadAllContent(Windows.Foundation.IAsyncAction action)
{
    if (action.Status == Windows.Foundation.AsyncStatus.Error)
       System.Diagnostics.Debug.WriteLine(action.ErrorCode);

    // load your contents

    ContentLoaded = true;
    Enable = true;
}

And at the beginning of your Draw method:

if (!ContentLoaded)
{
    // draw your loading screen

    return;
}

If you want a progress bar you need a counter to increase every resource you've loaded, then in your Draw your bar has to relate to that counter.

pinckerman
  • 4,115
  • 6
  • 33
  • 42
  • I didn't get it working with your exact answer because of my state management but I could use everything you said in another function and work it's way into my code and now runs nice without blocking my UI/Drawing Thread :D Thanks alot! – MrME Oct 05 '13 at 01:05
  • I've had your same problem a couple of months ago, so I'm glad if this can be useful to someone else. :) – pinckerman Oct 05 '13 at 09:43