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'm developing an Android game. Basically I want to pre-load all graphics/sounds when the app is first loaded. But I also would like to show a progress bar as this is happening. Here is a snippet of Java/Android code to display a progress bar while doing some work:

public class LoadingScreen extends Activity {

private static int progress = 0;
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadingscreen);

    progressBar = (ProgressBar) findViewById(R.id.progress);

    //---do some work in background thread---
    new Thread(new Runnable() 
    {
        public void run() 
        {
            while (progressStatus < 100) 
            {
                progressStatus = doSomeWork();

                //---Update the progress bar---
                handler.post(new Runnable() 
                {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                    }
                });

            }
            //---hides the progress bar---
            handler.post(new Runnable() 
            {
                public void run() {
                    progressBar.setVisibility(8);
                }
            });
        }    

        private int doSomeWork() 
        {
            try 
            {
                //---simulate doing some work---
                Thread.sleep(50);
            } catch (InterruptedException e) 
            {

            }
            return ++progress;
        }

    }).start(); 

}

My question is: how do I design my Asset Manager to load "chunks" of data (bitmaps and sounds/music) in the doSomeWork() function? Would it be some sort of stack that I keep popping off the next asset to load? How do I determine how much of the progress bar I should increment after loading a "chunk" of data?

Is there a better way to approach this or is this the correct way?

share|improve this question
    
FYI: I went with a stack of data. I pop the next item and load it into memory. I get the % by saying (Number of popped items/Total stack items) –  Bryan Denny Nov 7 '10 at 3:36

2 Answers 2

up vote 3 down vote accepted

I would've gone for the asset manager having a stack of data it needs to load. If there's an easy way to go about it, there's the option of adding to a size counter as you add elements to the stack - this means you could increase varyingly. So instead of a large texture and a quick sound effect both increasing loading by 10% say, it could increase by 13% and 2%, for example.

Then pop off each piece of data as you load, handling errors and the like, and increase the amount of bar visible.

I think that there's nothing wrong with that way, but I'm pretty sure someone would be able to give something more complex around here. :P

share|improve this answer
    
I ended up using a stack. Just popped off each thing I needed to load. Thanks! –  Bryan Denny Nov 9 '10 at 21:23

Why don't just say 100% = resource count? Resource loaded => increment.

I suppose you want to use stack to manage dependencies between resources? Push resource. Push dependencies... etc. Than you have 2 passes:

  1. collect resources in stack (count resources)
  2. load resources (increment status bar)
share|improve this answer

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.