Create 500 PGraphics at the same time

I am trying to optimize a part of my code that creates 500 PGraphics. Is there a way to creates them all at the same time ?

final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;

void setup() {
  img = loadImage("imge.png");
}

void draw() {
  Shape[] survivors = {}; // Assume that there are 100 objects in this variable
  Shape[] children = {};
  
  for(Shape shape: survivors) {
    for(int child=0; child < child_count; child++) {
      float x = randomVar(shape.x, position_variation, 0, img.width);
      float y = randomVar(shape.y, position_variation, 0, img.height);
      float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
      float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
      float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
      float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
      children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
    }
  }
}

float randomVar(float original, float range, float min, float max) {
  return constrain(original + random(-range, range+1), min, max);
}

I tried to use a thread() that fills up children and a while to wait for it to be full, but I didn’t came up to anything working, I am not an expert at all in threads…

How can I create a lot of PGraphics at the same time and put them inside an array ?

The draw method is called ~60 times a second and is used to render the frames. It is not the place to create the shapes. You might trty the following although its difficult to test as we don’t have the Shape class code

final int child_count = 5;
final int min_size = 5;
final int max_size = 300;
final int position_variation = 35;
final int size_variation = 20;
final int rotation_variation = 10;
final int alpha_variation = 30;
PImage img;

Shape[] survivors = {}; // Assume that there are 100 objects in this variable
Shape[] children = {};

void setup() {
  img = loadImage("imge.png");
  for (Shape shape : survivors) {
    for (int child=0; child < child_count; child++) {
      float x = randomVar(shape.x, position_variation, 0, img.width);
      float y = randomVar(shape.y, position_variation, 0, img.height);
      float sizeX = randomVar(shape.sizeX, size_variation, min_size, max_size);
      float sizeY = randomVar(shape.sizeY, size_variation, min_size, max_size);
      float rotation = randomVar(shape.rotation, rotation_variation, -180, 180);
      float alpha = randomVar(alpha(shape.c), alpha_variation, 0, 255);
      children = (Shape[]) append(children, new Shape(x, y, sizeX, sizeY, rotation, alpha));
    }
  }
}

void draw() {
}

float randomVar(float original, float range, float min, float max) {
  return constrain(original + random(-range, range+1), min, max);
}

The fact that it is in the draw method is not a problem, all I want is optimize the two for loops. The code I posted above is a lot shorter than my whole code.

Why are you having a problem maintaining a decent frame rate? What frame rate do you want?

Based on the code you have posted it unlikely there will be any benefit from using multiple threads.

It is a huge problem as has been pointed out.

new is costly and append as well.

Put it in setup() and when you really want to change it 60 times per second don’t use new but change the values of the existing objects.

Sorry if I don’t know how to say it clearly… "-_-
This is only a small part of my code, the whole program takes about 10 seconds to calculate everything, and then starts over.
I made a github repository if you want to understand what my program does

1 Like