Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
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) –  Dave Nov 10 '12 at 23:02
    
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. –  Trevor Powell Nov 10 '12 at 23:08
    
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 ... –  Dave Nov 10 '12 at 23:11
    
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. –  Markus von Broady Nov 10 '12 at 23:13
1  
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. –  Trevor Powell Nov 10 '12 at 23:18
add comment

"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.

share|improve this answer
    
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. –  Trevor Powell Nov 10 '12 at 22:57
    
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" ? –  Dave Nov 10 '12 at 22:59
    
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. –  Trevor Powell Nov 10 '12 at 23:05
    
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. –  opatut Nov 10 '12 at 23:07
    
Why is threading not recommended by the way? –  Dave Nov 10 '12 at 23:18
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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