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;
vsyncMode = VsyncMode.OFF;
maximumFramesPerSecond = 120;
vsyncMode = VsyncMode.ON;
maximumFramesPerSecond = 60;
}
/** {@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.graphics.GraphicsSubsystemConfiguration;
import de.staropensource.sosengine.graphics.classes.Window;
import de.staropensource.sosengine.graphics.types.window.VsyncMode;
import lombok.AccessLevel;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@ -83,6 +84,7 @@ public abstract class ApiManagementClass {
* @since v1-alpha2
*/
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
long renderTime; // Amount of time spent rendering
long sleepDuration; // Time spent sleeping the thread
@ -97,19 +99,23 @@ public abstract class ApiManagementClass {
frameCode.run();
});
// TODO v-sync is currently broken, monitor api needs to be implemented first
// Calculate amount of time the thread should spend sleeping
sleepDuration = (long) (1d / GraphicsSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() * 1000d) - renderTime;
if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() != VsyncMode.OFF)
// V-Sync is enabled, no need for manual busy waiting
sleepDuration = 0L;
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
splitDeltaTime.add(renderTime + sleepDuration);
try {
//noinspection BusyWait // true, true
Thread.sleep(sleepDuration);
} catch (InterruptedException exception) {
logger.crash("Rendering loop got interrupted. This is unsupported behaviour.", exception);
}
// Busy wait unless V-Sync is enabled
if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF)
try {
//noinspection BusyWait // true, true, but there's no other way to do it
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
if (System.currentTimeMillis() >= reportDuration + 1000) {