From 80478e6a69a425d2298e32dda45a88a40cbcb4ab Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 21 Jul 2024 21:12:59 +0200 Subject: [PATCH] Improve Graphics API initialization --- .../graphics/opengl/OpenGlSubsystem.java | 28 +++++++-------- .../GlfwInitializationException.java | 34 +++++++++++++++++++ .../exceptions/NotOnMainThreadException.java | 9 ++++- .../sosengine/graphics/GraphicsSubsystem.java | 26 ++++++++------ .../sosengine/testapp/Main.java | 3 +- 5 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/GlfwInitializationException.java diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java index a34e994..4e91990 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java @@ -24,6 +24,7 @@ import de.staropensource.sosengine.base.annotations.EventListener; import de.staropensource.sosengine.base.classes.helpers.EventHelper; import de.staropensource.sosengine.base.data.info.EngineInformation; import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem; +import de.staropensource.sosengine.base.internal.events.InternalEngineShutdownEvent; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.DependencyVector; @@ -36,6 +37,7 @@ import de.staropensource.sosengine.graphics.classes.ApiMainClass; import de.staropensource.sosengine.graphics.classes.ApiManagementClass; import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent; import de.staropensource.sosengine.graphics.opengl.events.GraphicsErrorEvent; +import de.staropensource.sosengine.graphics.opengl.exceptions.GlfwInitializationException; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFWErrorCallback; @@ -133,24 +135,18 @@ public final class OpenGlSubsystem implements ApiMainClass { /** {@inheritDoc} */ @Override public void initializeApi() { - logger.verb("Initializing Graphics API"); + // Set error callback + try (GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent())) { + errorCallback.set(); + } - long initTime = Miscellaneous.measureExecutionTime(() -> { - // Set error callback - try (GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent())) { - errorCallback.set(); - } + // Initialize GLFW + if (!glfwInit()) + throw new GlfwInitializationException(); - // Initialize GLFW - if (!glfwInit()) - logger.crash("Unable to initialize GLFW"); - - // Initialize management class - internalApi = new OpenGlInternalApi(); - management = new OpenGlManagement(); - }); - - logger.info("Initialized Graphics API in " + initTime + "ms"); + // Initialize management class + internalApi = new OpenGlInternalApi(); + management = new OpenGlManagement(); } /** diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/GlfwInitializationException.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/GlfwInitializationException.java new file mode 100644 index 0000000..2c6f6a4 --- /dev/null +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/GlfwInitializationException.java @@ -0,0 +1,34 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Contributors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.sosengine.graphics.opengl.exceptions; + +/** + * Thrown when GLFW fails to initialize. + * + * @since v1-alpha2 + */ +public class GlfwInitializationException extends RuntimeException { + /** + * Constructs this exception. + * + * @since v1-alpha2 + */ + public GlfwInitializationException() {} +} diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/NotOnMainThreadException.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/NotOnMainThreadException.java index 938bd52..5870e02 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/NotOnMainThreadException.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/exceptions/NotOnMainThreadException.java @@ -24,4 +24,11 @@ package de.staropensource.sosengine.graphics.opengl.exceptions; * * @since v1-alpha2 */ -public class NotOnMainThreadException extends Throwable {} +public class NotOnMainThreadException extends RuntimeException { + /** + * Constructs this exception. + * + * @since v1-alpha2 + */ + public NotOnMainThreadException() {} +} diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java index 0b6e863..389d500 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java @@ -201,20 +201,21 @@ public final class GraphicsSubsystem implements SubsystemMainClass { // Check if registered apis are compatible for (String apiName : registeredApis.keySet()) if (registeredApis.get(apiName).isCompatible()) { - logger.diag("Choosing a compatible Graphics API: " + apiName + " is compatible"); + logger.diag("" + apiName + " is compatible"); compatibleApis.add(apiName); } else - logger.diag("Choosing a compatible Graphics API: " + apiName + " is not compatible"); + logger.diag(apiName + " is not compatible"); - logger.diag("Choosing a compatible Graphics API: Compatible are: " + ListFormatter.formatList(compatibleApis)); + logger.diag("Compatible are " + ListFormatter.formatList(compatibleApis)); // Choose last item in list. if (!compatibleApis.isEmpty()) { - logger.verb("Choosing a compatible Graphics API: Chose Graphics API " + compatibleApis.getLast()); + logger.verb("Chose Graphics API " + compatibleApis.getLast()); api = registeredApis.get(compatibleApis.getLast()); try { - api.initializeApi(); + logger.diag("Initializing Graphics API " + api.getApiName()); + logger.diag("Initialized Graphics API " + api.getApiName() + " in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms"); } catch (Throwable throwable) { logger.crash("Graphics API " + api.getApiName() + " failed to initialize", throwable, true); throw throwable; @@ -231,20 +232,25 @@ public final class GraphicsSubsystem implements SubsystemMainClass { * Sets the Graphics API to use. * * @param name name of the Graphics API + * @return if the Graphics API has been found * @see GraphicsSubsystem#setGraphicsApi() * @since v1-alpha0 */ - public void setGraphicsApi(@NotNull String name) { - logger.verb("Setting Graphics API " + name); - + public boolean setGraphicsApi(@NotNull String name) { if (!registeredApis.containsKey(name)) - logger.crash("Unable to set Graphics API: Graphics API " + name + " is not registered"); + return false; + + logger.verb("Setting Graphics API " + name); if (api == null) api = registeredApis.get(name); else logger.crash("Unable to set Graphics API: Graphics API " + api.getApiName() + " already registered"); - api.initializeApi(); + // Initialize API + logger.diag("Initializing Graphics API " + api.getApiName()); + logger.diag("Initialized Graphics API " + api.getApiName() + " in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms"); + + return true; } } 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 a9de5b0..549c775 100644 --- a/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java +++ b/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java @@ -103,9 +103,8 @@ public class Main { logger.info("Hello world!"); // Choose Graphics API to use - if (!GraphicsSubsystem.getInstance().setGraphicsApi()) { + if (!GraphicsSubsystem.getInstance().setGraphicsApi()) logger.crash("No Graphics API is compatible"); - } // Define 'api' & 'management' variables ApiMainClass api = GraphicsSubsystem.getInstance().getApi();