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,24 +97,33 @@ public Simulation getSimulation() {
}

/**
* Converts all entities to drawables and sends them to the playfield drawer.
* Converts all entities to drawables and sends them to the playfield drawer every 32ms.
graefjk marked this conversation as resolved.
Show resolved Hide resolved
*/
public void drawEntities() {
final List<Drawable> drawables = new ArrayList<>();
for (final Entity entity : this.getAllEntities()) {
try {
drawables.add(entity.getDrawInformation());
} catch (@SuppressWarnings("unused") final EntityNotOnFieldException e) {
//Entity has been removed from the field while this loop was running.
//Just don't draw it and ignore the exception.
this.awaitingEntityDraw = true;
}

/**
* Converts all entities to drawables and sends them to the playfield drawer.
*/
private void drawEntitiesInternal() {
if (this.awaitingEntityDraw) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer the fast exit form over nesting everything in an if. This makes the method easier to read, as all the fail conditions are at the top and can be ignored for the rest of the method body.

if (!this.awaitingEntityDraw) {
    return; // no updates to draw
}
// rest of the method

final List<Drawable> drawables = new ArrayList<>();
for (final Entity entity : this.getAllEntities()) {
try {
drawables.add(entity.getDrawInformation());
} catch (@SuppressWarnings("unused") final EntityNotOnFieldException e) {
//Entity has been removed from the field while this loop was running.
//Just don't draw it and ignore the exception.
}
}
}
try {
if (this.drawablesChangedListener != null) {
this.drawablesChangedListener.accept(drawables);
try {
if (this.drawablesChangedListener != null) {
this.drawablesChangedListener.accept(drawables);
}
} catch (@SuppressWarnings("unused") final IllegalStateException e) {
//If we are not attached to a simultion we do not need to draw anything
}
} catch (@SuppressWarnings("unused") final IllegalStateException e) {
//If we are not attached to a simultion we do not need to draw anything
}
}

Expand Down