Huge CPU usage reduction by using view clamp

v5stable
Ondřej Hruška 11 years ago
parent d67b382f43
commit 34e1f3c797
  1. 2
      src/mightypork/gamecore/render/Render.java
  2. 29
      src/mightypork/rogue/world/WorldRenderer.java
  3. 22
      src/mightypork/rogue/world/level/render/MapRenderContext.java
  4. 21
      src/mightypork/util/constraints/rect/mutable/RectMutable.java
  5. 8
      src/mightypork/util/constraints/rect/mutable/RectVar.java

@ -449,9 +449,9 @@ public class Render {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
txquad.tx.bind(); txquad.tx.bind();
glBegin(GL_QUADS); glBegin(GL_QUADS);
setColor(tint);
} }
setColor(tint);
final RectDigest q = quad.digest(); final RectDigest q = quad.digest();
final RectDigest u = txquad.uvs.digest(); final RectDigest u = txquad.uvs.digest();

@ -82,7 +82,7 @@ public class WorldRenderer extends RectProxy implements Pollable {
} }
private int[] getOffset() private Vect getOffset()
{ {
WorldPos pos = player.getPosition(); WorldPos pos = player.getPosition();
final double playerX = pos.getVisualX(); final double playerX = pos.getVisualX();
@ -90,29 +90,26 @@ public class WorldRenderer extends RectProxy implements Pollable {
double ts = tileSize.value(); double ts = tileSize.value();
return new int[]{(int) (-ts * playerX), (int) (-ts * playerY)}; return Vect.make((-ts * playerX), (-ts * playerY));
} }
public void render() public void render()
{ {
int[] off = getOffset();
System.out.println(trc.getRectForTile(10, 10));
Render.pushMatrix(); Render.pushMatrix();
Render.translate(off[0], off[1]); Render.setColor(RGB.WHITE);
Render.translate(getOffset());
// tiles to render // tiles to render
final WorldPos pos = player.getPosition(); final WorldPos pos = player.getPosition();
final double w = width().value(); final double w = width().value();
final double h = height().value(); final double h = height().value();
final double ts = tileSize.value(); final double ts = tileSize.value();
final double tsh = ts / 2;
final int x1 = (int) Math.floor(pos.x - (w / tsh)); final int x1 = (int) Math.floor(pos.x - (w / (ts*2)));
final int y1 = (int) Math.floor(pos.y - (h / tsh)); final int y1 = (int) Math.floor(pos.y - (h / (ts*2)));
final int x2 = (int) Math.ceil(pos.x + (w / tsh)); final int x2 = (int) Math.ceil(pos.x + (w / (ts*2)));
final int y2 = (int) Math.ceil(pos.y + (h / tsh)); final int y2 = (int) Math.ceil(pos.y + (h / (ts*2)))+1;
// === TILES === // === TILES ===
@ -121,11 +118,16 @@ public class WorldRenderer extends RectProxy implements Pollable {
Render.enterBatchTexturedQuadMode(Res.getTexture("tiles16")); Render.enterBatchTexturedQuadMode(Res.getTexture("tiles16"));
} }
Render.setColor(RGB.WHITE);
int c = 0;
for (trc.y = y1; trc.y <= y2; trc.y++) { for (trc.y = y1; trc.y <= y2; trc.y++) {
for (trc.x = x1; trc.x <= x2; trc.x++) { for (trc.x = x1; trc.x <= x2; trc.x++) {
trc.renderTile(); trc.renderTile();
c++;
} }
} }
System.out.println(c);
if (USE_BATCH_RENDERING) { if (USE_BATCH_RENDERING) {
Render.leaveBatchTexturedQuadMode(); Render.leaveBatchTexturedQuadMode();
@ -167,8 +169,7 @@ public class WorldRenderer extends RectProxy implements Pollable {
public WorldPos getClickedTile(Vect clickPos) public WorldPos getClickedTile(Vect clickPos)
{ {
int[] off = getOffset(); Vect v = clickPos.sub(mapRect.origin().add(getOffset()));
Vect v = clickPos.sub(mapRect.origin().add(off[0], off[1]));
int ts = (int) tileSize.value(); int ts = (int) tileSize.value();
return new WorldPos(v.xi() / ts, v.yi() / ts); return new WorldPos(v.xi() / ts, v.yi() / ts);
} }

@ -3,7 +3,6 @@ package mightypork.rogue.world.level.render;
import mightypork.rogue.world.level.MapAccess; import mightypork.rogue.world.level.MapAccess;
import mightypork.util.constraints.rect.Rect; import mightypork.util.constraints.rect.Rect;
import mightypork.util.constraints.rect.RectConst;
import mightypork.util.constraints.rect.builders.TiledRect; import mightypork.util.constraints.rect.builders.TiledRect;
@ -13,33 +12,18 @@ public abstract class MapRenderContext {
protected final TiledRect tiler; protected final TiledRect tiler;
private final Rect mapRect; private final Rect mapRect;
private RectConst tileRects[][];
public MapRenderContext(MapAccess map, Rect drawArea) { public MapRenderContext(MapAccess map, Rect drawArea) {
this.map = map; this.map = map;
this.tiler = drawArea.tiles(map.getWidth(), map.getHeight()); this.tiler = drawArea.tiles(map.getWidth(), map.getHeight());
this.mapRect = drawArea; this.mapRect = drawArea;
tileRects = new RectConst[map.getHeight()][map.getWidth()];
rebuildTileRects();
}
public void rebuildTileRects()
{
for(int y=0;y<map.getHeight();y++) {
for(int x=0;x<map.getWidth();x++) {
tileRects[y][x] = tiler.tile(x, y).freeze();
}
}
} }
public Rect getRectForTile(int x, int y) public Rect getRectForTile(int x, int y)
{ {
return tileRects[y][x]; return tiler.tile(x, y);
} }

@ -71,20 +71,17 @@ public abstract class RectMutable extends Rect {
setTo(Vect.ZERO, Vect.ZERO); setTo(Vect.ZERO, Vect.ZERO);
} }
public abstract void setOrigin(double x, double y);
/** public void setOrigin(Vect origin) {
* Set new origin setOrigin(origin.x(), origin.y());
* }
* @param origin new origin
*/
public abstract void setOrigin(Vect origin);
/** public void setSize(Vect size) {
* Set new size setSize(size.x(), size.y());
* }
* @param size new size
*/
public abstract void setSize(Vect size);
public abstract void setSize(double x, double y);
} }

@ -41,15 +41,15 @@ public class RectVar extends RectMutable {
@Override @Override
public void setOrigin(Vect origin) public void setOrigin(double x, double y)
{ {
this.pos.setTo(origin); this.pos.setTo(x,y);
} }
@Override @Override
public void setSize(Vect size) public void setSize(double x, double y)
{ {
this.size.setTo(size); this.size.setTo(x,y);
} }
} }

Loading…
Cancel
Save