From 54eba0226e41e6fadb379c3d782be7d134bf5eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=BChler?= <17296905+buehlefs@users.noreply.github.com> Date: Wed, 2 Oct 2019 15:01:53 +0200 Subject: [PATCH 1/2] Fix window title not set and simulation initialization Closes #45 Closes #47 --- .../unistuttgart/informatik/fius/icge/manualstart/TestTask.java | 1 - .../fius/icge/simulation/internal/StandardSimulationProxy.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ICGE-ManualStart/src/main/java/de/unistuttgart/informatik/fius/icge/manualstart/TestTask.java b/ICGE-ManualStart/src/main/java/de/unistuttgart/informatik/fius/icge/manualstart/TestTask.java index 3be1e34fb..ea2d444c8 100644 --- a/ICGE-ManualStart/src/main/java/de/unistuttgart/informatik/fius/icge/manualstart/TestTask.java +++ b/ICGE-ManualStart/src/main/java/de/unistuttgart/informatik/fius/icge/manualstart/TestTask.java @@ -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()); } diff --git a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulationProxy.java b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulationProxy.java index 7c9879faa..178a33d3a 100644 --- a/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulationProxy.java +++ b/ICGE-Simulation/src/main/java/de/unistuttgart/informatik/fius/icge/simulation/internal/StandardSimulationProxy.java @@ -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); @@ -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 From 5859a0c67f3260221bb2b5b426a664ffa65208b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=BChler?= <17296905+buehlefs@users.noreply.github.com> Date: Wed, 2 Oct 2019 15:06:40 +0200 Subject: [PATCH 2/2] Fix rendering latency Fixes changes not beeing displayed on playfield immediately Always create a new list when sorting drawables: Closes #51 --- .../ui/internal/SwingPlayfieldDrawer.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/ICGE-Ui/src/main/java/de/unistuttgart/informatik/fius/icge/ui/internal/SwingPlayfieldDrawer.java b/ICGE-Ui/src/main/java/de/unistuttgart/informatik/fius/icge/ui/internal/SwingPlayfieldDrawer.java index c7de97c7e..8f7e91037 100644 --- a/ICGE-Ui/src/main/java/de/unistuttgart/informatik/fius/icge/ui/internal/SwingPlayfieldDrawer.java +++ b/ICGE-Ui/src/main/java/de/unistuttgart/informatik/fius/icge/ui/internal/SwingPlayfieldDrawer.java @@ -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; @@ -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; @@ -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; @@ -105,6 +109,9 @@ public void draw(long tickCount) { SwingPlayfieldDrawer.this.draw(tickCount); } }); + + this.setOpaque(true); + this.repaintManager = RepaintManager.currentManager(this); } /** @@ -173,8 +180,7 @@ public void mouseWheelMoved(MouseWheelEvent e) { @Override public void setDrawables(final List 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; @@ -183,11 +189,14 @@ public void setDrawables(final List 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; @@ -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