I have a piece of code that requires two offscreen buffers. When i draw the first buffer into the second one with image(), the first buffer “freezes”.
I draw both graphics to the window in main,
void draw() {
updateBuffer(buffers[0], buffers[1]);
image(buffers[0], 0, 0);
image(buffers[1], width/2, 0);
}
And they both draw as they should.
However when i add
buffers[1].beginDraw();
buffers[1].image(buffers[0], 0, 0); // Problem
buffers[1].endDraw();
the first buffer “freezes” when i draw it in the window, however it still displays it’s updates inside the second buffer.
Interestingly enough, adding an empty beginDraw(), endDraw() fixes the problem.
buffers[1].beginDraw();
buffers[1].image(buffers[0], 0, 0); // Problem
buffers[1].endDraw();
buffers[0].beginDraw();
buffers[0].endDraw();
Full snippet displaying the problem
PGraphics[] buffers = new PGraphics[2];
void setup() {
size(1200, 600, P2D);
for (int i = 0; i < 2; ++i) {
buffers[i] = createGraphics(width/2, height);
buffers[i].beginDraw();
buffers[i].background(0);
buffers[i].fill(i*255, 255 - i*255, 0);
buffers[i].endDraw();
}
}
void draw() {
buffers[0].beginDraw();
buffers[0].background(0);
buffers[0].ellipse(frameCount*4%width/2, height/2, 10, 10);
buffers[0].endDraw();
buffers[1].beginDraw();
buffers[1].background(0);
buffers[1].ellipse(frameCount*4%width/2, height/2, 10, 10);
//Uncomment this to cause buffers[0] to freeze
//buffers[1].image(buffers[0], 0, 0);
buffers[1].endDraw();
//Uncomment this to solve the buffers[0] freeze
//buffers[0].beginDraw();
//buffers[0].endDraw();
image(buffers[0], 0, 0);
image(buffers[1], width/2, 0);
}