From 4f9154f5fcb097e3d5f1fdf07af3beeb0c69de5c Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 26 Jul 2024 13:05:44 +0200 Subject: [PATCH] Make V-Sync global --- .../graphics/glfw/classes/GlfwWindow.java | 8 +-- .../graphics/opengl/classes/OpenGlWindow.java | 5 +- .../GraphicsSubsystemConfiguration.java | 34 +++++++++- .../sosengine/graphics/classes/Window.java | 64 ++----------------- .../sosengine/testapp/Main.java | 2 - 5 files changed, 41 insertions(+), 72 deletions(-) diff --git a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java index 7b6a55d..fa973e5 100644 --- a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java +++ b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java @@ -23,6 +23,7 @@ import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException; import de.staropensource.sosengine.base.types.Tristate; import de.staropensource.sosengine.base.types.vectors.Vec2i; 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.events.GraphicsApiErrorEvent; import de.staropensource.sosengine.graphics.events.InputEvent; @@ -89,7 +90,6 @@ public abstract class GlfwWindow extends Window { * @param maximumSize maximum size * @param position position * @param windowMode window mode - * @param vsyncMode V-Sync mode * @param resizable resizable flag * @param borderless borderless flag * @param focusable focusable flag @@ -99,8 +99,8 @@ public abstract class GlfwWindow extends Window { * @throws UnexpectedThrowableException stuff thrown by the {@link #initializeWindow()} and {@link #render()} methods of the implementing Graphics API * @since v1-alpha2 */ - public GlfwWindow(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, @NotNull VsyncMode vsyncMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { - super(name, title, size, minimumSize, maximumSize, position, windowMode, vsyncMode, resizable, borderless, focusable, onTop, transparent, rendering); + public GlfwWindow(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { + super(name, title, size, minimumSize, maximumSize, position, windowMode, resizable, borderless, focusable, onTop, transparent, rendering); } /** {@inheritDoc} */ @@ -162,7 +162,7 @@ public abstract class GlfwWindow extends Window { ownContext(); // Set swap interval based on V-Sync mode setting - glfwSwapInterval(getVsyncMode() == VsyncMode.ON ? 1 : 0); + glfwSwapInterval(GraphicsSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.ON ? 1 : 0); // Create callbacks keyCallback = GLFWKeyCallback.create(new KeyCallback(this)); diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/classes/OpenGlWindow.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/classes/OpenGlWindow.java index 7de19eb..cc4a00e 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/classes/OpenGlWindow.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/classes/OpenGlWindow.java @@ -49,7 +49,6 @@ public class OpenGlWindow extends GlfwWindow { * @param maximumSize maximum size * @param position position * @param windowMode window mode - * @param vsyncMode V-Sync mode * @param resizable resizable flag * @param borderless borderless flag * @param focusable focusable flag @@ -59,8 +58,8 @@ public class OpenGlWindow extends GlfwWindow { * @throws UnexpectedThrowableException stuff thrown by the {@link #initializeWindow()} and {@link #render()} methods of the implementing Graphics API * @since v1-alpha2 */ - public OpenGlWindow(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, @NotNull VsyncMode vsyncMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { - super(name, title, size, minimumSize, maximumSize, position, windowMode, vsyncMode, resizable, borderless, focusable, onTop, transparent, rendering); + public OpenGlWindow(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { + super(name, title, size, minimumSize, maximumSize, position, windowMode, resizable, borderless, focusable, onTop, transparent, rendering); } /** {@inheritDoc} */ diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java index a191812..74e9b9f 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java @@ -25,6 +25,7 @@ import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.logging.LogIssuer; import de.staropensource.sosengine.base.utility.parser.PropertyParser; import de.staropensource.sosengine.graphics.events.GraphicsErrorEvent; +import de.staropensource.sosengine.graphics.types.window.VsyncMode; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -128,7 +129,25 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat private boolean errorGraphicsError; /** - * Determines how many frames can be rendered max per second. + * Determines how many frames can be rendered per second. + *

+ * This setting determines if and how V-Sync will operate, which + * (if enabled) tries to synchronize the frame rate to the monitor's + * refresh rate. See {@link VsyncMode} for more information. + * + * @since v1-alpha2 + * + * -- GETTER -- + * Gets the value for {@link #vsyncMode}. + * + * @return variable value + * @see #vsyncMode + * @since v1-alpha2 + */ + private VsyncMode vsyncMode; + + /** + * Determines how many frames can be rendered per second. *

* This value will have no effect on windows with V-Sync enabled. * Set to {@code 0} for no limit. @@ -136,7 +155,7 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat * @since v1-alpha2 * * -- GETTER -- - * Gets the value for {@link #maximumFramesPerSecond} + * Gets the value for {@link #maximumFramesPerSecond}. * * @return variable value * @see #maximumFramesPerSecond @@ -181,6 +200,13 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat case "errorGraphicsError" -> errorGraphicsError = parser.getBoolean(group + property); + case "vsyncMode" -> { + try { + vsyncMode = VsyncMode.valueOf(parser.getString(group + property).toUpperCase()); + } catch (IllegalArgumentException exception) { + Logger.error(new LogIssuer(getClass(), CodePart.ENGINE), "V-Sync mode " + parser.getString(group + property) + " is not valid"); + } + } case "maximumFramesPerSecond" -> maximumFramesPerSecond = parser.getInteger(group + property, true); } } catch (NullPointerException ignored) {} @@ -206,7 +232,8 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat errorGraphicsError = true; - maximumFramesPerSecond = 60; + vsyncMode = VsyncMode.OFF; + maximumFramesPerSecond = 120; } /** {@inheritDoc} */ @@ -219,6 +246,7 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat case "errorGraphicsError" -> { return errorGraphicsError; } + case "vsyncMode" -> { return vsyncMode; } case "maximumFramesPerSecond" -> { return maximumFramesPerSecond; } default -> { return null; } } diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/Window.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/Window.java index 0c15e6d..7ca31be 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/Window.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/Window.java @@ -23,11 +23,9 @@ import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.Tristate; -import de.staropensource.sosengine.base.types.immutable.ImmutableHashSet; import de.staropensource.sosengine.base.types.logging.LogIssuer; import de.staropensource.sosengine.base.types.vectors.Vec2i; import de.staropensource.sosengine.graphics.GraphicsSubsystem; -import de.staropensource.sosengine.graphics.types.window.VsyncMode; import de.staropensource.sosengine.graphics.types.window.WindowMode; import lombok.AccessLevel; import lombok.Getter; @@ -319,27 +317,6 @@ public abstract class Window implements AutoCloseable { @Setter private WindowMode windowMode; - /** - * Determines the V-Sync mode this window targets. - * - * @since v1-alpha2 - * - * -- GETTER -- - * Returns the V-Sync mode. - * - * @return V-Sync mode - * @since v1-alpha2 - * - * -- SETTER -- - * Sets the V-Sync mode. - * - * @param vsyncMode new vsync mode - * @since v1-alpha2 - */ - @NotNull - @Setter - private VsyncMode vsyncMode; - /** * Determines how fast the window may update it's contents. * @@ -508,7 +485,6 @@ public abstract class Window implements AutoCloseable { * @param maximumSize maximum size * @param position position * @param windowMode window mode - * @param vsyncMode V-Sync mode * @param resizable resizable flag * @param borderless borderless flag * @param focusable focusable flag @@ -518,8 +494,8 @@ public abstract class Window implements AutoCloseable { * @throws UnexpectedThrowableException stuff thrown by the {@link #initializeWindow()} and {@link #render()} methods of the implementing Graphics API * @since v1-alpha2 */ - public Window(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, @NotNull VsyncMode vsyncMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { - logger.diag("Creating new window with properties: name=\"" + name + "\" title=\"" + title + "\" size=" + size + " minimumSize=" + minimumSize + " maximumSize=" + maximumSize + " position=" + position + " windowMode=" + windowMode + " vsyncMode=" + vsyncMode + " resizable=" + resizable + " borderless=" + borderless + " focusable=" + focusable + " onTop=" + onTop + " transparent=" + transparent + " rendering=" + rendering); + public Window(@NotNull String name, @NotNull String title, @NotNull Vec2i size, @NotNull Vec2i minimumSize, @NotNull Vec2i maximumSize, @NotNull Vec2i position, @NotNull WindowMode windowMode, boolean resizable, boolean borderless, boolean focusable, boolean onTop, boolean transparent, boolean rendering) throws UnexpectedThrowableException { + logger.diag("Creating new window with properties: name=\"" + name + "\" title=\"" + title + "\" size=" + size + " minimumSize=" + minimumSize + " maximumSize=" + maximumSize + " position=" + position + " windowMode=" + windowMode + " resizable=" + resizable + " borderless=" + borderless + " focusable=" + focusable + " onTop=" + onTop + " transparent=" + transparent + " rendering=" + rendering); // Initialize variables this.name = name; @@ -529,7 +505,6 @@ public abstract class Window implements AutoCloseable { this.maximumSize = maximumSize; this.position = position; this.windowMode = windowMode; - this.vsyncMode = vsyncMode; this.resizable = resizable; this.borderless = borderless; this.focusable = focusable; @@ -777,22 +752,6 @@ public abstract class Window implements AutoCloseable { @Nullable private WindowMode windowMode = null; - /** - * Contains the V-Sync mode. - * - * @see Window#vsyncMode - * @since v1-alpha2 - * - * -- GETTER -- - * Returns the V-Sync mode. - * - * @return V-Sync mode - * @see Window#vsyncMode - * @since v1-alpha2 - */ - @Nullable - private VsyncMode vsyncMode = null; - /** * Contains the resizable flag. * @@ -929,8 +888,6 @@ public abstract class Window implements AutoCloseable { maximumSize = new Vec2i(-1, -1); if (windowMode == null) windowMode = WindowMode.WINDOWED; - if (vsyncMode == null) - vsyncMode = VsyncMode.ON; // Override booleanized tristate defaults if (resizable == Tristate.FALSE) @@ -949,8 +906,8 @@ public abstract class Window implements AutoCloseable { // Create new Window instance try { return GraphicsSubsystem.getInstance().getApi().getInternalApi().getWindowClass() - .getDeclaredConstructor(String.class, String.class, Vec2i.class, Vec2i.class, Vec2i.class, Vec2i.class, WindowMode.class, VsyncMode.class, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE) - .newInstance(name, title, size, minimumSize, maximumSize, position, windowMode, vsyncMode, resizableBoolean, borderlessBoolean, focusableBoolean, onTopBoolean, transparentBoolean, renderingBoolean); + .getDeclaredConstructor(String.class, String.class, Vec2i.class, Vec2i.class, Vec2i.class, Vec2i.class, WindowMode.class, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE) + .newInstance(name, title, size, minimumSize, maximumSize, position, windowMode, resizableBoolean, borderlessBoolean, focusableBoolean, onTopBoolean, transparentBoolean, renderingBoolean); } catch (Throwable throwable) { throw new UnexpectedThrowableException(throwable, "Window.Builder was unable to create new Window instance"); } @@ -1047,19 +1004,6 @@ public abstract class Window implements AutoCloseable { return this; } - /** - * Sets the V-Sync mode. - * - * @param vsyncMode new V-Sync mode - * @return builder instance - * @since v1-alpha2 - */ - @NotNull - public synchronized Builder setVsyncMode(@Nullable VsyncMode vsyncMode) { - this.vsyncMode = vsyncMode; - return this; - } - /** * Sets the resizable flag. * diff --git a/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java b/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java index 7baad6f..21360dc 100644 --- a/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java +++ b/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java @@ -42,7 +42,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; -import java.util.LinkedList; /** * The initialization class for sos!engine's development application. @@ -139,7 +138,6 @@ public class Main { .setTitle("test application window") .setSize(new Vec2i(960, 540)) .setPosition(new Vec2i(10, 10)) - .setVsyncMode(VsyncMode.OFF) .build(); } catch (Throwable throwable) { logger.crash("Window.Builder#build() failed", throwable);