Skip to content

Commit

Permalink
Don't overwrite the transform of the Graphics2D object
Browse files Browse the repository at this point in the history
When rendering shapes, don't overwrite the AffineTransform object of the Graphics2D object. Instead only transform the single Shape to the desired state.

Fixes #275
  • Loading branch information
steffen-wilke committed Jan 31, 2021
1 parent 0af1561 commit 07eec17
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
71 changes: 41 additions & 30 deletions src/de/gurkenlabs/litiengine/environment/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public final class Environment implements IRenderable {

private int gravity;

private boolean rendering;

static {
registerMapObjectLoader(new PropMapObjectLoader());
registerMapObjectLoader(new CollisionBoxMapObjectLoader());
Expand Down Expand Up @@ -2278,48 +2280,53 @@ public void removeRenderable(final IRenderable renderable) {
public void render(final Graphics2D g) {
long renderStart = System.nanoTime();

AffineTransform otx = g.getTransform();
g.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());
if (this.getMap() != null && this.getMap().getBackgroundColor() != null) {
g.setColor(this.getMap().getBackgroundColor());
g.fill(new Rectangle2D.Double(0.0, 0.0, Game.world().camera().getViewport().getWidth(), Game.world().camera().getViewport().getHeight()));
}
final AffineTransform otx = g.getTransform();
this.rendering = true;

this.render(g, RenderType.BACKGROUND);
try {
g.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());
if (this.getMap() != null && this.getMap().getBackgroundColor() != null) {
g.setColor(this.getMap().getBackgroundColor());
g.fill(new Rectangle2D.Double(0.0, 0.0, Game.world().camera().getViewport().getWidth(), Game.world().camera().getViewport().getHeight()));
}

this.render(g, RenderType.GROUND);
DebugRenderer.renderMapDebugInfo(g, this.getMap());
this.render(g, RenderType.BACKGROUND);

this.render(g, RenderType.SURFACE);
this.render(g, RenderType.NORMAL);
this.render(g, RenderType.OVERLAY);
this.render(g, RenderType.GROUND);
DebugRenderer.renderMapDebugInfo(g, this.getMap());

long ambientStart = System.nanoTime();
if (Game.config().graphics().getGraphicQuality().ordinal() >= Quality.MEDIUM.ordinal() && this.getAmbientLight() != null && this.getAmbientLight().getColor().getAlpha() != 0) {
this.getAmbientLight().render(g);
}
this.render(g, RenderType.SURFACE);
this.render(g, RenderType.NORMAL);
this.render(g, RenderType.OVERLAY);

final double ambientTime = TimeUtilities.nanoToMs(System.nanoTime() - ambientStart);
long ambientStart = System.nanoTime();
if (Game.config().graphics().getGraphicQuality().ordinal() >= Quality.MEDIUM.ordinal() && this.getAmbientLight() != null && this.getAmbientLight().getColor().getAlpha() != 0) {
this.getAmbientLight().render(g);
}

long shadowRenderStart = System.nanoTime();
if (this.getStaticShadows().stream().anyMatch(x -> x.getShadowType() != StaticShadowType.NONE)) {
this.getStaticShadowLayer().render(g);
}
final double ambientTime = TimeUtilities.nanoToMs(System.nanoTime() - ambientStart);

final double shadowTime = TimeUtilities.nanoToMs(System.nanoTime() - shadowRenderStart);
long shadowRenderStart = System.nanoTime();
if (this.getStaticShadows().stream().anyMatch(x -> x.getShadowType() != StaticShadowType.NONE)) {
this.getStaticShadowLayer().render(g);
}

this.render(g, RenderType.UI);
final double shadowTime = TimeUtilities.nanoToMs(System.nanoTime() - shadowRenderStart);

if (Game.config().debug().trackRenderTimes()) {
this.render(g, RenderType.UI);

final double totalRenderTime = TimeUtilities.nanoToMs(System.nanoTime() - renderStart);
if (Game.config().debug().trackRenderTimes()) {

Game.metrics().trackRenderTime("shadow", shadowTime);
Game.metrics().trackRenderTime("ambient", ambientTime);
Game.metrics().trackRenderTime("world", totalRenderTime);
}
final double totalRenderTime = TimeUtilities.nanoToMs(System.nanoTime() - renderStart);

g.setTransform(otx);
Game.metrics().trackRenderTime("shadow", shadowTime);
Game.metrics().trackRenderTime("ambient", ambientTime);
Game.metrics().trackRenderTime("world", totalRenderTime);
}
} finally {
this.rendering = false;
g.setTransform(otx);
}
}

/**
Expand Down Expand Up @@ -2387,6 +2394,10 @@ public void unload() {
this.fireEvent(l -> l.unloaded(this));
}

public boolean isRendering(){
return this.rendering;
}

private static <T extends IEntity> T getById(Collection<T> entities, int mapId) {
for (final T m : entities) {
if (m.getMapId() == mapId) {
Expand Down
11 changes: 9 additions & 2 deletions src/de/gurkenlabs/litiengine/graphics/RenderEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ public void renderShape(final Graphics2D g, final Shape shape, boolean antialias
Object hint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
final AffineTransform t = new AffineTransform();
t.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());
if(Game.world().environment() == null || !Game.world().environment().isRendering()){
t.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());
}

t.translate(Game.world().camera().getPixelOffsetX(), Game.world().camera().getPixelOffsetY());
t.rotate(Math.toRadians(angle), shape.getBounds().getX() + shape.getBounds().getWidth() * 0.5, shape.getBounds().getY() + shape.getBounds().getHeight() * 0.5);

Expand Down Expand Up @@ -348,7 +351,11 @@ public void renderOutline(final Graphics2D g, final Shape shape, final Stroke st
Object hint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
final AffineTransform t = new AffineTransform();
t.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());

if(Game.world().environment() == null || !Game.world().environment().isRendering()){
t.scale(Game.world().camera().getRenderScale(), Game.world().camera().getRenderScale());
}

t.translate(Game.world().camera().getPixelOffsetX(), Game.world().camera().getPixelOffsetY());
t.rotate(Math.toRadians(angle), shape.getBounds().getX() + shape.getBounds().getWidth() * 0.5, shape.getBounds().getY() + shape.getBounds().getHeight() * 0.5);

Expand Down
11 changes: 3 additions & 8 deletions src/de/gurkenlabs/litiengine/graphics/ShapeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ public static void renderOutline(final Graphics2D g, final Shape shape, final St
}

public static void renderTransformed(final Graphics2D g, final Shape shape, AffineTransform transform) {
final AffineTransform oldTransForm = g.getTransform();
g.setTransform(transform);
render(g, shape);
g.setTransform(oldTransForm);

render(g, transform.createTransformedShape(shape));
}

public static void renderOutlineTransformed(final Graphics2D g, final Shape shape, AffineTransform transform) {
Expand All @@ -70,9 +68,6 @@ public static void renderOutlineTransformed(final Graphics2D g, final Shape shap
return;
}

final AffineTransform oldTransForm = g.getTransform();
g.setTransform(transform);
renderOutline(g, shape, stroke);
g.setTransform(oldTransForm);
renderOutline(g, transform.createTransformedShape(shape), stroke);
}
}

0 comments on commit 07eec17

Please sign in to comment.