Skip to content

Commit

Permalink
⚡ Improve Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jul 10, 2023
1 parent e89ba2e commit 4db148f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/java/fr/xen0xys/models/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Computer(AlgorithmType algorithmType, List<Rod> rods) {
case INSERTION_SORT -> algorithm = new InsertionSort(this, rods);
default -> throw new IllegalStateException("Unexpected value: " + algorithmType);
}
this.runner = new Runner(algorithm::step, 10000, false);
this.runner = new Runner(algorithm::step, 20, true);
}

public void start(){
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/fr/xen0xys/ui/utils/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,34 @@ public Runner(@NotNull final Runnable action, @Range(from = 0, to=10000) final i
this.action = action;
this.aps = 1D/aps;
this.stability = stability;
this.apsQueue = EvictingQueue.create(1000);
this.apsQueue = EvictingQueue.create(aps);
}

@SuppressWarnings("BusyWait")
@Override
public void run() {
this.running = true;
long nextAction = (long) (System.currentTimeMillis() + this.aps * 1000L);
long start = System.currentTimeMillis();
long nextAction = (long) (System.nanoTime() + this.aps * 1_000_000_000L);
long start = System.nanoTime();
while (running){
if(!paused){
if(System.currentTimeMillis() >= nextAction){

if(System.nanoTime() >= nextAction){
this.action.run();
this.passedActions++;
if(this.stability)
nextAction = (long) (nextAction + this.aps * 1000L);
nextAction = (long) (nextAction + this.aps * 1_000_000_000L);
else
nextAction = (long) (System.currentTimeMillis() + this.aps * 1000L);
long end = System.currentTimeMillis();
nextAction = (long) (System.nanoTime() + this.aps * 1_000_000_000L);
long end = System.nanoTime();
this.queueLock.lock();
this.apsQueue.add(end - start);
this.queueLock.unlock();
start = System.currentTimeMillis();
try {
this.apsQueue.add(end - start);
} finally {
this.queueLock.unlock();
}
start = System.nanoTime();
}
}
try {
Thread.sleep(0, 100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Thread.yield();
}
this.apsQueue.clear();
}
Expand All @@ -87,19 +84,22 @@ public int getPassedActions() {
return passedActions;
}
public int getCurrentAps() {
return (int) (1D / (computeAverage() / 1000D));
return (int) (1D / (computeAverage() / 1_000_000_000D));
}

private double computeAverage() {
long sum = 0;
int count = this.apsQueue.size();
this.queueLock.lock();
for (long element : this.apsQueue)
sum += element;
this.queueLock.unlock();
try{
for (long element : this.apsQueue)
sum += element;
}finally {
this.queueLock.unlock();
}
if (count > 0)
return (double) sum / count;
else
return -1000;
return -1_000_000_000;
}
}
27 changes: 15 additions & 12 deletions src/main/java/fr/xen0xys/ui/views/AlgorithmView.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ public class AlgorithmView extends JPanel {
private final Runner graphicRunner;
private final Computer computer;

private final Font font = new Font("Roboto", Font.PLAIN, 18);
private final Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, 0, 0);
private final double spaceWidth = 1;
private final double spaces;

public AlgorithmView(GameWindow window, int rodCount) {
this.window = window;
this.graphicRunner = new Runner(this::repaint, 1000, false);
this.graphicRunner = new Runner(this::repaint, 300, true);
this.initPanel();
// Init rods
this.rods = new ArrayList<>();
this.initRods(rodCount);
this.computer = new Computer(AlgorithmType.INSERTION_SORT, this.rods);
this.computer = new Computer(AlgorithmType.SELECTION_SORT, this.rods);
// Init graphic values
this.spaces = this.rods.size() * spaceWidth;
// Start runners
this.graphicRunner.start();
this.computer.start();
Expand All @@ -53,32 +60,28 @@ private void initRods(int rodCount){
@Override
protected void paintComponent(@NotNull final Graphics g) {
super.paintComponent(g);
final Graphics2D g2d = (Graphics2D) g;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);
g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("Roboto", Font.PLAIN, 18));
g2d.setFont(font);
g2d.drawString("FPS : %d TPS : %d".formatted(this.graphicRunner.getCurrentAps(), this.computer.getRunner().getCurrentAps()), 15, 25);
this.drawRods(g2d);
}

private void drawRods(Graphics2D g) {
double spaceWidth = 2;
double spaces = this.rods.size() * spaceWidth;
double rodWidth = (this.getWidth() - spaces) / this.rods.size();
double rodWidth = (this.getWidth() - this.spaces) / this.rods.size();
double maxRodHeight = this.getHeight() - 50;
for(int i = 0; i < this.rods.size(); i++){
Rod rod = this.rods.get(i);
double x = i * spaceWidth + i * rodWidth;
double x = i * this.spaceWidth + i * rodWidth;
double height = maxRodHeight * rod.getValue() / this.rods.size();
double y = this.getHeight() - height;
g.setColor(rod.getColor());
g.fill(new Rectangle2D.Double(x, y, rodWidth, height));
rectangle2D.setRect(x, y, rodWidth, height);
g.fill(rectangle2D);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/fr/xen0xys/ui/views/MenuView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public MenuView(GameWindow frame) {
private void initComponents() {
this.startButton.setBounds(50, 250, 400, 50);
this.startButton.addActionListener(e -> {
this.frame.loadView(new AlgorithmView(this.frame, 64));
this.frame.loadView(new AlgorithmView(this.frame, 25));
});
this.add(this.startButton);
}
Expand Down

0 comments on commit 4db148f

Please sign in to comment.