Hi @Chrisir – sorry for the late reply. I’m not certain that I understood this question.
What is your goal in avoiding thread? Note that redraw() and draw() will not work – you can try to create a metadraw() and then invoke draw manually, but the screen won’t actually update.
This sketch doesn’t demonstrate recursion – it just shows the failure to update the screen when you try to wrap something around a series of manual draw calls:
// THIS DOES NOT UPDATE THE SCREEN UNTIL THE END
int metaFrameCount = 0;
void setup() {
noLoop();
metadraw();
}
void metadraw() {
background(192);
for(int i=0; i<100; i++){
delay(50);
draw();
// redraw();
}
metaFrameCount++;
}
void draw() {
rect(random(-10, width), random(-10, height), 20, 20);
println("mfc:", metaFrameCount, "fc:", frameCount, millis());
}
If you have a basic recursive structure – like this example:
void setup() {
size(640, 640);
noStroke();
rectMode(RADIUS);
background(255);
drawSquare(width/2, height/2, width/2.1, 0.88, 2);
}
void drawSquare(float x, float y, float w, float scale, float min) {
fill(random(128,255), random(128,255), random(128,255), 255);
rect(x, y, w, w);
float off = w/2.0;
float w2 = off * scale;
if (w2 > min) {
drawSquare(x-off, y-off, w2, scale, min);
drawSquare(x+off, y-off, w2, scale, min);
drawSquare(x-off, y+off, w2, scale, min);
drawSquare(x+off, y+off, w2, scale, min);
}
}
…and you want to animate it in the draw thread, then you need to queue a set of actions and draw off the queue. So, rather than drawSquare, queueSquare would add new float[]{x, y, w2, scale, min} to an ArrayList<float[]> squareQueue. This gives you one big queuing pass, but once it is done you can now loop over the queue data and animate it – for example, only drawing n rows per second.
However, what if it takes you an hour to render that queue data, and you want a preview during that time – but you only want to do it on one thread? In that case, you need to model your recursion tree as a linked data structure in a way such that you can be expanding nodes on the tree, stop (when the tree gets to large or too much time has passed, respecting your update limits) to draw from it, then resume on the next frame and keep expanding nodes on the tree. There are many ways to do this – depending on how drawing relates to intermediate or terminal nodes, which should be drawn first, and if you can safely throw away items away after you have drawn then.
