Framedrops when displaying PImage at negative coords

Hi,
I noticed that when I call the image() function with negative coordinates but part of the image is still visible, I get massive framedrops from the set framerate(60) to around 30 to 45. Does anyone know if this is a known issue or if it has to be something else?

I guess the code doesn’t really matter but for completeness (it’s part of a game, the whole program would be too large):

void display() {  
    for (GameObject go : toDisplay) {
      // pos is a PVector, p my local player (always in the center of the screen) 
      float x = go.pos.x - p.pos.x + width/2;  
      float y = go.pos.y - p.pos.y + height/2;
      imageMode(CORNER);
      image(go.img, x, y);  // img is a 500x500 png
    }
}

The problem has to be inside those lines since everything is normal when i comment out the image() function. As I said framedrops appear when the object leaves the screen at the top or left (not right or bottom). Once it’s completely out of sight the frames are back at 60. I am honestly confused and would be very thankful for an answer.

You might need to dig into the source code for the PImage to see how it perform calculations in CORNER mode for negative values. An anlternative, which is untested, and requires a bit of extra work on your side, is to avoid the negative values in the first place.You need to only show the image that is within the sketch area. To do so, you need the code that I propose below. Notice this is just a concept and you will need to fit it to your work

      float x = go.pos.x - p.pos.x + width/2;  
      float y = go.pos.y - p.pos.y + height/2;
      imageMode(CORNER);
      int ww=go.img.width;
      int hh=go.img.height;
      image(go.img.get( ww+x, ww+y,ww,hh), 0, 0);  //Display only the iamge that is vissible. Remark: : x and y must be negative

Notice the above only applies when x and y is negative. Notice it is untested and a full running code is needed to make sure it works properly and that indeed solves the frame rate drop.

I think the source code should be doing something similar but I just don’t know the details.

I am curious, do you experience the same effect if your go.img leaves your sketch in the opposite corner? I mean, leaving at the bottom right corner?

Lastly, if this solves the problem, then it would be a good idea to fill a bug report in the Processing site.

Kf

1 Like

Thank you for your suggestion, I think PImage.get() will fix the problem but I didn’t try it yet.

But no I don’t experience the same effect in the other corner. It really is just with negative coords. I got even more confused when I put together the following sample sketch and everything was fine. It represents pretty much what happens in my larger game project just with a constant velocity instead of user input.

I guess I have to keep making changes to my code until I discover where the problem hides. But as it looks like it’s not in the source code of processing itself (luckily).

PImage img;
PVector pos;
PVector vel;
PVector player;

void setup() {
  frameRate(60);
  size(800, 600);
  img = loadImage("img.png");
  pos = new PVector(600, 600);
  player = new PVector(800, 800);
  vel = new PVector(1, 1);
}

void draw() {
  background(0);

  player.add(vel);

  float x = pos.x - player.x + width/2;
  float y = pos.y - player.y + height/2;

  image(img, x, y);
  image(img, width/2, height/2);
  
  text("FPS: " + frameRate, 10, 10);
}

Thank you for your time

Bottom