Fix V-Sync rendering

Turns out I did not need the monitor API for that. Just reading glfwSwapInterval's documentation did the job and fixing my existing code did the job.
This commit is contained in:
JeremyStar™ 2024-07-27 02:28:00 +02:00
parent 935bbaf7ce
commit d4c5b982df
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 18 additions and 12 deletions

View file

@ -232,8 +232,8 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat
errorGraphicsError = true; errorGraphicsError = true;
vsyncMode = VsyncMode.OFF; vsyncMode = VsyncMode.ON;
maximumFramesPerSecond = 120; maximumFramesPerSecond = 60;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View file

@ -26,6 +26,7 @@ import de.staropensource.sosengine.base.utility.Math;
import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.graphics.GraphicsSubsystemConfiguration; import de.staropensource.sosengine.graphics.GraphicsSubsystemConfiguration;
import de.staropensource.sosengine.graphics.classes.Window; import de.staropensource.sosengine.graphics.classes.Window;
import de.staropensource.sosengine.graphics.types.window.VsyncMode;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -83,6 +84,7 @@ public abstract class ApiManagementClass {
* @since v1-alpha2 * @since v1-alpha2
*/ */
public LinkedHashMap<@NotNull Window, @NotNull Throwable> runRenderLoopContinuously(@NotNull Runnable frameCode) { public LinkedHashMap<@NotNull Window, @NotNull Throwable> runRenderLoopContinuously(@NotNull Runnable frameCode) {
// Define variables
AtomicReference<LinkedHashMap<@NotNull Window, @NotNull Throwable>> output = new AtomicReference<>(new LinkedHashMap<>()); // runRenderLoop output AtomicReference<LinkedHashMap<@NotNull Window, @NotNull Throwable>> output = new AtomicReference<>(new LinkedHashMap<>()); // runRenderLoop output
long renderTime; // Amount of time spent rendering long renderTime; // Amount of time spent rendering
long sleepDuration; // Time spent sleeping the thread long sleepDuration; // Time spent sleeping the thread
@ -97,19 +99,23 @@ public abstract class ApiManagementClass {
frameCode.run(); frameCode.run();
}); });
// TODO v-sync is currently broken, monitor api needs to be implemented first if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() != VsyncMode.OFF)
// V-Sync is enabled, no need for manual busy waiting
// Calculate amount of time the thread should spend sleeping sleepDuration = 0L;
sleepDuration = (long) (1d / GraphicsSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() * 1000d) - renderTime; else
// Calculate amount of time the thread should spend sleeping
sleepDuration = (long) (1d / GraphicsSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() * 1000d) - renderTime;
// Add render and sleep time to list used for calculating the delta time value // Add render and sleep time to list used for calculating the delta time value
splitDeltaTime.add(renderTime + sleepDuration); splitDeltaTime.add(renderTime + sleepDuration);
try { // Busy wait unless V-Sync is enabled
//noinspection BusyWait // true, true if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF)
Thread.sleep(sleepDuration); try {
} catch (InterruptedException exception) { //noinspection BusyWait // true, true, but there's no other way to do it
logger.crash("Rendering loop got interrupted. This is unsupported behaviour.", exception); Thread.sleep(sleepDuration);
} } catch (InterruptedException exception) {
logger.crash("Rendering loop got interrupted. This is unsupported behaviour", exception);
}
// Calculate delta time and frame count every second // Calculate delta time and frame count every second
if (System.currentTimeMillis() >= reportDuration + 1000) { if (System.currentTimeMillis() >= reportDuration + 1000) {