Resource Loader / Loading Screen

Hey there!
I want to make a Loading Screen( a progress bar painted with Java2D)

Painting the ProgressBar is no problem.
I’m more confused how to load the Resources dynamically.

My game is seperated by Scenes and the Game should be like this:

  • Loading
  • Title Screen
  • Loading
  • Level1
  • Loading
  • Level2

and so on. Since Title and every Level uses different Fields and Images and Music and Sounds to load, I have no Idea how to start a “SceneLoader”.
I have no Idea how to “detect” what has to be loaded because for example Level 1 uses 2 Backgrounds, while Level 2 uses 4 Backgrounds etc. :clue:

Well, for every level you have, you’ll need to specify in the constructor which backgrounds/sounds/anything you need. What exactly are you having trouble with? You need to actually tell the level what resources you need.

I think the problem is, how to tell the loading screen that every resource is loaded. Or is it how to load these?

Well from a structure standpoint I suggest this:
Have the object of your LoaderClass in your Main or Game or whatever you call it class. When you first start the game switch the game state to “loading” (Just use enums. Even a small bit gamestate management code makes doing that kind of stuff a lot more organized.) and create the window for the progress bar. Then, set your variables, eg


Image player = new ImageIcon("player.png").getImage();

and then after that add a bit to the number that defines how far the loading bar has progressed.
To make this more dynamic, what I usually like to do is keep and my images all in an array, or sets of arrays.


Image[] weapons;
Image[] players;
Image[] mobs;

This way it makes it nice and easy to load from tile sets.
Also, you can make your progress bar progress based on the loading of these easily, which is really the whole point.


BufferedImage weaponTileset = {Load the tileset into BI};
for(Image img : weapons){
    img = {If each tile is, say, 10x10 then progress 10 pixels down the BI and load this image into a 10x10 piece from that point.}
    progressBar.add(1);
}

Of course you probably wouldn’t add one to the progress bar. Just play around with some math and get it to add to the progress bar based on how many things you have to load.

I implemented a File e.g. into my TitleFolder.
It describes the TitleScreen:
how many BGs,
how many maps(only used in Levels, but for standardization) are used
The Sound Files are also stored in a separated file, the same with the Music.

For every File described in the “Description” File, the progressbar gets a “MaxValue++”.
For every loaded File the Progressbar gets a “Fill++”.
It now looks like this:

https://dl.dropboxusercontent.com/u/13341521/Unbenannt.jpg

Thanks for your help :]

No problem :slight_smile: always happy to help. A medal is always appreciated if I helped you though. :wink:

Rendering a game with ImageIcons is terrible, I strongly suggest not ever recommending using them for anything other than GUIs.

Ah I didn’t know that I’ll keep it in mind. Thanks!

since i abstract all my game resources such as images and sound files i made a GameResource interface that has a load(); method and a isLoaded();

then loading resources is as easy as calling load(); and in the load method just load the resource the way you normally would and then set loaded to true;

you can just as easily make an arraylist of and add all your resources , then amount_loaded/total_resources(make percentage) and use that to display your loading bar.

Better yet, you could use a hashmap, they’re quite handy for resource loading and retrieval!

yeah this^^

i actually do this to index all my resources in a hash map but i have a different class apart from the resource loader to do that for simplicity hehe