forked from StarOpenSource/Engine
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:
parent
935bbaf7ce
commit
d4c5b982df
2 changed files with 18 additions and 12 deletions
|
@ -232,8 +232,8 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat
|
|||
|
||||
errorGraphicsError = true;
|
||||
|
||||
vsyncMode = VsyncMode.OFF;
|
||||
maximumFramesPerSecond = 120;
|
||||
vsyncMode = VsyncMode.ON;
|
||||
maximumFramesPerSecond = 60;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
|
|
|
@ -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,18 +99,22 @@ public abstract class ApiManagementClass {
|
|||
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
|
||||
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);
|
||||
|
||||
// Busy wait unless V-Sync is enabled
|
||||
if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF)
|
||||
try {
|
||||
//noinspection BusyWait // true, true
|
||||
//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);
|
||||
logger.crash("Rendering loop got interrupted. This is unsupported behaviour", exception);
|
||||
}
|
||||
|
||||
// Calculate delta time and frame count every second
|
||||
|
|
Loading…
Reference in a new issue