[SOLVED] Dynamic Loading Process (Keeping up with percentages)

Hello JGO on occasion I get rather bored so I’ll choose something to program ;D

Tonight I’m trying to program a loading system for my game that will do the following:
Load my games resources (Textures, Maps, Objects etc…)

I would like to make this loading system ‘dynamic’, and by dynamic I mean I can always calculate the percentage of the loading process. (Which will hopefully depend on the # of objects being loaded)

Pseudo-code:


public void load() {
     while(percentage < 100) {
          // object by object, image by image load them
          // all into memory.
          percentage += some_formula;
     }
}

So my question is:
How should I go about writing a dynamic loading system?

Notes:

of objects needing loading are predefined.

The loading process is occurring via a SwingWorker to update the GUI while loading.

If anybody could give me some advice or a snippet / pseudo code on how to go about this that would be great!
Thanks JGO :slight_smile:

Hopefully this snippet will give some insight into what I’m asking :slight_smile:


	ImageIcon[] imgs1 = new ImageIcon[15]; // Dynamic Texture Array
	ImageIcon[] imgs2 = new ImageIcon[7]; // Dynamic Texture Array
	ImageIcon[] imgs3 = new ImageIcon[80]; // Dynamic Texture Array
	int percent, totalPercent = 100;

	public void load() {
		int objects2Load = imgs1.length + imgs2.length + imgs3.length;
		int increment = 0; // <--- THIS IS WHAT NEEDS CALCULATING
		print("At this rate (Loading " + objects2Load + " objects).");
		print("Each object will increment the % by " + increment + ".");
		while (percent < totalPercent) {
			/* Load a object */

			// ....

			/* Increment our percentage ^_^ */
		}
	}

Well, If I am understanding what you are trying to do then this is simple.

you want to figure out the increment do 100 / number of files.


	ImageIcon[] imgs1 = new ImageIcon[15]; // Dynamic Texture Array
	ImageIcon[] imgs2 = new ImageIcon[7]; // Dynamic Texture Array
	ImageIcon[] imgs3 = new ImageIcon[80]; // Dynamic Texture Array

	double percent, increment; /*needs to be a double, if you have over 100 files to load, we wont be able to accurately check the percent, as 200 files will mean a increment of 0.5*/

	public void load(){

		int numberOfFiles = imgs1.length + imgs2.length + imgs3.length;

		increment = 100 / numberOfFiles;

		System.out.println("At this rate (Loading " + numberOfFiles + " objects).");
		System.out.println("Each object will increment the % by " + increment + ".");

		while(percent < 100){
			/* Load a object */

			percent += increment;
		}

	}

We dont really need a totalPercent, but you can change it to a variable name if you want.

Thanks mate I figured it out :slight_smile:


	ImageIcon[] imgs1 = new ImageIcon[15]; // Dynamic Texture Array
	ImageIcon[] imgs2 = new ImageIcon[7]; // Dynamic Texture Array
	ImageIcon[] imgs3 = new ImageIcon[80]; // Dynamic Texture Array
	double percent = 0;
	double totalPercent = 100;

	public void load() {
		int objects2Load = imgs1.length + imgs2.length + imgs3.length;
		double increment_formula = totalPercent / objects2Load;
		print("At this rate (Loading " + objects2Load + " objects).");
		print("Each object will increment the % by " + increment_formula);
		boolean accurate_formula = (increment_formula * objects2Load) == totalPercent;
		if (accurate_formula) {
			print("Formula's accurate!");
		}
	}

You’re correct, I need double data type not int ~_^

I would do it with something like this


interface ResourceLoader
{
  void load();
  // a guess of as hard it is to load this resource
  float loadFactor();
}

and then calculate the percentage from the loadfactor

when loading image resource or other bigger files you could use the filesize to calculate the loadfactor.
Also when you have to do some calculations after loading the file you could add some linear or constantfactor to the filesize

I’d to it differently :slight_smile:

I’d create a class called:


public class Loader<A> {

    private float loaded; // range 0 to 1
    private A product;

    public void setProgressInRelation(int loaded, int toLoad) {
        setProgress((float) loaded / (float) toLoad);
    }

    public void setProgress(float progress) { // progress in range 0 to 1
        loaded = progress;
    }
    
    public float percentage() {
        return loaded * 100;
    }

    public void finishProduct(A product) {
        this.product = product;
    }

    public void isFinished() {
        return this.product != null;
    }

    public A getProduct() {
        return this.product;
    }
}

And what I’d do when loading code:


void loadImages(final Loader<Image[]> percentage, final String[] paths) {
    final Image[] images = new Image[images.length];
    (new Thread(new Runnable() {
        public void run() {
            int toLoad = paths.length;
            int loaded = 0;
            for (int i = 0; i < paths.length; i++) {
                // TODO: Implement loadImage
                images[i] = loadImage(paths[i]);
                loaded = i;
                percentage.setProgressInRelation(loaded, toLoad);
            }
        }
    )).start();
    percentage.finishProduct(images);
}

Loader<Image[]> percentage = new Loader<>();
loadImages(percentage, new String[] { "enemy.png", "data/player.png" });
while (!percentage.isLoaded()) {
    updateLoadingBar(percentage.percentage());
}
Image[] myFinalImageProductOhGladItsFinallyThere = percentage.getProduct();


-void loadImages(final Loader<Image[]> percentage, final String[] paths) {
+void loadImages(final String... paths) {
+    final Loader<Image[]> percentage = new Loader<>(); 
    final Image[] images = new Image[images.length];
    new Thread(new Runnable() {
        public void run() {
            for (int i = 0; i < paths.length; i++) {
                images[i] = loadImage(paths[i]);
                percentage.setProgressInRelation(i, paths.length);
            }
+          percentage.finishProduct(images);
        }
    ).start();
-    percentage.finishProduct(images);
}

-Loader<Image[]> percentage = new Loader<>();
+Loader<Image[]> percentage =  loadImages("enemy.png", "data/player.png");
while (!percentage.isLoaded()) {
    updateLoadingBar(percentage.percentage());
}
Image[] myFinalImageProductOhGladItsFinallyThere = percentage.getProduct();

Nice!

The code had some errors, because I’ve never actually tried it out :slight_smile: Also, it was written in the box I’m typing in right now ::slight_smile: