Skip to content

Commit

Permalink
Merge pull request #54 from FIUS/fix/rendering-bugs
Browse files Browse the repository at this point in the history
Fix rendering bugs
  • Loading branch information
buehlefs authored Oct 2, 2019
2 parents 3ed5126 + 5859a0c commit d3c25b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class TestTask implements Task {
@Override
public void prepare(Simulation sim) {
this.sim = sim;
sim.initialize();
sim.getPlayfield().addEntity(new Position(3, 4), new TestEntity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ private synchronized void switchTask(String newTaskName) {
final StandardSimulation simulation = new StandardSimulation(
playfield, newSimulationClock, entityProgramRegistry, entityProgramRunner
);
simulation.initialize();
final StandardTaskRunner taskRunner = new StandardTaskRunner(task, simulation);

simulation.setEntityDrawListener(this.entityDrawListener);
Expand All @@ -238,6 +239,7 @@ private synchronized void switchTask(String newTaskName) {
this.currentTaskName = newTaskName;
this.currentSimulation = simulation;
this.currentRunningTask = runningTask;
this.gameWindow.setWindowTitle("Task: " + newTaskName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
Expand All @@ -28,6 +29,7 @@
import java.util.stream.Collectors;

import javax.swing.JPanel;
import javax.swing.RepaintManager;

import de.unistuttgart.informatik.fius.icge.ui.Drawable;
import de.unistuttgart.informatik.fius.icge.ui.PlayfieldDrawer;
Expand Down Expand Up @@ -61,7 +63,9 @@ public class SwingPlayfieldDrawer extends JPanel implements PlayfieldDrawer {
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON
);

private SwingTextureRegistry textureRegistry;
private final RepaintManager repaintManager;

private final SwingTextureRegistry textureRegistry;

// current display offset and zoom
private double offsetX = SwingPlayfieldDrawer.CELL_SIZE;
Expand Down Expand Up @@ -105,6 +109,9 @@ public void draw(long tickCount) {
SwingPlayfieldDrawer.this.draw(tickCount);
}
});

this.setOpaque(true);
this.repaintManager = RepaintManager.currentManager(this);
}

/**
Expand Down Expand Up @@ -173,8 +180,7 @@ public void mouseWheelMoved(MouseWheelEvent e) {

@Override
public void setDrawables(final List<Drawable> drawables) {
drawables.sort((a, b) -> a.compareTo(b));
this.drawables = drawables;
this.drawables = drawables.stream().sorted((a, b) -> a.compareTo(b)).collect(Collectors.toUnmodifiableList());
this.animatedDrawables = drawables.stream()
.filter(d -> d.isAnimated() || this.textureRegistry.isTextureAnimated(d.getTextureHandle())).collect(Collectors.toList());
this.fullRepaintNeeded = true;
Expand All @@ -183,11 +189,14 @@ public void setDrawables(final List<Drawable> drawables) {
@Override
public void draw(long tickCount) {
this.currentFrame = tickCount;
boolean bufferEnabled = this.repaintManager.isDoubleBufferingEnabled();
this.repaintManager.setDoubleBufferingEnabled(false);
if (this.fullRepaintNeeded) {
repaint(10);
final Rectangle visible = this.getVisibleRect();
this.paintImmediately(visible);
this.fullRepaintNeeded = false;
} else {
this.drawables.sort((a, b) -> a.compareTo(b));
this.drawables = this.drawables.stream().sorted((a, b) -> a.compareTo(b)).collect(Collectors.toUnmodifiableList());
if (this.animatedDrawables.size() > 0) {
final Rectangle visible = this.getVisibleRect();
final double cellSize = SwingPlayfieldDrawer.CELL_SIZE * this.scale;
Expand All @@ -203,16 +212,19 @@ public void draw(long tickCount) {
});
if (rectToDraw.isPresent()) {
if (this.lastRedrawArea != null) {
this.repaint(this.lastRedrawArea);
this.paintImmediately(this.lastRedrawArea);
}
this.lastRedrawArea = rectToDraw.get();
this.repaint(rectToDraw.get());
Rectangle toDraw = rectToDraw.get();
this.paintImmediately(toDraw);
} else {
this.lastRedrawArea = null;
}
}
}

// flush drawing changes to screen (improves render latency when mouse is not in window)
Toolkit.getDefaultToolkit().sync();
this.repaintManager.setDoubleBufferingEnabled(bufferEnabled);
}

@Override
Expand Down

0 comments on commit d3c25b8

Please sign in to comment.