3
\$\begingroup\$

I'm making a 2D XNA game. I'd like to implement loading screens when stuff has to load for a while. Like when I login to an account, connect to the server, and generate worlds. I'm pretty sure it needs to be multithreaded, because I want to be able to do something like "Generating World 10%...11%...".

  GenerateWorld()
  {
       //Call StartLoading("Generating World"); or something
       //Starter generating, Updating progress...
       //End loading screen and fade into world
  }

Help appreciated, I'm new.

\$\endgroup\$
3
  • \$\begingroup\$ Eek. What have you tried already? This site isn't really for "Write my code for me" type situations. \$\endgroup\$
    – House
    Mar 29, 2012 at 20:25
  • \$\begingroup\$ Im not trying to ask for exact code, Id just like a method too do it, I know how multithreading works, But havent tried it with XNA and am a little unsure what approach to take on how to make the other thread draw the loading screen while something else is loading. \$\endgroup\$
    – Cyral
    Mar 29, 2012 at 20:28
  • \$\begingroup\$ I see, well the way your question is laid out, it looks like "Fill this function for me". I recommend you research it and try on your own first, then ask specific questions if you have them. I'm sure there are plenty of pages out there for loading screens in XNA. \$\endgroup\$
    – House
    Mar 29, 2012 at 20:35

5 Answers 5

1
\$\begingroup\$

You don't need multi-threading at all, but some knowledge in parallel programming could be useful.

Do your load in batches in your game loop and you'll be fine.

\$\endgroup\$
3
\$\begingroup\$

I'd highly recommend looking at the following guide: http://www.spikie.be/blog/page/Building-a-main-menu-and-loading-screens-in-XNA.aspx

I had a look at it yesterday, and pulled out the loading screen code for a project I was working on, think it might be of help to you.

\$\endgroup\$
1
\$\begingroup\$

I wouldn't really call multithreading overkill. It at least doesn't lock up the game/system. So what I would do is have a counter for percentage, load everything in a thread, and for each item loaded increase the counter (by 1/number of items for 0-1 scale). Then, in the draw function, just have something draw current percentage. Then, once the data is finished loading, set a flag to true saying that the game has been loaded (this is important so that you do not crash the game because it wants to draw what hasn't yet been loaded).

\$\endgroup\$
0
\$\begingroup\$

Disclaimer: This is kinda off-the-cuff advice, so take it as such.

If you're trying to multi-thread, it might be overkill for a loading screen. Unless you have a cool loading screen like (I think Fallout's) install screen, where you play Arkanoid while it installs!

You can usually do this with something simple, like using a timer on a short (1 second?) interval to update the "loaded" percentage, while your main application loads what you need.

Plus, if your game is small enough, you won't even need a loading screen!

\$\endgroup\$
5
  • \$\begingroup\$ A timer sounds lik it might work, If I called Draw() during it. While I was offline I think i thought of a soulution, Ill post it when Im done coding it. \$\endgroup\$
    – Cyral
    Mar 29, 2012 at 21:35
  • \$\begingroup\$ One thing Im stuck on, actually drawing it, I set up a draw method for it, but when I call it from my Logon method it dosent do anything. \$\endgroup\$
    – Cyral
    Mar 29, 2012 at 22:03
  • \$\begingroup\$ I don't know what you mean. Just have a timer that checks how much is loaded and updates some integar value to be the percentage. Draw just uses that to draw the progress bar. \$\endgroup\$
    – ashes999
    Mar 30, 2012 at 0:58
  • \$\begingroup\$ I don't know if I got you right, but if you're trying to draw something inside the Load or Update functions, you're not doing a good thing. \$\endgroup\$ Mar 30, 2012 at 2:37
  • \$\begingroup\$ Im trying to draw loading screens when I login, Generate a world etc. My problem is drawing the screen while its loading. I already got the class set up but cant seem to draw it. \$\endgroup\$
    – Cyral
    Mar 30, 2012 at 21:29
0
\$\begingroup\$

I've done something like this:

  • A class ContentLoaderSpool that have:

    . One Dictionary keyed by the paths of the files to load, with the value as the instance of the content loaded (Texture2D, Song, Video and so);

    . One property to flag if there's some content still need to load;

    . A method asking for start loading the content. It iterates through the Dictionary and populates their values. This method works in a parallel thread with high priority. While this method is running, the main game loop continues normally.

The game loop can grab informations of the Spool, to update one progress bar or see if the entire collection of content were loaded and finally change to the next screen.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .