Fix delta time and FPS count calculation

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; renderingAdapter = RenderingAdapter.ANY;
vsyncMode = VsyncMode.OFF; vsyncMode = VsyncMode.OFF;
maximumFramesPerSecond = 0; maximumFramesPerSecond = 60;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View file

@ -101,17 +101,22 @@ public final class Renderer {
private static long frameCount = 0L; private static long frameCount = 0L;
/** /**
* Contains the delta time, also * Contains the delta time.
* known as the render time.
* <p> * <p>
* Updated every second. * Delta time is the time in seconds
* between the last and current frame.
* <p>
* Updated every frame.
* *
* @since v1-alpha9 * @since v1-alpha9
* -- GETTER -- * -- GETTER --
* Returns the delta time, also * Returns the delta time, also
* known as the render time. * known as the render time.
* <p> * <p>
* Updated every second. * Delta time is the time in seconds
* between the last and current frame.
* <p>
* Updated every frame.
* *
* @return delta time * @return delta time
* @since v1-alpha9 * @since v1-alpha9
@ -140,6 +145,11 @@ public final class Renderer {
* Contains the time it took * Contains the time it took
* to calculate the last frame. * to calculate the last frame.
* <p> * <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. * Updated every frame.
* *
* @since v1-alpha9 * @since v1-alpha9
@ -187,15 +197,22 @@ public final class Renderer {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void run() { public void run() {
int offset = 2; int offset = 7;
bgfx_dbg_text_clear(0, false); 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, 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, "LFT.size(): " + lastFrameTime.size()); 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()) { for (String item : lastFrameTime.keySet()) {
bgfx_dbg_text_printf(0, offset, 0x0f, item + ": " + lastFrameTime.get(item) + "ms"); bgfx_dbg_text_printf(0, offset, 0x0f, item + ": " + lastFrameTime.get(item) + "ms");
offset += 1; offset += 1;
} }
try {
Thread.sleep(0);
} catch (InterruptedException ignored) {}
} }
}); });
@ -271,11 +288,13 @@ public final class Renderer {
*/ */
@SuppressWarnings({ "InfiniteLoopStatement" }) @SuppressWarnings({ "InfiniteLoopStatement" })
private static void render() throws Throwable { private static void render() throws Throwable {
long previousFrameCount = 0L; // Frame count one second ago long previousFrameCount = 0L; // Frame count one second ago
LinkedList<Long> deltaTimeFractions = new LinkedList<>(); // Contains all delta time fractions long systemTimeNow; // Current system time
Map<String, Long> execTimes = new LinkedHashMap<>(); // Contains the amount of time of all rendering operations long systemTimePrevious = System.currentTimeMillis(); // Previous system time
long timesWait; // Time to wait until the next frame LinkedList<Double> deltaTimes = new LinkedList<>(); // Contains all delta time fractions
long timesPSO = System.currentTimeMillis() + 1000; // Time to wait until invoking per-second operations 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
while (true) { while (true) {
// Invoke frame handlers // Invoke frame handlers
@ -337,15 +356,17 @@ public final class Renderer {
// Perform per-frame operations // Perform per-frame operations
frameCount += 1; frameCount += 1;
lastFrameTime = new HashMap<>(execTimes); lastFrameTime = new HashMap<>(execTimes);
deltaTimeFractions.add(execTimes.get("Rendering") + execTimes.get("Waiting"));
execTimes.clear(); execTimes.clear();
systemTimeNow = System.currentTimeMillis();
deltaTime = (double) (systemTimeNow - systemTimePrevious) / 1000;
systemTimePrevious = systemTimeNow;
deltaTimes.add(deltaTime);
// Perform per-second operations // Perform per-second operations
if (System.currentTimeMillis() >= timesPSO) { if (System.currentTimeMillis() >= timesPSO) {
// Calculate delta time and FPS count // Calculate FPS count
deltaTime = NumberUtil.calculateMeanLong(deltaTimeFractions); framesPerSecond = 1 / deltaTime;
framesPerSecond = 1000 / deltaTime;
// Log frame count // Log frame count
if (RenderingSubsystemConfiguration.getInstance().isDebugFrames()) if (RenderingSubsystemConfiguration.getInstance().isDebugFrames())
@ -376,7 +397,7 @@ public final class Renderer {
// Reset per-second variables // Reset per-second variables
previousFrameCount = frameCount; previousFrameCount = frameCount;
deltaTimeFractions.clear(); deltaTimes.clear();
timesPSO = System.currentTimeMillis() + 1000; timesPSO = System.currentTimeMillis() + 1000;
} }
} }

View file

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