Improve Graphics API initialization

This commit is contained in:
JeremyStar™ 2024-07-21 21:12:59 +02:00
parent 2469e124bd
commit 80478e6a69
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 71 additions and 29 deletions

View file

@ -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,9 +135,6 @@ public final class OpenGlSubsystem implements ApiMainClass {
/** {@inheritDoc} */
@Override
public void initializeApi() {
logger.verb("Initializing Graphics API");
long initTime = Miscellaneous.measureExecutionTime(() -> {
// Set error callback
try (GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent())) {
errorCallback.set();
@ -143,14 +142,11 @@ public final class OpenGlSubsystem implements ApiMainClass {
// Initialize GLFW
if (!glfwInit())
logger.crash("Unable to initialize GLFW");
throw new GlfwInitializationException();
// Initialize management class
internalApi = new OpenGlInternalApi();
management = new OpenGlManagement();
});
logger.info("Initialized Graphics API in " + initTime + "ms");
}
/**

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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() {}
}

View file

@ -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() {}
}

View file

@ -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;
}
}

View file

@ -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();