3
\$\begingroup\$

I have a question that is confusing me with making a game.

For my game launch i pre load everything for the main menu then i loop the game to display it.

But lets argue player wants to launch a new game or a specific level, then you preload the relevant sounds/graphics for that level right?

How ever, the player must already be running the game loop if they are at the main menu, so you can't load in the game loop or you will load every frame...but im pretty sure most big games don't preload every level when you first run the game from desktop because it would take a long time on some of these games....

How is it done :S

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

At the most basic level, you can simply put an if statement in your main loop, to make the loop do different things at different times.

This would probably take the form of a simple state machine. All you need to do is have a variable which tells the main loop whether it's in "loading" state or in "playing" state, and then have an if ( state == loading ) { } else { } block of code, with the loading code only happening in the 'loading' block, and the gameplay code only happening in the 'else' block.

\$\endgroup\$
5
  • \$\begingroup\$ If i had a load(level_id) ; wouldn't the code not move on past the load call until the loading is finished meaning the game would look frozen ? (assuming the load was very large) \$\endgroup\$ Commented Nov 10, 2012 at 23:02
  • \$\begingroup\$ Yup. Which is why you don't do it that way if your load is going to take a long time. Instead, you'd set up your load so that it loads progressively, loading just the next little bit each time you call it, until the load is finally completed. That way, you want to call it over and over again from your main loop (or somewhere that's called from the main loop) until the load finishes. And that allows you to keep rendering to the screen, all without adding the complexity of thread management. \$\endgroup\$ Commented Nov 10, 2012 at 23:08
  • \$\begingroup\$ Ah i see so if say i had 1000 images i would read each line in a text file to get their directory then load that 1 image then update the progress bar and so on ... \$\endgroup\$ Commented Nov 10, 2012 at 23:11
  • \$\begingroup\$ I would also add, that if you have a fixed time_step (time between frames rendered) you shouldn't call load() method just once in each frame, but rather use something along end = now () + 10; while ( now () < end ) load (); otherwise, if chunks of loaded data are too small, loading process may consist mostly of waiting between frames. \$\endgroup\$ Commented Nov 10, 2012 at 23:13
  • 1
    \$\begingroup\$ When I've done this in the past, we passed the amount of time remaining in the frame into the load() function, and had a heuristic in the load() function which decided whether or not to attempt to load the next file, based on whether or not it thought it had enough time. But all of this is just polish on top of the general concept, and I think it's the concept of splitting up work into small chunks that can be distributed across multiple frames that's the really important thing, here. This doesn't just apply to loading, but to pathfinding, model optimisation, and heaps of other game tasks. \$\endgroup\$ Commented Nov 10, 2012 at 23:18
3
\$\begingroup\$

"Big" games (commercial ones) usually do the loading in another thread. For every small beginner's game without a game engine involved I can give you this advice: DON'T.

Multi-threading is and will always be kind of a problem if you don't know what you're doing. So better don't. What this means for you: you have to load your resources and stop rendering in that time. So yes, basically you load your stuff in the main loop, but remember (e.g. in a boolean value) that you already loaded them and do not load them again in the text frame.

Notice, that with this approach, the main loop will "hang" while you load your resources. People may get confused when the game does not react, so you should make sure to display a loading screen (e.g. the text "loading" somewhere visible) and render it the frame before you do the loading. Look at the in-game loading screen of any Source engine game (Portal, Black Mesa Mod, ...) for an example.

Of course, if you are hardcore, you could try your hand at loading something in the background while displaying a status bar, or loading the resources step by step (one each frame) and drawing the new status in between - but this is rather complicated and not really suited for first-time game developers.

\$\endgroup\$
7
  • \$\begingroup\$ Yes, threads come into the picture in modern commercial games. But this question isn't really about threads; it's about basic program flow control. I worry that bringing up threads is only going to confuse what is, in essence, an extremely simple issue. \$\endgroup\$ Commented Nov 10, 2012 at 22:57
  • \$\begingroup\$ But how do they do for example an animated "Loading" icon if your loading something at the same time... because whilst you load the code won't be moving on to run any thing else yet... so the game looks "frozen" ? \$\endgroup\$ Commented Nov 10, 2012 at 22:59
  • \$\begingroup\$ Lots of different ways to do that. I'm sure that lots of modern games use threads to do the loading in the background. I even worked on a commercial game which used a background thread to do the rendering, while loading happened on the main thread. But honestly, it's just as common (and heaps easier) to just load things one at a time. Load a texture, then go around the main loop again to render a frame. Load the next texture, go around the main loop again. As long as each individual load isn't too lengthy, it's normally not a big problem. \$\endgroup\$ Commented Nov 10, 2012 at 23:05
  • \$\begingroup\$ The animation while loading is definitely not possible without any parallelism (multi-threading). And the one-at-a-time-approach is what I describe in the end ;) Yes, loading it all with a frozen frame is the easiest. \$\endgroup\$ Commented Nov 10, 2012 at 23:07
  • \$\begingroup\$ Why is threading not recommended by the way? \$\endgroup\$ Commented Nov 10, 2012 at 23:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.