From a69be98713aac48acf9d5ef0976bd58c8ae73511 Mon Sep 17 00:00:00 2001 From: drdev Date: Mon, 26 May 2014 14:56:25 +0000 Subject: [PATCH] Optimize rendering so objects outside visible area but within parent that's rendered aren't drawn --- forge-gui-mobile/src/forge/Forge.java | 10 +++++++++- forge-gui-mobile/src/forge/util/Utils.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/forge-gui-mobile/src/forge/Forge.java b/forge-gui-mobile/src/forge/Forge.java index 89dbd800d00..58e61b7496e 100644 --- a/forge-gui-mobile/src/forge/Forge.java +++ b/forge-gui-mobile/src/forge/Forge.java @@ -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; diff --git a/forge-gui-mobile/src/forge/util/Utils.java b/forge-gui-mobile/src/forge/util/Utils.java index 8515ce247af..9531d65689d 100644 --- a/forge-gui-mobile/src/forge/util/Utils.java +++ b/forge-gui-mobile/src/forge/util/Utils.java @@ -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; + } }