Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit draw updates #202

Merged
merged 15 commits into from
Sep 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -54,6 +56,9 @@ public class StandardPlayfield implements Playfield {

private Consumer<List<Drawable>> drawablesChangedListener;

private boolean awaitingEntityDraw = false;
private long timeBetweenDraws = 32; //the time between draw calls in milliseconds

/**
* Initialize the playfield for the given simulation
*
Expand All @@ -68,6 +73,15 @@ public void initialize(final StandardSimulation simulation) {
});

this.simualtionTreeRootNode = new SimulationTreeNode("root", "Entities", "", false);

new Timer().scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
drawEntitiesInternal();
}

}, 0, this.timeBetweenDraws);
}

/**
Expand All @@ -83,9 +97,20 @@ public Simulation getSimulation() {
}

/**
* Converts all entities to drawables and sends them to the playfield drawer.
* Queues a draw update to the playfield drawer. Draw updates are sent to the playfield drawer automatically every
* 32ms.
*/
public void drawEntities() {
this.awaitingEntityDraw = true;
}

/**
* Converts all entities to drawables and sends them to the playfield drawer.
*/
private void drawEntitiesInternal() {
if (!this.awaitingEntityDraw) { //fast exit as default
return; // no updates to draw
}
final List<Drawable> drawables = new ArrayList<>();
for (final Entity entity : this.getAllEntities()) {
try {
Expand All @@ -102,6 +127,7 @@ public void drawEntities() {
} catch (@SuppressWarnings("unused") final IllegalStateException e) {
//If we are not attached to a simultion we do not need to draw anything
}

}

@Override
Expand Down