Reputation: 3
The more images are currently in the sketch the slower image() runs which results in framerate lowering. What could be causing this?
//for each obstacle
for (int i=0; i<obsList.size(); i+=1){
ObstacleInstance = obsList.get(i);
//if inside moving zone
if (obsList.get(i).ObstacleX < 1920 + 400 && 0 - 400
obsList.get(i).ObstacleX) {
ObstacleInstance.move();
//draws image in sketch; has performance issue
image(ObstacleInstance.ObstacleImg,
ObstacleInstance.ObstacleX,
ObstacleInstance.ObstacleHeightFromTop,
ObstacleInstance.ObstacleSizeX,
ObstacleInstance.ObstacleSizeY);
}
}
}
PS: used timer to see the time image() took to run and it increased with more images and decreased after removing them.
Upvotes: 0
Views: 42
Reputation: 109567
One remark first: variables and fields should start with a small letter.
Every drawn image costs.
If the width and height of an object is not fixed (400), use something like:
final int visibleX = 0;
final int visibleY = 0;
final int visibleSizeX = 1920;
final int visibleSizeY = 1080;
for (Obstacle obs: obsList) {
if (obs.ObstacleX < visibleX + visibleSizeX
&& obs.ObstacleX + obs.ObstacleSizeX > visibleX) {
&& obs.ObstacleY < visibleY + visibleSizeY
&& obs.ObstacleY + obs.ObstacleSizeY > visibleY) {
ObstacleInstance.move();
image(obs.ObstacleImg,
obs.ObstacleX,
obs.ObstacleHeightFromTop,
obs.ObstacleSizeX,
obs.ObstacleSizeY);
}
}
Overlapped drawing costs too, but is hard to optimize, other than with regions, collected areas (rectangles here).
Then the image
call seems to be slow.
It seems you are working with some visualization library.
System.arraycopy
is faster than a for-loop.To check the speed, it would be nice to draw rectangle outlines i.o. images.
And move()
should (hopefully) be substantial, not 0 or 1.
Upvotes: 1