A concrete example, using a simple sketch as pretext.
- int idx = 0;
- int[] indices;
-
- void setup()
- {
- size(200, 200);
-
- println("Starting init");
- background(255);
- fill(0);
- text("Please wait, loading...", 20, height / 2);
- indices = new int[width * height];
- // Generate a list of random indices
- for (int i = 1; i < indices.length; i++)
- {
- int j = int(random(0, i+1));
- indices[i] = indices[j];
- indices[j] = i;
- }
- delay(5000); // Lengthly initialization here: load stuff, compute things, etc.
- println("Init ended");
-
- background(255);
- }
-
- void draw()
- {
- if (idx >= width * height)
- {
- println("Done!");
- noLoop();
- return;
- }
-
- loadPixels();
- pixels[indices[idx++]] = 0xFF000000 + idx * 64; //#000000;
- updatePixels();
- }
We don't see the message, because draw() hasn't started.
So we can do the drawing in two parts, as GoToLoop said:
- int idx = 0;
- int[] indices;
- int startPhase = 0;
-
- void setup()
- {
- size(200, 200);
- }
-
- void draw()
- {
- switch (startPhase)
- {
- case 0: // Display message in this frame
- println(startPhase);
- background(255);
- fill(0);
- text("Please wait, loading...", 20, height / 2);
- startPhase++;
- return;
- case 1: // Do the setup
- println(startPhase);
- setUpSketch();
- startPhase++;
- return;
- case 2: // Doing the setup
- println(startPhase);
- return;
- }
-
- if (idx >= width * height)
- {
- println("Done!");
- noLoop();
- return;
- }
-
- loadPixels();
- pixels[indices[idx++]] = 0xFF000000 + idx * 64; //#000000;
- updatePixels();
- }
-
- void setUpSketch()
- {
- println("Starting init");
- indices = new int[width * height];
- // Generate a list of random indices
- for (int i = 1; i < indices.length; i++)
- {
- int j = int(random(0, i+1));
- indices[i] = indices[j];
- indices[j] = i;
- }
- delay(5000); // Lengthly initialization here: load stuff, compute things, etc.
- println("Init ended");
- startPhase++;
- background(255);
- }
The phase 2 is actually skipped, because the setup phase is blocking the sketch.
And the wait screen is a bit static...
We can do an animation, while doing the init in a separate thread:
- int idx = 0;
- int[] indices;
- int startPhase = 0;
-
- void setup()
- {
- size(200, 200);
- }
-
- void draw()
- {
- switch (startPhase)
- {
- case 0: // Start the setup
- println(startPhase);
- // setUpSketch();
- thread("setUpSketch");
- startPhase++;
- return;
- case 1: // During the setup
- // println(startPhase);
- showLoading();
- return;
- }
- // println("- " + startPhase);
-
- if (idx >= width * height)
- {
- println("Done!");
- noLoop();
- return;
- }
-
- loadPixels();
- pixels[indices[idx++]] = 0xFF000000 + idx * 64; //#000000;
- updatePixels();
- }
-
- void setUpSketch()
- {
- println("Starting init");
- indices = new int[width * height];
- // Generate a list of random indices
- for (int i = 1; i < indices.length; i++)
- {
- int j = int(random(0, i+1));
- indices[i] = indices[j];
- indices[j] = i;
- }
- delay(5000); // Lengthly initialization here: load stuff, compute things, etc.
- println("Init ended");
- background(255);
- startPhase++;
- }
-
- int loadingX = 20;
- void showLoading()
- {
- background(255);
- fill(0);
- text("Please wait, loading...", 20, height / 2);
- fill(#0000AA);
- ellipse(loadingX, 2 * height / 3, 20, 20);
- loadingX += 2;
- if (loadingX > width - 20)
- {
- loadingX = 20;
- }
- }
Note: don't name the setup function "init", it is a method used internally by Processing.