Any way to optimize this pixel overlay?

Hi there, I'm currently working on a "retro" looking game and I've made a function that draws a pixelized overlay across the canvas. I'm currently working on optimizing it and would love any help. It already runs fairly well (60fps) but compared to when it's not running (500+fps) I think it could do better.

void setup() {
  size(1024,896,P2D);
  frameRate(500);
  noStroke();
  smooth(0);
}

void draw() {
  background(0);
  ellipse(mouseX,mouseY,100,100);
  if(mousePressed){
    drawPixels(256,224);
  }
  println(frameRate);
}

void drawPixels(int retroResX, int retroResY) {
  int pixelScale = width/retroResX;
  loadPixels();
  for(int y=0;y<retroResY;y++){
    for(int x=0;x<retroResX;x++){
      for(int py=0;py<pixelScale;py++){
        for(int px=0;px<pixelScale;px++) {
          pixels[((py+(y*pixelScale))*width)+(px+(x*pixelScale))] = pixels[(y*width*pixelScale)+(x*pixelScale)];
        }
      }
    }
  }
  updatePixels();
}

Answers

Sign In or Register to comment.