Changing render mode

PerPer
edited August 2018 in Questions about Code

Hi!

I have a sketch that needs different render modes depending on different tasks. Is there a way to change the rendermode without restarting the sketch? I did this quick test below with a bad result. I got this error:

processing.corePApplet$RendererChangeException

void setup() {
  size(300, 300, P2D);
  background(233, 23, 23);
}

void draw() {
  fill(255, 0, 0);
  rect(100, 100, 100, 100);
  fill(0, 255, 0);
  rect(50, 50, 100, 100);
}

void keyPressed() {
  if (key == '1') {
    size(255, 255);
  }
  if (key == '2') {
    size(255, 255, P2D);
  }
  if (key == '3') {
    size(255, 255, P3D);
  }
}

Are there any other way?

Answers

  • You're gonna need another PApplet instance in order to have more than 1 renderer.
    Take a look at my examples in this thread post:
    http://forum.processing.org/two/discussion/7202/controlp5-controlwindow-and-controlp5frame

    However be aware that only 1 of those instances can use an OpenGL renderer like P2D & P3D!
    On the other hand, we can have as many JAVA2D renderers as we wish! :-bd

    And as a last touch, we can issue frame.setVisible(false/true) to hide or show the window.

  • Ok. Two stupid questions:

    1) PApplet = the core in processing. Its the java class that processing is built upon?

    2) But if I extends the PApplet - I need to switch into another window. Than the sketch will be even more heavy to run ... I need to use P2D cause making my sketch more smooth but if I need to switch between different windows it might be the same result. Or did I misunderstand?

  • edited April 2015

    Yup! It's the core of Processing's API framework. You can see the whole thing below:
    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java

    I need to use P2D...

    • You can surely choose P2D (or P3D) renderer in 1 of the canvases.
    • But as mentioned, only 1 OpenGL-based renderer per entire program!
    • Only JAVA2D renderer can be used deliberately! :bz

    ... the sketch will be even more heavy to run...

    • If you don't need some of the windows, use noLoop() & frame.setVisible(false); there.
    • Just be aware that each PApplet scope controls its own canvas.
    • If 1 of the PApplet's code needs to access the canvas of the other, it's gonna need the latter's reference.
Sign In or Register to comment.