Overwriting pixels[] Manually

I just wanted to know if there’s any way I can bypass loadPixels() and just do pixels = new int [width * height];. I have a function that renders a tilemap to an int array, and load&updatePixels() is slowing it down quite a lot, so anything I can do to mitigate this would be really helpful!

I’ve tried

//loadPixels();
pixels = new color [width * height];
Render (pixels);
updatePixels();

and it doesn’t seem to render anything to the screen. I thought it would either work or crash, but it doesn’t do either. It just doesn’t do anything at all, which is pretty weird. I’ve also tried commenting out (only) updatePixels(), but that has the exact same effect (which is no effect at all). If anyone knows a better way to work with this, please let me know!

(also, I know I probably just shouldn’t use processing for this, but I want to know if there’s any way to make this (the overwriting pixels[]) work)

Hello,

There is a tutorial here:
Images and Pixels / Processing.org

Coding Train:
https://www.youtube.com/watch?v=EmtU0eloTlE
https://www.youtube.com/watch?v=j-ZLDEnhT3Q

:)

1 Like

You can bypass loadPixels() like this:

void setup() {
  size(800, 800);
  g.pixels = new color [width * height];
}

void draw() {
  for (int i = 0; i<g.pixels.length; i++) {
    g.pixels[i] = color(255, 0, 255);
  }
  updatePixels();
}

You can’t get around needing updatePixels() unless you write directly to the underlying render-dependent buffer, in which case you’ll need to dig around the internals a little. This is possible – I’ve done it with JavaFX at least – but don’t have the code right now (with lockdown I’m away from my main PC).

2 Likes

thanks so much! It’s really nice to have people like you!