Loading Screen Functionality (States)

Started by
4 comments, last by CC Ricers 11 years, 3 months ago

Hey guys,

Im currently programming a game using Allegro 5 (written in C++) and im looking for functionality regarding Loading Screens.

I suspect it will be a state in between my "TITLE SCREEN" state and my "PLAYING" state.

Can anyone tell me if it is possible to include a Loading screen bitmap with a "progress bar"?

If so, could you also guide me to a resource or point me in the direction to find out more?

Thanks,

Toshi

Advertisement

this can be accomplished several ways.

if you simply want a load screen, with no feedback, then using a state for loading is fully reasonable, however if you want a progress bar, things get a bit interesting.

either you:

1. use multi-threading/async loading, in which case, you need some method to post a percentage complete(for example, at specific point's in the load, pass an value to some variable that say's "i'm 10% done, 20%, etc.), or if loading multiple resources, and you only want to pass back what the file is your currently loading, then before opening a file, simply pass the filename string to some varaible to be rendered over your loading screen.

2. if single-threaded, post redraws at those percentage points, simply clear the screen, draw w/e your loading screen is, and flip the screen(disabling v-sync would be important here)

any decent content manager should incorporate such functionality for anything it's capable of loading, however if your loading the files directly yourself, it might by a bit tricker to incorporate such functionality.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

I see, thanks slicer, thats very helpful!

At the moment I am creating art for the various screens, and I can now confidently build the loading screen.

I had some thoughts about loading screens too.

In general, putting up a simple bitmap before starting loading is simple enough. And as slicer said, there's a couple of ways to do a loading bar.

Since I do my own loading, I have the following idea:

I assume that only 1 and only 1 loading screen will be shown at a time. This meant that I can stick it as a globally accessible property in my singleton interface class. So, to access the loading screen, I would do Interface::I().GetLoadingScreen(); - which would return me the main loading screen. From there, I can then proceed to set its properties like background image, percent loaded etc.

The good thing about this is that since I have my interface drawing setup, i don't have the worry about drawing the screen at all - it's automatically done by the interface. I just have to call a Show/Hide loading screen function to make it show / hide.

Having a loading screen like that also allows me to write code in the Interface to automatically disable all input whenever the loading screen is shown - so that's another thing I don't have to worry about when loading stuff, since it will be done when I tell the UI to show the loading screen.

The requirement for this, however, is that I must put all my loading into separate threads. The basic flow in my main thread (that handles drawing / updating / input) is like this:

1. User triggers loading

2. I start a new thread to do my loading, so I don't block the main thread.

And in the loading thread:

1. Set UI to show loading screen

2. Set the background graphic / initial percent loaded (usually 0%)

3. Do my loading here, while periodically updating the percent loaded based on what I'm loading

4. Set UI to hide the loading screen.

The UI will automatically handle displaying the loading screen, blocking all user input and updating the loading bar on the screen.

Normally with multi-threading you'd need to have a mutex on the values you're changing, but in this case, I think I can get away without any, since there's nothing critical there, and the UI will only read the values, while the loading thread will only write them.

Hey Toshi....I'm also using Allegro 5 for my current project. I ended up creating a LoadScreen class which is capable of presenting a screen of messages to let the user know the status of the load. Like Milcho I disable input while this screen is up. Now I don't have a progress bar, but it wouldn't be too hard to add based on the way I've put this together. The key would be knowing ahead of time how many things you are going to load, which in my current setup I don't know.

To give you an idea...
Loadscreen class is really simple...


class LoadScreen
{
public:
    LoadScreen();
    ~LoadScreen(void);
    void Destroy();
    void AddMessage(std::string);
    void Display(float);
    void ClearMessages();

private:
    ALLEGRO_BITMAP *LSimage;
    std::string messageQueue[5];
};

Then as I'm looping through loading all my graphics, entities, etc. I call this inside the loops...


    LoadingScreen->AddMessage(Message);
    if(g_DebugLevel==0)
        LoadingScreen->Display(0);
    else
        LoadingScreen->Display(0.5);
    al_flip_display();
    al_clear_to_color(al_map_rgb(0,0,0));

LoadingScreen is just an instance of LoadScreen.

Message is a std::string of whatever I want to update the user about

The float that gets passed into Display() is a "pause time" the artificially slows down the display so you can read the messages.

(and yes I know this isn't the best code in the world....this is an area of my product thats on the eventual refactor list...but it works for now)

My loading screen is a static class with a static function, because I won't need to display more than one screen at a time. So, similar to Milcho's setup. It also stores a list of the resources to load in a queue, to send to special content loader made for this type.

Here is some of my code in the loading screen. I've generalized the loader class to be any type of file you may want. I'm not familiar with Allegro or what ALLEGRO_BITMAP supports, but the trick is to go through each item frame by frame.


BitmapLoader Bitmaps;

LoadingState::LoadingState()
{
	Bitmaps.addToQueue("dragon.bmp");
        Bitmaps.addToQueue("tiles.bmp");
        Bitmaps.addToQueue("background.bmp");
}

void LoadState::update()
{

	if (!Bitmaps.queueEmpty()) {

		if (!Bitmaps.fileLoaded())
			Bitmaps.loadNext();


if (!Bitmaps.doneProcessing()) {


// Add to the data buffer

Bitmaps.Process();

}

else {


/// Add the bitmap to the scene and then clear buffer
Assets->addBitmap(Bitmaps.dataBuffer());
Bitmaps.clearData();
}
}
else
{
// Move on
Game->pushState(PlayingState::Instance());
}
}


Granted, I only needed to do this with 3D meshes, as it sometimes took more than a few seconds to load files in one go. For more complex assets such as meshes, I broke it down into smaller discrete parts of data, process a certain number of those at a time and treating that as one complete "item". One pass of processing is done per frame. Textures didn't need a lot of attention yet, but I didn't use too many of them so they were loaded in just one frame.

If individual files don't take too long, you can perhaps make your object loader more generic to read everything from map files to sounds or bitmaps. Then choose a certain number of files to load each frame, take that number and divide by the total of files on your list to determine the loading progress.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

This topic is closed to new replies.

Advertisement