Skip to content

Commit

Permalink
Improve memory usage of the GridRenderer
Browse files Browse the repository at this point in the history
Instead of caching the entire grid image, cache and render the Grid per tile.
Some of the grid images consumed around 150MB of memory...

Issue #428, Issue #404
  • Loading branch information
steffen-wilke committed Dec 28, 2021
1 parent b3c47f9 commit a1571c1
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.ConcurrentHashMap;

public class GridRenderer implements IEditorRenderer {

private final Map<String, GridImages> gridCache = new ConcurrentHashMap<>();

@Override
Expand Down Expand Up @@ -57,12 +58,18 @@ public void render(Graphics2D g) {
: camera.getRenderScale() > GridImages.RENDERSCALE_MID
? images.getMidImage()
: images.getSmallImage();
ImageRenderer.renderScaled(
g,
image,
viewPortLocation.getX() * camera.getRenderScale(),
viewPortLocation.getY() * Game.world().camera().getRenderScale(),
camera.getRenderScale() / scale);

for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
ImageRenderer.renderScaled(
g,
image,
(viewPortLocation.getX() + (x * map.getTileWidth())) * camera.getRenderScale(),
(viewPortLocation.getY() + (y * map.getTileHeight())) * Game.world().camera().getRenderScale(),
camera.getRenderScale() / scale);
}
}

}
}

Expand All @@ -71,6 +78,7 @@ public void clearCache() {
}

private static class GridImages {

private static final float RENDERSCALE_LARGE = 6;
private static final float RENDERSCALE_MID = 1.5f;

Expand Down Expand Up @@ -102,20 +110,15 @@ public BufferedImage getLargeImage() {
private static BufferedImage createImage(IMap map, float scale) {
BufferedImage image =
Imaging.getCompatibleImage(
(int) (map.getSizeInPixels().width * scale) + 1,
(int) (map.getSizeInPixels().height * scale) + 1);
(int) (map.getTileWidth() * scale) + 1,
(int) (map.getTileHeight() * scale) + 1);
Graphics2D graphics = (Graphics2D) image.getGraphics();

final float lineSize = Editor.preferences().getGridLineWidth() / scale;
final Stroke stroke = new BasicStroke(lineSize);
graphics.setColor(Editor.preferences().getGridColor());
for (int x = 0; x < map.getWidth(); x++) {
for (int y = 0; y < map.getHeight(); y++) {
Shape tile = map.getOrientation().getShape(x, y, map);
ShapeRenderer.renderOutlineTransformed(
graphics, tile, AffineTransform.getScaleInstance(scale, scale), stroke);
}
}
Shape tile = map.getOrientation().getShape(0, 0, map);
ShapeRenderer.renderOutlineTransformed(graphics, tile, AffineTransform.getScaleInstance(scale, scale), stroke);

graphics.dispose();

Expand Down

0 comments on commit a1571c1

Please sign in to comment.