From 34e1f3c797342058d16444932f95ed65d652ba41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 23 Apr 2014 20:52:48 +0200 Subject: [PATCH] Huge CPU usage reduction by using view clamp --- src/mightypork/gamecore/render/Render.java | 2 +- src/mightypork/rogue/world/WorldRenderer.java | 29 ++++++++++--------- .../world/level/render/MapRenderContext.java | 22 ++------------ .../constraints/rect/mutable/RectMutable.java | 21 ++++++-------- .../constraints/rect/mutable/RectVar.java | 8 ++--- 5 files changed, 32 insertions(+), 50 deletions(-) diff --git a/src/mightypork/gamecore/render/Render.java b/src/mightypork/gamecore/render/Render.java index 7ef82c5..d7a8656 100644 --- a/src/mightypork/gamecore/render/Render.java +++ b/src/mightypork/gamecore/render/Render.java @@ -449,9 +449,9 @@ public class Render { glEnable(GL_TEXTURE_2D); txquad.tx.bind(); glBegin(GL_QUADS); + setColor(tint); } - setColor(tint); final RectDigest q = quad.digest(); final RectDigest u = txquad.uvs.digest(); diff --git a/src/mightypork/rogue/world/WorldRenderer.java b/src/mightypork/rogue/world/WorldRenderer.java index 99d7282..9237fd2 100644 --- a/src/mightypork/rogue/world/WorldRenderer.java +++ b/src/mightypork/rogue/world/WorldRenderer.java @@ -82,7 +82,7 @@ public class WorldRenderer extends RectProxy implements Pollable { } - private int[] getOffset() + private Vect getOffset() { WorldPos pos = player.getPosition(); final double playerX = pos.getVisualX(); @@ -90,29 +90,26 @@ public class WorldRenderer extends RectProxy implements Pollable { double ts = tileSize.value(); - return new int[]{(int) (-ts * playerX), (int) (-ts * playerY)}; + return Vect.make((-ts * playerX), (-ts * playerY)); } public void render() - { - int[] off = getOffset(); - System.out.println(trc.getRectForTile(10, 10)); - + { Render.pushMatrix(); - Render.translate(off[0], off[1]); + Render.setColor(RGB.WHITE); + Render.translate(getOffset()); // tiles to render final WorldPos pos = player.getPosition(); final double w = width().value(); final double h = height().value(); final double ts = tileSize.value(); - final double tsh = ts / 2; - final int x1 = (int) Math.floor(pos.x - (w / tsh)); - final int y1 = (int) Math.floor(pos.y - (h / tsh)); - final int x2 = (int) Math.ceil(pos.x + (w / tsh)); - final int y2 = (int) Math.ceil(pos.y + (h / tsh)); + final int x1 = (int) Math.floor(pos.x - (w / (ts*2))); + final int y1 = (int) Math.floor(pos.y - (h / (ts*2))); + final int x2 = (int) Math.ceil(pos.x + (w / (ts*2))); + final int y2 = (int) Math.ceil(pos.y + (h / (ts*2)))+1; // === TILES === @@ -121,11 +118,16 @@ public class WorldRenderer extends RectProxy implements Pollable { Render.enterBatchTexturedQuadMode(Res.getTexture("tiles16")); } + Render.setColor(RGB.WHITE); + + int c = 0; for (trc.y = y1; trc.y <= y2; trc.y++) { for (trc.x = x1; trc.x <= x2; trc.x++) { trc.renderTile(); + c++; } } + System.out.println(c); if (USE_BATCH_RENDERING) { Render.leaveBatchTexturedQuadMode(); @@ -167,8 +169,7 @@ public class WorldRenderer extends RectProxy implements Pollable { public WorldPos getClickedTile(Vect clickPos) { - int[] off = getOffset(); - Vect v = clickPos.sub(mapRect.origin().add(off[0], off[1])); + Vect v = clickPos.sub(mapRect.origin().add(getOffset())); int ts = (int) tileSize.value(); return new WorldPos(v.xi() / ts, v.yi() / ts); } diff --git a/src/mightypork/rogue/world/level/render/MapRenderContext.java b/src/mightypork/rogue/world/level/render/MapRenderContext.java index 55bac08..5366aa2 100644 --- a/src/mightypork/rogue/world/level/render/MapRenderContext.java +++ b/src/mightypork/rogue/world/level/render/MapRenderContext.java @@ -3,7 +3,6 @@ package mightypork.rogue.world.level.render; import mightypork.rogue.world.level.MapAccess; import mightypork.util.constraints.rect.Rect; -import mightypork.util.constraints.rect.RectConst; import mightypork.util.constraints.rect.builders.TiledRect; @@ -13,33 +12,18 @@ public abstract class MapRenderContext { protected final TiledRect tiler; private final Rect mapRect; - private RectConst tileRects[][]; - public MapRenderContext(MapAccess map, Rect drawArea) { - this.map = map; + this.map = map; + this.tiler = drawArea.tiles(map.getWidth(), map.getHeight()); this.mapRect = drawArea; - - tileRects = new RectConst[map.getHeight()][map.getWidth()]; - - rebuildTileRects(); - } - - - public void rebuildTileRects() - { - for(int y=0;y