Fix delta time and FPS count calculation
Some checks failed
build-and-test / build (push) Failing after 2m0s
build-and-test / generate-javadoc (push) Failing after 2m3s
build-and-test / test (push) Failing after 2m5s

This commit is contained in:
JeremyStar™ 2024-11-25 02:26:32 +01:00
parent 2db14012f8
commit d6d890b259
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 41 additions and 19 deletions

View file

@ -338,7 +338,7 @@ public final class RenderingSubsystemConfiguration extends Configuration {
renderingAdapter = RenderingAdapter.ANY;
vsyncMode = VsyncMode.OFF;
maximumFramesPerSecond = 0;
maximumFramesPerSecond = 60;
}
/** {@inheritDoc} */

View file

@ -101,17 +101,22 @@ public final class Renderer {
private static long frameCount = 0L;
/**
* Contains the delta time, also
* known as the render time.
* Contains the delta time.
* <p>
* Updated every second.
* Delta time is the time in seconds
* between the last and current frame.
* <p>
* Updated every frame.
*
* @since v1-alpha9
* -- GETTER --
* Returns the delta time, also
* known as the render time.
* <p>
* Updated every second.
* Delta time is the time in seconds
* between the last and current frame.
* <p>
* Updated every frame.
*
* @return delta time
* @since v1-alpha9
@ -140,6 +145,11 @@ public final class Renderer {
* Contains the time it took
* to calculate the last frame.
* <p>
* <b>Note</b>: The {@code Waiting} count may be
* negative if the renderer is lagging behind.
* No waiting will be performed internally if
* this count is {@code <0}.
* <p>
* Updated every frame.
*
* @since v1-alpha9
@ -187,15 +197,22 @@ public final class Renderer {
/** {@inheritDoc} */
@Override
public void run() {
int offset = 2;
int offset = 7;
bgfx_dbg_text_clear(0, false);
bgfx_dbg_text_printf(0, 0, 0x0f, "It's " + NumberUtil.padNumbers(LocalTime.now().getHour(), 2) + ":" + NumberUtil.padNumbers(LocalTime.now().getMinute(), 2) + ":" + NumberUtil.padNumbers(LocalTime.now().getSecond(), 2));
bgfx_dbg_text_printf(0, 1, 0x0f, "LFT.size(): " + lastFrameTime.size());
bgfx_dbg_text_printf(0, 0, 0x0f, "Time: " + NumberUtil.padNumbers(LocalTime.now().getHour(), 2) + ":" + NumberUtil.padNumbers(LocalTime.now().getMinute(), 2) + ":" + NumberUtil.padNumbers(LocalTime.now().getSecond(), 2));
bgfx_dbg_text_printf(0, 1, 0x0f, "Frames: " + Renderer.getFrameCount());
bgfx_dbg_text_printf(0, 2, 0x0f, "Frames/s: " + Renderer.getFramesPerSecond());
bgfx_dbg_text_printf(0, 3, 0x0f, "Delta: " + Renderer.getDeltaTime());
bgfx_dbg_text_printf(0, 4, 0x0f, "V-Sync mode: " + RenderingSubsystemConfiguration.getInstance().getVsyncMode().name());
bgfx_dbg_text_printf(0, 6, 0x0f, "Rendering time:");
for (String item : lastFrameTime.keySet()) {
bgfx_dbg_text_printf(0, offset, 0x0f, item + ": " + lastFrameTime.get(item) + "ms");
offset += 1;
}
try {
Thread.sleep(0);
} catch (InterruptedException ignored) {}
}
});
@ -272,7 +289,9 @@ public final class Renderer {
@SuppressWarnings({ "InfiniteLoopStatement" })
private static void render() throws Throwable {
long previousFrameCount = 0L; // Frame count one second ago
LinkedList<Long> deltaTimeFractions = new LinkedList<>(); // Contains all delta time fractions
long systemTimeNow; // Current system time
long systemTimePrevious = System.currentTimeMillis(); // Previous system time
LinkedList<Double> deltaTimes = new LinkedList<>(); // Contains all delta time fractions
Map<String, Long> execTimes = new LinkedHashMap<>(); // Contains the amount of time of all rendering operations
long timesWait; // Time to wait until the next frame
long timesPSO = System.currentTimeMillis() + 1000; // Time to wait until invoking per-second operations
@ -337,15 +356,17 @@ public final class Renderer {
// Perform per-frame operations
frameCount += 1;
lastFrameTime = new HashMap<>(execTimes);
deltaTimeFractions.add(execTimes.get("Rendering") + execTimes.get("Waiting"));
execTimes.clear();
systemTimeNow = System.currentTimeMillis();
deltaTime = (double) (systemTimeNow - systemTimePrevious) / 1000;
systemTimePrevious = systemTimeNow;
deltaTimes.add(deltaTime);
// Perform per-second operations
if (System.currentTimeMillis() >= timesPSO) {
// Calculate delta time and FPS count
deltaTime = NumberUtil.calculateMeanLong(deltaTimeFractions);
framesPerSecond = 1000 / deltaTime;
// Calculate FPS count
framesPerSecond = 1 / deltaTime;
// Log frame count
if (RenderingSubsystemConfiguration.getInstance().isDebugFrames())
@ -376,7 +397,7 @@ public final class Renderer {
// Reset per-second variables
previousFrameCount = frameCount;
deltaTimeFractions.clear();
deltaTimes.clear();
timesPSO = System.currentTimeMillis() + 1000;
}
}

View file

@ -503,7 +503,7 @@ public final class Window implements Closeable {
glfwWindowHintString(GLFW_X11_INSTANCE_NAME, name);
// Create window
internalWindowIdentifier = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
internalWindowIdentifier = glfwCreateWindow(1, 1, "", NULL, NULL);
if (internalWindowIdentifier == NULL)
throw new WindowCreationFailureException();
@ -849,6 +849,7 @@ public final class Window implements Closeable {
return;
this.title = title;
queuedPropertyUpdates.put("title", title);
}
/**