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 @@ -54,6 +54,37 @@ public class StandardPlayfield implements Playfield {

private Consumer<List<Drawable>> drawablesChangedListener;

private long lastEntityDraw = 0;
private volatile boolean delayedEntityDraw = false;
graefjk marked this conversation as resolved.
Show resolved Hide resolved
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();
};

/**
* Initialize the playfield for the given simulation
*
Expand Down Expand Up @@ -83,9 +114,20 @@ 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 if the last draw has not occurred
* recently.
*/
public void drawEntities() {
if (!this.delayedEntityDraw) {
this.delayedEntityDraw = true;
new Thread(this.restartDelayedEntitiesDrawThreadRunnable).start();
}
}

/**
* 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 {
Expand Down Expand Up @@ -408,4 +450,23 @@ 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;
}
}
Loading