Isn't Processing very inefficient when redrawing all the time?

Hello,
so, I am currently writing a Processing port for C++ which should be focused on Performance. So I use OpenGL and noticed something (for me atleast) interesting, let’s take a look at this example code I wrote for this (normal Processing):

class Button {
  float x, y, w, h;
  boolean isPressed;
  Button(float x_, float y_, float w_, float h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
  }

  void display() {
    rectMode(CORNER);
    noStroke();
    if (!isPressed) {
      fill(0);
    } else {
      fill(245, 132, 62);
    }
    rect(x, y, w, h);
    fill(235, 122, 52);
    rect(x+h/10, y+h/10, w-h/5, (h*8)/10);
  }
}

Button button;

void setup() {
  size(600, 400);
  button = new Button(width/2 - 100, height/2-20, 200, 50);
}

void draw() {
  background(127);
  button.display();
}

void mousePressed() {
  button.isPressed = true;
}

void mouseReleased() {
  button.isPressed = false;
}

In this case Performance doesn’t really matter, but if you have 1000 buttons, it might do. And the thing here is, why does Processing force me here to redraw all the time? That would every time open a vertex buffer, display it, and destroy it, or not? Or is there a special hidden class I didn’t know about, which allows me to hold a vertex buffer in my class? I am specifically asking, because that’s the way I implement the rect() function right now in my libary: It makes a vertex buffer, displays it, and destroys it.

1 Like

Yup that’s what draw does. You can call noloop() and redraw() if you want better control.

There is an alternative and that is to use an off-screen buffer to remember the current visual state of the control state. It means that every frame you are simply copying a single image to the screen (a very fast op). I use this approach in G4P.

Also G4P uses lazy updating of the buffer - by that I mean the buffer is only updated if

  1. there is a request to display the button AND
  2. the button has changed state since the last buffer update
3 Likes