Optimize rendering so objects outside visible area but within parent that's rendered aren't drawn

This commit is contained in:
drdev
2014-05-26 14:56:25 +00:00
parent 34c20250a4
commit a69be98713
2 changed files with 24 additions and 1 deletions

View File

@@ -560,11 +560,13 @@ public class Forge implements ApplicationListener {
public static class Graphics {
private Rectangle bounds;
private Rectangle visibleBounds;
private int failedClipCount;
private float alphaComposite = 1;
private Graphics() {
bounds = new Rectangle(0, 0, screenWidth, screenHeight);
visibleBounds = new Rectangle(bounds);
}
public void startClip() {
@@ -595,8 +597,14 @@ public class Forge implements ApplicationListener {
bounds = new Rectangle(parentBounds.x + displayObj.getLeft(), parentBounds.y + displayObj.getTop(), displayObj.getWidth(), displayObj.getHeight());
displayObj.setScreenPosition(bounds.x, bounds.y);
if (bounds.overlaps(parentBounds)) { //avoid drawing object if it's not within visible region
Rectangle intersection = Utils.getIntersection(bounds, visibleBounds);
if (intersection != null) { //avoid drawing object if it's not within visible region
final Rectangle backup = visibleBounds;
visibleBounds = intersection;
displayObj.draw(this);
visibleBounds = backup;
}
bounds = parentBounds;

View File

@@ -1,6 +1,7 @@
package forge.util;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Utils {
@@ -84,4 +85,18 @@ public class Utils {
result.y = (p1.y + p2.y) / 2;
return result;
}
//get rectangle defining the interestion between two other rectangles
public static Rectangle getIntersection(Rectangle r1, Rectangle r2) {
float left = Math.max(r1.x, r2.x);
float right = Math.min(r1.x + r1.width, r2.x + r2.width);
if (right > left) {
float top = Math.max(r1.y, r2.y);
float bottom = Math.min(r1.y + r1.height, r2.y + r2.height);
if (bottom > top) {
return new Rectangle(left, top, right - left, bottom - top);
}
}
return null;
}
}