6
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Bryan Denny Nov 7 '10 at 3:36
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ I ended up using a stack. Just popped off each thing I needed to load. Thanks! \$\endgroup\$ – Bryan Denny Nov 9 '10 at 21:23
0
\$\begingroup\$

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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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