Refactor Context and GameRunner
This commit is contained in:
@@ -4,9 +4,12 @@ import com.bartlomiejpluta.base.api.game.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
|
||||
public interface Context {
|
||||
GameRunner getGameRunner();
|
||||
|
||||
Screen getScreen();
|
||||
|
||||
Camera getCamera();
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.bartlomiejpluta.base.api.game.runner;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||
|
||||
public interface GameRunner {
|
||||
public interface GameRunner extends Disposable {
|
||||
void init(Context context);
|
||||
|
||||
void input(Screen screen);
|
||||
|
||||
void update(float dt);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.bartlomiejpluta.base.api.util.profiler;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static java.util.function.Function.identity;
|
||||
import static java.util.stream.Collectors.counting;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
|
||||
public class FPSProfiler {
|
||||
private static final int MOD = 30;
|
||||
private final List<Double> values = new LinkedList<>();
|
||||
private float fpsAccumulator = 0;
|
||||
private int pointer = 0;
|
||||
private double fps = 0;
|
||||
|
||||
public void update(float dt) {
|
||||
fpsAccumulator += dt;
|
||||
|
||||
if (++pointer % MOD == 0) {
|
||||
fps = pointer / fpsAccumulator;
|
||||
fpsAccumulator = 0;
|
||||
pointer = 0;
|
||||
|
||||
values.add(fps);
|
||||
}
|
||||
}
|
||||
|
||||
public void printResult() {
|
||||
System.out.format("Min FPS: %f, max FPS: %f, avg FPS: %f",
|
||||
values.stream().min(Double::compareTo).orElse(-1.0),
|
||||
values.stream().max(Double::compareTo).orElse(-1.0),
|
||||
totalAverage()
|
||||
);
|
||||
|
||||
printHistogram();
|
||||
}
|
||||
|
||||
private double totalAverage() {
|
||||
return values.stream().reduce(0.0, Double::sum) / values.size();
|
||||
}
|
||||
|
||||
private void printHistogram() {
|
||||
values
|
||||
.stream()
|
||||
.mapToInt(Double::intValue)
|
||||
.boxed()
|
||||
.collect(groupingBy(identity(), counting()))
|
||||
.entrySet()
|
||||
.stream()
|
||||
.sorted(comparingInt(Map.Entry::getKey))
|
||||
.forEach(e -> System.out.printf("%s FPS: %f%% (%d occurrences)", e.getKey(), e.getValue() * 100.0f / values.size(), e.getValue()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.bartlomiejpluta.base.api.util.profiler;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class TimeProfiler {
|
||||
private static final DecimalFormat DF = new DecimalFormat("0.00");
|
||||
private final Map<String, Double> averages = new HashMap<>();
|
||||
|
||||
public void measure(String key, Runnable task) {
|
||||
var start = System.nanoTime();
|
||||
task.run();
|
||||
var time = System.nanoTime() - start;
|
||||
|
||||
if (!averages.containsKey(key)) {
|
||||
averages.put(key, time * 1.0);
|
||||
} else {
|
||||
averages.put(key, (averages.get(key) + time) / 2.0);
|
||||
}
|
||||
}
|
||||
|
||||
public void printResult() {
|
||||
averages.entrySet().stream()
|
||||
.sorted(Entry.<String, Double>comparingByValue().reversed())
|
||||
.forEachOrdered(entry -> System.out.format("[%s]: [%sms] [%sus] [%sns]",
|
||||
entry.getKey(),
|
||||
DF.format(entry.getValue() / 1_000_000),
|
||||
DF.format(entry.getValue() / 1_000),
|
||||
DF.format(entry.getValue())
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user