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,36 +56,8 @@ public class StandardPlayfield implements Playfield {

private Consumer<List<Drawable>> drawablesChangedListener;

private long lastEntityDraw = 0;
private volatile boolean delayedEntityDraw = false;
private long timeBetweenDraws = 32; //the time between draw calls in milliseconds
private Thread delayedDrawEntitiesThread = new Thread();

private Runnable delayedEntitiesDrawRunnable = () -> {
long timeSinceLastEntityDraw = System.nanoTime() - this.lastEntityDraw;
if (timeSinceLastEntityDraw < this.timeBetweenDraws * 1000000) {
try {
Thread.sleep(this.timeBetweenDraws - timeSinceLastEntityDraw / 1000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.delayedEntityDraw = false;
drawEntitiesInternal();
this.lastEntityDraw = System.nanoTime();
};

private Runnable restartDelayedEntitiesDrawThreadRunnable = () -> {
try {
this.delayedDrawEntitiesThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.delayedDrawEntitiesThread = new Thread(this.delayedEntitiesDrawRunnable);
this.delayedDrawEntitiesThread.start();
};
private boolean awaitingEntityDraw = false;
private long timeBetweenDraws = 32; //the time between draw calls in milliseconds

/**
* Initialize the playfield for the given simulation
Expand All @@ -99,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 @@ -114,35 +97,33 @@ public Simulation getSimulation() {
}

/**
* Converts all entities to drawables and sends them to the playfield drawer if the last draw has not occurred
* recently.
* 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() {
if (!this.delayedEntityDraw) {
this.delayedEntityDraw = true;
new Thread(this.restartDelayedEntitiesDrawThreadRunnable).start();
}
this.awaitingEntityDraw = true;
}

/**
* Converts all entities to drawables and sends them to the playfield drawer.
*/
private void drawEntitiesInternal() {
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.
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 Expand Up @@ -450,23 +431,4 @@ public void removeDrawablesChangedListener() {
public String toString() {
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode());
}

/**
* Get's {@link #timeBetweenDraws timeBetweenDraws}
*
* @return time waiting between draw calls in milliseconds
*/
public long getTimeBetweenDraws() {
return this.timeBetweenDraws;
}

/**
* Set's {@link #timeBetweenDraws timeBetweenDraws}
*
* @param timeBetweenDraws
* time waiting between draw calls in milliseconds
*/
public void setTimeBetweenDraws(long timeBetweenDraws) {
this.timeBetweenDraws = timeBetweenDraws;
}
}