We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Fast way to render offscreen buffer
Page Index Toggle Pages: 1
Fast way to render offscreen buffer? (Read 1414 times)
Fast way to render offscreen buffer?
May 31st, 2010, 10:01am
 
I'm rendering path trails in an offscreen buffer and rendering the buffer:

Code:
// somewhere in setup()
trailBuffer = p.createGraphics(myWidth, myHeight, JAVA2D)

// somewhere in draw()
image(trailBuffer.get(), 0, 0)


After profiling, the get() method is a performance bottleneck since it clones the offscreen buffer, copying all the pixels to a new PImage.

Is there a faster way to blit the entire buffer?
Re: Fast way to render offscreen buffer?
Reply #1 - May 31st, 2010, 10:21am
 
Code:
// somewhere in setup()
trailBuffer = p.createGraphics(myWidth, myHeight, JAVA2D);

// somewhere in draw()
image(trailBuffer, 0, 0);
Re: Fast way to render offscreen buffer?
Reply #2 - May 31st, 2010, 12:01pm
 
Thanks for the tip! It does work, but I had some weird results. If I try your suggestion,  I get the following error:

Code:
Exception in thread "Animation Thread" java.lang.NullPointerException
at java.lang.System.arraycopy(Native Method)
at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:427)
at processing.core.PGraphicsJava2D.updatePixels(Unknown Source)
at processing.core.PGraphicsJava2D.imageImpl(Unknown Source)
at processing.core.PGraphics.image(Unknown Source)
at processing.core.PApplet.image(Unknown Source)


However, if I store the results of trailBuffer.get() in an image first, everything works fine.  So now I do:

Code:
In setup():
PGraphics trailBuffer = p.createGraphics(myWidth, myHeight, JAVA2D);
PImage trailImage = trailBuffer.get();

In draw():
trailBuffer.endDraw();
trailImage = trailBuffer;
image(trailImage, 0, 0);


Thanks again for the tip! It does work faster now.
Re: Fast way to render offscreen buffer?
Reply #3 - Jun 1st, 2010, 1:00am
 
You don't need trailImage. And the get() you do isn't useful, since you just overwrite the content you get.

The error you show is generally because of lack of beginDraw() or endDraw() around the drawing in the PGraphics.
Page Index Toggle Pages: 1