From d6d890b259c227e150351ddf991d8ca67e276d6c Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 25 Nov 2024 02:26:32 +0100 Subject: [PATCH] Fix delta time and FPS count calculation --- .../RenderingSubsystemConfiguration.java | 2 +- .../engine/rendering/renderer/Renderer.java | 55 +++++++++++++------ .../engine/rendering/type/Window.java | 3 +- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/rendering/src/main/java/de/staropensource/engine/rendering/RenderingSubsystemConfiguration.java b/rendering/src/main/java/de/staropensource/engine/rendering/RenderingSubsystemConfiguration.java index d0ac7fe..5255d56 100644 --- a/rendering/src/main/java/de/staropensource/engine/rendering/RenderingSubsystemConfiguration.java +++ b/rendering/src/main/java/de/staropensource/engine/rendering/RenderingSubsystemConfiguration.java @@ -338,7 +338,7 @@ public final class RenderingSubsystemConfiguration extends Configuration { renderingAdapter = RenderingAdapter.ANY; vsyncMode = VsyncMode.OFF; - maximumFramesPerSecond = 0; + maximumFramesPerSecond = 60; } /** {@inheritDoc} */ diff --git a/rendering/src/main/java/de/staropensource/engine/rendering/renderer/Renderer.java b/rendering/src/main/java/de/staropensource/engine/rendering/renderer/Renderer.java index 563d4c0..768821e 100644 --- a/rendering/src/main/java/de/staropensource/engine/rendering/renderer/Renderer.java +++ b/rendering/src/main/java/de/staropensource/engine/rendering/renderer/Renderer.java @@ -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. *

- * Updated every second. + * Delta time is the time in seconds + * between the last and current frame. + *

+ * Updated every frame. * * @since v1-alpha9 * -- GETTER -- * Returns the delta time, also * known as the render time. *

- * Updated every second. + * Delta time is the time in seconds + * between the last and current frame. + *

+ * 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. *

+ * Note: 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}. + *

* 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) {} } }); @@ -271,11 +288,13 @@ public final class Renderer { */ @SuppressWarnings({ "InfiniteLoopStatement" }) private static void render() throws Throwable { - long previousFrameCount = 0L; // Frame count one second ago - LinkedList deltaTimeFractions = new LinkedList<>(); // Contains all delta time fractions - Map 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 + long previousFrameCount = 0L; // Frame count one second ago + long systemTimeNow; // Current system time + long systemTimePrevious = System.currentTimeMillis(); // Previous system time + LinkedList deltaTimes = new LinkedList<>(); // Contains all delta time fractions + Map 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) { // Invoke frame handlers @@ -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; } } diff --git a/rendering/src/main/java/de/staropensource/engine/rendering/type/Window.java b/rendering/src/main/java/de/staropensource/engine/rendering/type/Window.java index bbe44d5..ed6b7f7 100644 --- a/rendering/src/main/java/de/staropensource/engine/rendering/type/Window.java +++ b/rendering/src/main/java/de/staropensource/engine/rendering/type/Window.java @@ -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); } /**