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 & HelpIntegration › Switching renderers
Page Index Toggle Pages: 1
Switching renderers (Read 1312 times)
Switching renderers
Nov 22nd, 2005, 9:25pm
 
I know this is a specialized need, so it's not a priority request, but I'll ask anyway...

I would like to have the option of switching renderers during runtime. This is for a Java project where an application is set up to run as a PApplet inside a Frame. This then uses modules to draw different animated pieces. One might look best in P3D, another better in Java2D etc.

I've hacked up a switchRenderer() that basically does the relevant reflection calls that size() does and sets up a new renderer.

Code:
  public void switchRenderer(String renderer) {
try {
println("switchRenderer - "+renderer);
Class rendererClass = Class.forName(renderer);
Class constructorParams[]=
new Class[] {
Integer.TYPE,Integer.TYPE,PApplet.class};
Constructor constructor=
rendererClass.getConstructor(constructorParams);
Object constructorValues[]=
new Object[] {
new Integer(width), new Integer(height),
this};
g = (PGraphics)constructor.newInstance(constructorValues);
g.defaults();

}
catch(Exception e) {
println(e.toString());
}
}


Is there a nicer way of doing this? Will the renderers be more modular in the future, like BGraphics used to be? It was great to be able to set up multiple renderers that could be used separately.
Re: Switching renderers
Reply #1 - Nov 23rd, 2005, 5:23pm
 
Can't you just call size(width,height,newrenderer)?  Or am I misunderstanding your question?

Marcello
Re: Switching renderers
Reply #2 - Nov 24th, 2005, 12:14pm
 
size() is only allowed in setup(). My switchRenderer() does the normal renderer setup done by size(). My question is ultimately whether PGraphics could be disconnected from PApplet, so that one could instantiate several renderers. I realize there are issues with OPENGL etc.
Re: Switching renderers
Reply #3 - Nov 24th, 2005, 8:36pm
 
I see.  Looking at the source, you could probably try doing:
Code:
int f = frameCount;
frameCount = 0;
size(width,height,newrenderer);
frameCount = f;

However, that's not much better than what you're doing.  The size() only being allowed in setup() is merely by design, so clearly you're not intended to change the renderer after the fact.  If you do this frequently, it might be a waste of processing (making a new image buffer each time), as well.

Looking at the source, you can create new PGraphics3 and PGraphics2 with a null PApplet (if you want to bypass using PApplet altogether), just not for PGraphicsGL.  

Then you can either change PApplet.g to each one (without having to create a new PGraphics object every switch), or avoid PApplet altogether and call begin/endFrame() manually and use the PGraphics.image object.

Still, all this stuff might change, since it's dealing with Processing internals, so I guess fry or someone would have to answer whether this stuff will be finalized at some point.

Marcello

p.s. this post makes no sense, but I think that's all I have to add. -__-
Re: Switching renderers
Reply #4 - Nov 25th, 2005, 2:22am
 
The code you quote won't work. My code duplicates what size() actually does to initialize a renderer, but without the safeguards size() uses to prevent you from doing so outside of setup(). Duplicate image buffers is not a problem, in fact that's the whole idea. Your idea of passing null PApplets would probably work.

I guess I'm getting at a more general issue of how the processing.core classes interact. Right now the class hierarchy is obviously focused on working properly for users working within the Processing GUI. But for library writers or people coding in Java with Processing as a library, they might want to do things differently.

For instance, if one coukd abstract the renderers so that they are not tied explicitly to PApplet, one could use them a bit like BufferedImage. I'm aware that OpenGL probably won't work this way. There is also internal information (triangles, matrices etc) inside PGraphics which would be useful to expose to the user.

I'm of course aware that none of this is set in stone since we're still in BETA. I just wondered if anyone had tried doing this.
Re: Switching renderers
Reply #5 - Nov 25th, 2005, 8:06pm
 
This code should work (tested on v0095-expert):
Code:

void setRenderer(String type) {
g = null;
size(width,height,type);
}


With duplicate image buffers I was implying that switching back and forth was not a good idea, since each switch constructs a new pixel array/BufferedImage.  If you want to tie processing into other Java utilities, you should be able to bypass PApplet completely by passing null to the constructor, then calling the begin/end frame methods as I mentioned before around graphics operations, then grabbing the image from PGraphics to draw to screen.  That allows you to abstract the renderers as you say.

Again, OpenGL is a different issue.  As for exposing triangles and matrices.  I believe you can already access the matrices (they're public), but triangle information is done in immediate mode and may not be so useful.

Marcello
Re: Switching renderers
Reply #6 - Nov 26th, 2005, 3:24pm
 
Thanks for the tips, Marcello. I have a working solution for now.
Page Index Toggle Pages: 1